swiffy

package module
v0.0.0-...-6b182af Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2018 License: Apache-2.0 Imports: 8 Imported by: 1

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Error

func Error(status int, text string, message interface{}) error

Error returns an error with corresponding HTTP status code, when text emtpy, the default HTTP status text will be used.

func NewServiceHandler

func NewServiceHandler(serv interface{}, opt *Options) http.Handler

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

func ProtoDecoder(dst interface{}, src []byte, format string) error

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

type Handler func(ctx context.Context, req interface{}) (res interface{}, err error)

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

type Middleware func(handler Handler) Handler

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

type RequestDecoder func(dst interface{}, src []byte, format string) error

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()

Directories

Path Synopsis
examples module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL