This repository was archived by the owner on Sep 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
Home
crhym3 edited this page Apr 16, 2013
·
10 revisions
A possible way (just a thought for now) of registering a service and how the discovery doc would be inferred from it:
type Req struct {
A int `endpoints:"path"` // implies "required"
B float64 `endpoints:"required"` // does not imply "path"
C bool // default location is "query"
D string `endpoints:"{hello|goodbye},path"` // regexp "pattern"
}
type MyService struct {}
func (s *MyService) DoSomething(req *Req, resp *Resp) {
// ...
}
func init() {
s := new(MyService)
// Will implicitly call
// endpoints.RegisterService(s, "myservice", "v1", "My Service API", "description")
// then register methods of s and create a discovery doc inferred from
// their declarations.
endpoints.RegisterServiceWithDefaults(s)
// I could also modify defaults for a specific method
method := endpoints.MethodByName("MyService.DoSomething")
// or endpoints.Method(s.DoSomething)
// Default was POST because not all fields of Req struct are in "path" location,
// and not all fields have explicit location specified (either "path" or "query").
// Otherwise, this would've been inferred as GET.
method.SetHttpMethod("GET")
// Default was "do_something/{a}/{d}"
// (inferred from the method name and Req struct fields)
method.SetPath("/do/{a}/foo-bar/{d}")
// I could also move this method into a discovery "resources" section.
// Otherwise, this would've been placed into the top-leve "methods" of discovery doc.
method.SetResourceName("things")
// "my.things" would work too (think of "drive.files.list")
// Serve discovery and registered service methods over HTTP from the BackendService.
http.Handle("/_ah/spi/", endpoints.HttpHandler)
// or endpoints.HandleHttp()
}