I've been having fun in the exp-resources branch.
What I'm trying to achieve is a something like what the
google-protorpc library does. In protorpc you specify request and response types as "messages" and register "services" that are exposed. All your services, request and response messages are then discoverable via a "RegistryService".
I'm not such a fan of the RPC approach – obviously – I like REST and goweb :) But I feel having discovery service and typed requests/responses is an important and useful feature. It enables clients to be programatically built and removes much of the boilerplate code related with decoding the request bodies.
So, here's my concept of how resource controllers are built in exp-resources. Below is a quick example of a BookController with just a single method "Update".
// an entity struct
type Book struct {
Name string
Ibsn string
}
// the controller struct
type BookController struct {
*goweb.Context
}
// Update()
// accepts a Book from the request body
// returns a Book as the response
func (r *BookController) Update(book *Book) *Book {
&Book{"Dune", "12345"}
}
// This is the constructor passed to MapResource
func Books(cx *goweb.Context) *BookController {
return &BookController{cx}
}
A special "discovery" resource can then be mapped, which exposes the resources and types.
There is a full example with javascript API browser that uses the discovery resource to automatically build a client to let you play with the API, it's in the examples folder. To install and run.
First checkout the exp-resources branch:
cd goweb/examples/
./build_examples.sh
Then navigate to the simple api example:
cd simple_api_example
./start
Then visit:
And the browser should load.
Note: You will need a recent version of Go. It requires the changes to exp/regexp that came in after r60.