Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Builder ¶
Builder creates new path-based, POST-only router, with custom receiver (aka session) for each request.
type API struct { User string // to be filled by Server } type Server struct {} func (srv *Server) newAPI(r *http.Request) (*API, error) {} // ... var server Server handler := Builder(server.newAPI)
Handler will return ¶
400 Bad Request in case payload can not be unmarshalled to arguments or number of arguments not enough.
404 Not Found in case method is not known (case-insensitive).
500 Internal Server Error in case method returned an error or factory returned error. Response payload will be error message (plain text)
200 OK in case everything fine
func Index ¶
func Index(object interface{}) map[string]*ExposedMethod
Index object's (usually pointer to struct) method. Matched public methods will be wrapped to http handler, which parses request body as JSON array and passes it to function. Result will be returned also as json.
Criteria for matching methods: no return values, or single return value/error, or two return values, where second one must be an error. First input argument could be context.Context which will be automatically wired from request.Context().
Foo() // OK Foo(ctx context.Context) // OK Foo(ctx context.Context, bar int, baz SomeObj) // OK Foo(bar int, baz string) // OK Foo(...) error // OK Foo(...) int // OK Foo(...) (int, error) // OK Foo(...) (int, int) // NOT ok - last argument is not an error
Handler will return ¶
400 Bad Request in case payload can not be unmarshalled to arguments or number of arguments not enough.
500 Internal Server Error in case method returned an error. Response payload will be error message (plain text)
200 OK in case everything fine
Types ¶
type ExposedMethod ¶
type ExposedMethod struct {
// contains filtered or unexported fields
}
func (*ExposedMethod) ServeHTTP ¶
func (em *ExposedMethod) ServeHTTP(writer http.ResponseWriter, request *http.Request)