Skip to content
This repository was archived by the owner on Sep 16, 2022. It is now read-only.
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 (REST+RPC) 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}")

    // Serve discovery + REST and RPC registered service methods.
    http.Handle("/_ah/spi/", endpoints.HttpHandler)
    // or endpoints.HandleHttp()
}

Clone this wiki locally