server

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2023 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddRoute

func AddRoute[InT, OutT, AppCtxT any](
	s *Server[AppCtxT], route *Route[InT, OutT, AppCtxT],
)

func IsVoid

func IsVoid[T any]() bool

Types

type AppCtxInit

type AppCtxInit[AppCtxT any] func(
	ctx ServerCtx[AppCtxT], r *http.Request,
) AppCtxT

AppCtxInit is the signature for the function which initializes the application provided AppCtxT for every request.

type CORSHeaderMW

type CORSHeaderMW[AppCtxT any] struct {
	AllowedOrigins []string
}

CORSHeaderMW adds the headers required to bypass browsers' CORS policy.

func (CORSHeaderMW[AppCtxT]) Exec

func (mw CORSHeaderMW[AppCtxT]) Exec(
	ctx *ServerCtx[AppCtxT],
	w http.ResponseWriter, r *http.Request,
) *Error

type Config

type Config struct {
	Host string `env:"SVC_HOST"`
	Port int    `env:"SVC_PORT"`
}

type ErrCode

type ErrCode = string
const (
	MethodPathInvalidErr ErrCode = "method_path_error"
	EncoderErr           ErrCode = "encoder_error"
	DecoderErr           ErrCode = "decoder_error"
	UnknownErr           ErrCode = "unknown_error"
	ApplicationErr       ErrCode = "application_error"
)

type Error

type Error struct {
	Status  int     `json:"status_code"`
	Code    ErrCode `json:"app_err_code"`
	Msg     string  `json:"message,omitempty"`
	Details string  `json:"details,omitempty"`
}

A more verbose way to return an error.

func (Error) Error

func (e Error) Error() string

type Middleware

type Middleware[AppCtxT any] interface {
	Exec(sctx *ServerCtx[AppCtxT], w http.ResponseWriter, r *http.Request) *Error
}

type RequestLoggerMW

type RequestLoggerMW[AppCtxT any] struct{}

RequestLoggerMW logs some information about a request (such as sender IP) when it is accepted by the server.

func (RequestLoggerMW[AppCtxT]) Exec

func (mw RequestLoggerMW[AppCtxT]) Exec(
	ctx *ServerCtx[AppCtxT],
	w http.ResponseWriter, r *http.Request,
) *Error

type Route

type Route[InT, OutT, AppCtxT any] struct {
	// should be one of http.Method{Get,Head,Post,Put,Patch} etc.
	Method  string
	Pattern string
	Handler RouteHandler[InT, OutT, AppCtxT]
	Codec   codec.Codec
	MwStack []Middleware[AppCtxT]
}

type RouteHandler

type RouteHandler[InT, OutT, AppCtxT any] func(
	ctx *ServerCtx[AppCtxT], req *InT,
) (resp *OutT, err error)

RouteHandler is the signature for routes registered with the server.

type Server

type Server[AppCtxT any] struct {
	// contains filtered or unexported fields
}

func NewServer

func NewServer[AppCtxT any](
	c Config, aci AppCtxInit[AppCtxT],
) (*Server[AppCtxT], error)

func (*Server[AppCtxT]) Run

func (s *Server[AppCtxT]) Run()

type ServerCtx

type ServerCtx[AppCtxT any] struct {
	context.Ctx[AppCtxT]

	Pattern, Path, Method string
	PathVars              map[string]string

	Response ServerResponse
}

ServerCtx a context which is threaded through all route handlers by a Server, and is intended to contain information that the handlers might need about the request they are executing.

type ServerResponse added in v0.4.0

type ServerResponse struct {
	Status      int // http.Status*
	ContentType string
}

type Void

type Void struct{}

Jump to

Keyboard shortcuts

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