transport

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

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

Go to latest
Published: Aug 10, 2022 License: MIT Imports: 6 Imported by: 0

README

goRPC Transport

Package to serve goRPC as JSON over HTTP.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(r *http.Request, v interface{}) error

func Encode

func Encode(w http.ResponseWriter, r *http.Request, status int, payload interface{}) error

Types

type ClientError

type ClientError struct {
	Code    int    `json:"-"`
	Message string `json:"error"`
}

func (ClientError) Error

func (e ClientError) Error() string

type ErrorHandler

type ErrorHandler func(w http.ResponseWriter, r *http.Request, err error)
var DefaultErrorHandler ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
	clientErr, ok := err.(ClientError)
	if ok {
		_ = Encode(w, r, clientErr.Code, clientErr)
		return
	}

	_ = Encode(w, r, http.StatusInternalServerError, map[string]string{"message": "internal server error"})
}

type Middleware

type Middleware func(http.Handler) http.Handler

type Option

type Option func(*server)

func WithErrorHandler

func WithErrorHandler(handler ErrorHandler) Option

func WithMiddleware

func WithMiddleware(mw Middleware) Option

func WithPathPrefix

func WithPathPrefix(prefix string) Option

type Server

type Server interface {
	http.Handler

	OnErr(w http.ResponseWriter, r *http.Request, err error)
	Register(service, method string, h http.HandlerFunc)
}

func NewServer

func NewServer(options ...Option) Server

Jump to

Keyboard shortcuts

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