Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var JS []byte
JS is embedded content of supporting script. Can be served as-is.
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)
Status codes ¶
- 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.
Supported methods ¶
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
Status codes ¶
- 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
func New ¶
New exposes matched methods of object as HTTP endpoints. It's shorthand for Router(Index(object)).
Types ¶
type ExposedMethod ¶
type ExposedMethod struct {
// contains filtered or unexported fields
}
func (*ExposedMethod) Args ¶ added in v1.1.0
func (em *ExposedMethod) Args() []reflect.Type
func (*ExposedMethod) HasResponse ¶ added in v1.1.0
func (em *ExposedMethod) HasResponse() bool
func (*ExposedMethod) Response ¶ added in v1.1.0
func (em *ExposedMethod) Response() reflect.Type
func (*ExposedMethod) ServeHTTP ¶
func (em *ExposedMethod) ServeHTTP(writer http.ResponseWriter, request *http.Request)
Directories
¶
Path | Synopsis |
---|---|
cmd
|
|
demo
|
|
internal
|
|
Package jrpc defines JSON-oriented, simple version of HTTP RPC
|
Package jrpc defines JSON-oriented, simple version of HTTP RPC |
Package schema defines basic OpenAPI schema for RPC
|
Package schema defines basic OpenAPI schema for RPC |