rpc

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

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

Go to latest
Published: Apr 10, 2024 License: MIT Imports: 9 Imported by: 0

README

rpc

RPC is a small remote procedure call library with a focus on building REST-ish APIs that can be natively called from the browser.

Why build yet another RPC library?

  1. GRPC support for the web browser is dismal

  2. GRPC tooling is not fun and not fast

  3. GRPC can't run natively in a servless context

  4. The Go RPC library focuses on Go to Go services

What is this library trying to do?

  1. Be grok-able

  2. Be orthogonal

  3. Get out of its own way

  4. Runnable in a servless context

Non-goals

  • Be a cross language, general purpose RPC framework.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var InternalServerError = RpcError{HTTPCode: 500, Message: "Internal Server Error"}

Functions

func GetHandlerNameFromContext

func GetHandlerNameFromContext(ctx context.Context) string

func HandleError

func HandleError(w http.ResponseWriter, err error)

func Register

func Register[Input any, Output any](mux *http.ServeMux, httpMethod HTTPMethod, path string, h RpcHandler[Input, Output], rpcMiddleware ...Middleware)

Register registers a new RPC handler to the given mux. handlers are applied in the order they are registered. Handlers regiestered with RegisterMiddleware are applied first.

func RegisterDocsEndpoint

func RegisterDocsEndpoint(mux *http.ServeMux)

func RegisterMiddleware

func RegisterMiddleware(middlewware ...Middleware)

RegisterMiddleware registers a middleware that is used for all RPCs that are registed. Use the handlers argument in Register to apply RPC specific middleware in addition to the default middleware.

Handlers are applied in the order they are registered.

func SetHandlerNameInContext

func SetHandlerNameInContext(ctx context.Context, handlerName string) context.Context

Types

type HTTPMethod

type HTTPMethod string

type Middleware

type Middleware func(http.Handler) http.Handler

type RpcEndpoint

type RpcEndpoint struct {
	Path       string      `json:"path"`
	HTTPMethod HTTPMethod  `json:"httpMethod"`
	InputType  string      `json:"inputType"`
	Input      interface{} `json:"input"`
	OutputType string      `json:"outputType"`
	Output     interface{} `json:"output"`
}

Docs

type RpcError

type RpcError struct {
	HTTPCode int
	Message  string
}

func NewBadRequestError

func NewBadRequestError(message string) RpcError

func (RpcError) Error

func (e RpcError) Error() string

func (RpcError) IsClientError

func (e RpcError) IsClientError() bool

func (RpcError) IsServerError

func (e RpcError) IsServerError() bool

type RpcHandler

type RpcHandler[Input any, Output any] interface {
	Input() Input
	Output() Output
	Handle(ctx context.Context, input Input) (Output, error)
}

Jump to

Keyboard shortcuts

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