httprpc

package
v0.0.0-...-2c63f42 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BadRouteError

func BadRouteError(msg string, method, url string) *xerrors.APIError

func Register

func Register(fn RegisterFunc)

func ServeJSON

func ServeJSON(
	ctx context.Context,
	resp http.ResponseWriter,
	req *http.Request,
	hooks Hooks,
	info *HookInfo,
	reqContent Message,
	fn ExecFunc,
)

func ServerHTTPStatusFromErrorCode

func ServerHTTPStatusFromErrorCode(code xerrors.Code) int

func WriteError

func WriteError(ctx context.Context, resp http.ResponseWriter, hooks Hooks, info HookInfo, err error)

Types

type ErrorInterface

type ErrorInterface interface {
	// Code is of the valid error codes.
	Code() xerrors.Code

	// Msg returns a human-readable, unstructured messages describing the error.
	Msg() string

	// WithMeta returns a copy of the Error with the given key-value pair attached
	// as metadata. If the key is already set, it is overwritten.
	WithMeta(key string, val string) ErrorInterface

	// Meta returns the stored value for the given key. If the key has no set
	// value, Meta returns an empty string. There is no way to distinguish between
	// an unset value and an explicit empty string.
	Meta(key string) string

	// MetaMap returns the complete key-value metadata map stored on the error.
	MetaMap() map[string]string

	// Error returns a string of the form "twirp error <Type>: <Msg>"
	Error() string
}

type ExecFunc

type ExecFunc func(context.Context) (ctx context.Context, respContent Message, err error)

type HookInfo

type HookInfo struct {
	Route       string
	HTTPRequest *http.Request
	Request     Message
	Response    Message
	Inner       interface{}
}

type Hooks

type Hooks struct {
	RequestReceived  func(ctx context.Context, info HookInfo) (context.Context, error)
	RequestRouted    func(ctx context.Context, info HookInfo) (context.Context, error)
	ResponsePrepared func(ctx context.Context, info HookInfo, respHeaders http.Header) (context.Context, error)
	ResponseSent     func(ctx context.Context, info HookInfo)
	Error            func(ctx context.Context, info HookInfo, err error) (context.Context, error)
}

func WrapHooks

func WrapHooks(builder HooksBuilder) (res Hooks)

type HooksBuilder

type HooksBuilder interface {
	BuildHooks() Hooks
}

func ChainHooks

func ChainHooks(hooks ...HooksBuilder) HooksBuilder

type HooksFunc

type HooksFunc func() Hooks

func (HooksFunc) BuildHooks

func (h HooksFunc) BuildHooks() Hooks

type Message

type Message interface{}

type Muxer

type Muxer interface {
	Handle(path string, Handler http.Handler)
}

type RegisterFunc

type RegisterFunc func(builder interface{}, hooks ...HooksBuilder) (Server, bool)

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

func (*Registry) NewServer

func (r *Registry) NewServer(builder interface{}, hooks ...HooksBuilder) (Server, error)

func (*Registry) NewServers

func (r *Registry) NewServers(builders ...interface{}) (servers []Server, _ error)

func (*Registry) Register

func (r *Registry) Register(fn RegisterFunc)

type ServeFunc

type ServeFunc func(ctx context.Context, resp http.ResponseWriter, req *http.Request, hooks Hooks, info *HookInfo, reqContent Message, fn ExecFunc)

func ParseRequestHeader

func ParseRequestHeader(req *http.Request) (ServeFunc, error)

type Server

type Server interface {
	http.Handler

	PathPrefix() string

	// WithHooks returns a new server which calls given hooks on processing http
	// requests. It must always clone the server and leave the original one
	// unaffected.
	WithHooks(HooksBuilder) Server
}

func MustNewServer

func MustNewServer(builder interface{}, hooks ...HooksBuilder) Server

func MustNewServers

func MustNewServers(builders ...interface{}) (servers []Server)

func NewServer

func NewServer(builder interface{}, hooks ...HooksBuilder) (Server, error)

func NewServers

func NewServers(builders ...interface{}) (servers []Server, _ error)

func StripPrefix

func StripPrefix(prefix string, servers ...Server) Server

func WithHooks

func WithHooks(servers []Server, hooks ...HooksBuilder) []Server

func WithPrefix

func WithPrefix(prefix string, servers []Server) []Server

type TwError

type TwError interface {
	ErrorInterface
	Cause() error
	OrigFile() string
	OrigLine() int
}

func TwirpError

func TwirpError(err error) TwError

Jump to

Keyboard shortcuts

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