router

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2024 License: MIT Imports: 17 Imported by: 12

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound      = fmt.Errorf("not found")
	ErrNotAuthorized = fmt.Errorf("not authorized")
	ErrForbidden     = fmt.Errorf("forbidden")
	ErrInternal      = fmt.Errorf("internal server error")
)

Functions

func BadRequest

func BadRequest(req *http.Request, message string) error

func EmptyJSON added in v1.1.0

func EmptyJSON(rw http.ResponseWriter, status int) error

EmptyJSON renders a response with the given status and JSON body `{}`

func Forbidden

func Forbidden(req *http.Request) error

func GetErrorCode added in v1.5.0

func GetErrorCode(err error) int

func Internal

func Internal(req *http.Request) error

func JSON

func JSON(rw http.ResponseWriter, status int, v interface{}) error

JSON renders a response with given status and JSON serialized data

func NewQuietError

func NewQuietError(req *http.Request, code int32, e error) error

NewQuietError logs the error but does not show it to the user

func NewQuietWrap

func NewQuietWrap(req *http.Request, code int32, detail string) error

NewQuietWrap creates a quiet *wrapped* error that does not return a body

func NewRedirectingError

func NewRedirectingError(req *http.Request, e error) error

NewRedirectingError creates an Error with source set to the request url and the redirect flag set to true, which will

func NewRequestError

func NewRequestError(req *http.Request, code int32, detail string) error

NewRequestError creates an Error with source set to the request url

func NotFound

func NotFound(req *http.Request, message string) error

func Proto

func Proto(rw http.ResponseWriter, status int, pb proto.Message) error

Proto renders a response with given status code and JSON-serialized proto

func ProtoOK

func ProtoOK(rw http.ResponseWriter, pb proto.Message) error

ProtoOK renders a 200 response with JSON-serialized proto

Types

type Config

type Config struct {
	Port         int  `json:"port"`
	BindExternal bool `json:"bindext"`
}

Config for the router module

type Error

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

Error wraps an api.Error return object

func (*Error) Error

func (e *Error) Error() string

func (*Error) GetCode

func (e *Error) GetCode() int

func (*Error) GetRequest

func (e *Error) GetRequest() *http.Request

func (*Error) Source added in v1.0.1

func (e *Error) Source() api.Error

type GetCoder added in v1.5.0

type GetCoder interface {
	GetCode() int
}

type Handle

type Handle func(rw http.ResponseWriter, req *http.Request, par httprouter.Params) error

type HandleError

type HandleError func(rw http.ResponseWriter, req *http.Request, err error)

type Module

type Module struct {
	Logger *logger.Module
	Config *config.Module

	Root         *http.ServeMux
	HTTPRouter   *httprouter.Router
	ErrorHandler HandleError
	Middleware   *middleware.MiddlewareServer

	ErrorPage  func(rw http.ResponseWriter, req *http.Request, status int, err error)
	IsAPIRoute func(req *http.Request) bool

	APIPrefixes []string // paths with this prefix get API errors
	// contains filtered or unexported fields
}

Module router implements basic routing with helpers for protobuf-rootd responses.

func (*Module) DELETE

func (m *Module) DELETE(path string, h Handle)

DELETE is a shortcut for m.HTTPRouter.DELETE

func (*Module) Error

func (m *Module) Error(rw http.ResponseWriter, status int, errors ...*api.Error) error

Error writes an an API error to rw

func (*Module) GET

func (m *Module) GET(path string, h Handle)

GET is a shortcut for m.HTTPRouter.GET

func (*Module) Handle

func (m *Module) Handle(method, path string, h http.HandlerFunc)

Handle is a shortcut for m.HTTPRouter.Handle

func (*Module) HandleError

func (m *Module) HandleError(rw http.ResponseWriter, req *http.Request, err error) int

HandleError is the base error handler for the router

func (*Module) Init

func (m *Module) Init(c *service.Config)

Init implements service.Init

func (*Module) PATCH

func (m *Module) PATCH(path string, h Handle)

PATCH is a shortcut for m.HTTPRouter.PATCH

func (*Module) POST

func (m *Module) POST(path string, h Handle)

POST is a shortcut for m.HTTPRouter.POST

func (*Module) PUT

func (m *Module) PUT(path string, h Handle)

PUT is a shortcut for m.HTTPRouter.PUT

func (*Module) Shutdown

func (m *Module) Shutdown(ctx context.Context)

Shutdown the server

func (*Module) Subrouter

func (m *Module) Subrouter(path string) *httprouter.Router

Subrouter creates a new router rooted at path

func (*Module) WrappedHandle

func (m *Module) WrappedHandle(method, path string, h Handle)

WrappedHandle is a shortcut for m.HTTPRouter.Handle

type Params

type Params = httprouter.Params

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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