Documentation ¶
Overview ¶
Package swiffy exposes gRPC style handler function as HTTP handler that can directly handles HTTP requests from Web apps using plain JSON.
gRPC style handler is like f(context, *requestProto) (*responseProto, error) From Web app, it can be called as a POST request like /api/foo?method=Bar&format=json and the body is simply JSON that can be handled by github.com/golang/protobuf/jsonpb For such request, the response will be Status 200 and the plain JSON object as result, or any HTTP status code for error conditions.
Index ¶
- func Error(status int, text string, message interface{}) error
- func NewServiceHandler(serv interface{}, opt *Options) http.Handler
- func ProtoDecoder(dst interface{}, src []byte, format string) error
- func ProtoEncoder(w http.ResponseWriter, status int, src interface{}, format string) error
- type Handler
- type Middleware
- type Options
- type RequestDecoder
- type ResponseEncoder
- type WithHTTPStatus
- type WithMessage
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Error ¶
Error returns an error with corresponding HTTP status code, when text emtpy, the default HTTP status text will be used.
func NewServiceHandler ¶
NewServiceHandler creates an http.Handler that serves all public method of serv. These public methods must conforms to Handler, but their req and res can be any types that implements proto.Message, NewServiceHandler handles them using reflect.
Note that RegisterService exports all public method of serv, it would generally be safer to pass in an interface instead of struct, to avoid unintentially exports methods that's not intended to serve externally.
func ProtoDecoder ¶
ProtoDecoder implements RequestDecoder for protobuf.
func ProtoEncoder ¶
func ProtoEncoder(w http.ResponseWriter, status int, src interface{}, format string) error
ProtoEncoder implements ResponseEncoder for protobuf.
Types ¶
type Handler ¶
Handler describes generalize form of gRPC style functions swiffy can serve. The actual handler provided to NewServiceHandler can use any types that conforms to encoder/decoder
type Middleware ¶
Middleware wraps a handler and do its processing before or after calling underliring handler.
type Options ¶
type Options struct { RequestDecoder RequestDecoder ResponseEncoder ResponseEncoder Middleware Middleware }
Options contains options like encoder/decoder.
type RequestDecoder ¶
RequestDecoder decodes src into dst.
type ResponseEncoder ¶
type ResponseEncoder func(w http.ResponseWriter, status int, src interface{}, format string) error
ResponseEncoder writes encoded result of src to w.
type WithHTTPStatus ¶
type WithHTTPStatus interface {
HTTPStatus() int
}
WithHTTPStatus interface can report an HTTP StatusCode the object associated with. Often used with an error that needed to be coresponded to a certain HTTP Code
type WithMessage ¶
type WithMessage interface {
Message() interface{}
}
WithMessage interface can report structured data for error. If error returned from handler implements this interface, we return encoded result of Message() instead of plain text by String()