ups

package module
v0.0.0-...-6762395 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2017 License: LGPL-3.0 Imports: 11 Imported by: 0

README

ups is a Go package for implementing http microservices using Protocol Buffers.

GoDoc Build Status

Protocol Buffers: https://github.com/golang/protobuf

JSON is also supported using https://github.com/golang/protobuf/jsonpb

Example

syntax = "proto3";

message HelloRequest {
    string name = 1;
}

message HelloResponse {
    string text = 1;
}
	http.Handle("/hello", ups.UPS(func(req *HelloRequest) *HelloResponse {
		return &HelloResponse{Text: "Hello, " + req.Name + "!"}
	}))

Documentation

Overview

Package ups supports implementing http microservices using Protocol Buffers.

Index

Examples

Constants

This section is empty.

Variables

View Source
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

func UPS(handler interface{}) http.Handler

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

func UPSWithConfig(handler interface{}, config Config) http.Handler

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

func UPSWithParameter(handler interface{}, parameter interface{}) http.Handler

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.

Jump to

Keyboard shortcuts

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