Documentation ¶
Overview ¶
Package ups supports implementing http microservices using Protocol Buffers.
Index ¶
- Variables
- func UPS(handler interface{}) http.Handler
- func UPSWithConfig(handler interface{}, config Config) http.Handler
- func UPSWithParameter(handler interface{}, parameter interface{}) http.Handler
- func UPSWithParameterAndConfig(handler interface{}, parameter interface{}, config Config) http.Handler
- type Config
- type StatusCoder
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( DefaultConfig = Config{ JSONMarshaler: &jsonpb.Marshaler{OrigName: true}, LogError: func(ctx context.Context, tag string, err error) { log.Printf("ERROR: %s: %s", tag, err.Error()) }, LogPanic: func(ctx context.Context, err interface{}) { log.Printf("PANIC: %v: %s", err, debug.Stack()) }, LogStartRequest: func(ctx context.Context, method string, url *url.URL) { log.Printf("%s %s", method, url) }, LogEndRequest: func(ctx context.Context, method string, url *url.URL, statusCode int) { log.Printf("STATUS: %d %s", statusCode, url) }, LogRequestMessage: func(ctx context.Context, req proto.Message) { log.Printf("REQ proto: %s", req.String()) }, LogResponseMessage: func(ctx context.Context, resp proto.Message) { log.Printf("RESP proto: %s", resp.String()) }, LogRequestBytes: func(ctx context.Context, req []byte) { log.Printf("REQ bytes: %x", req) }, LogResponseBytes: func(ctx context.Context, resp []byte) { log.Printf("RESP bytes: %x", resp) }, LogRequestJSON: func(ctx context.Context, req string) { log.Printf("REQ JSON: %s", req) }, LogResponseJSON: func(ctx context.Context, resp string) { log.Printf("RESP JSON: %s", resp) }, } )
Functions ¶
func UPS ¶
UPS takes a func and creates an http.Handler using the DefaultConfig.
The func must take take one or two arguments and return one or two values.
The func must return a proto.Message, which will be marshalled into the response, or return a (proto.Message, error). If the error is not nil, the response will be 500 HTTP status unless the error implements StatusCoder, in which case it will provide the HTTP status of the response.
If the func takes one argument, it must be a proto.Message, which will be unmarshalled from the request body.
If the func takes two arguments, the first argument must either be a context.Context or a *http.Request, and the second argument must be a proto.Message.
UPS will panic if the argument is not a valid func.
Example ¶
http.Handle("/hello", UPS(func(req *testingups.HelloRequest) *testingups.HelloResponse { return &testingups.HelloResponse{Text: "Hello, " + req.Name + "!"} }))
Output:
func UPSWithConfig ¶
UPSWithConfig takes a func and creates an http.Handler using the provided Config.
The func must take take one or two arguments and return one or two values.
The func must return a proto.Message, which will be marshalled into the response, or return a (proto.Message, error). If the error is not nil, the response will be 500 HTTP status unless the error implements StatusCoder, in which case it will provide the HTTP status of the response.
If the func takes one argument, it must be a proto.Message, which will be unmarshalled from the request body.
If the func takes two arguments, the first argument must either be a context.Context or a *http.Request, and the second argument must be a proto.Message.
UPSWithConfig will panic if the argument is not a valid func.
func UPSWithParameter ¶
UPSWithParameter takes a func and creates an http.Handler using the DefaultConfig.
The func must take take two or three arguments and return one or two values.
The func must return a proto.Message, which will be marshalled into the response, or return a (proto.Message, error). If the error is not nil, the response will be 500 HTTP status unless the error implements StatusCoder, in which case it will provide the HTTP status of the response.
If the func takes two arguments, The first argument will be the parameter passed to UPSWithParameter. The second argument must be a proto.Message, which will be unmarshalled from the request body.
If the func takes three arguments, the first argument must either be a context.Context or a *http.Request, the secon argument will be the parameter passed to UPSWithParameter, and the third argument must be a proto.Message.
UPSWithParameter will panic if the argument is not a valid func.
func UPSWithParameterAndConfig ¶
func UPSWithParameterAndConfig(handler interface{}, parameter interface{}, config Config) http.Handler
UPSWithParameterAndConfig takes a func and creates an http.Handler using the provided Config.
The func must take take two or three arguments and return one or two values.
The func must return a proto.Message, which will be marshalled into the response, or return a (proto.Message, error). If the error is not nil, the response will be 500 HTTP status unless the error implements StatusCoder, in which case it will provide the HTTP status of the response.
If the func takes two arguments, The first argument will be the parameter passed to UPSWithParameterAndConfig. The second argument must be a proto.Message, which will be unmarshalled from the request body.
If the func takes three arguments, the first argument must either be a context.Context or a *http.Request, the secon argument will be the parameter passed to UPSWithParameter, and the third argument must be a proto.Message.
UPSWithParameterAndConfig will panic if the argument is not a valid func.
Types ¶
type Config ¶
type Config struct { JSONMarshaler *jsonpb.Marshaler LogError func(context.Context, string, error) LogPanic func(context.Context, interface{}) LogStartRequest func(ctx context.Context, method string, url *url.URL) LogEndRequest func(ctx context.Context, method string, url *url.URL, statusCode int) LogRequestMessage func(context.Context, proto.Message) LogResponseMessage func(context.Context, proto.Message) LogRequestBytes func(context.Context, []byte) LogResponseBytes func(context.Context, []byte) LogRequestJSON func(context.Context, string) LogResponseJSON func(context.Context, string) ErrorResponse func(ctx context.Context, statusCode int) string }
type StatusCoder ¶
type StatusCoder interface {
StatusCode() int
}
StatusCoder can be implemented by the error returned by a handler, in which case it provides the HTTP status code of the response.
Directories ¶
Path | Synopsis |
---|---|
Package testingups is a generated protocol buffer package.
|
Package testingups is a generated protocol buffer package. |