router

package
v1.10.0 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2025 License: MIT Imports: 17 Imported by: 15

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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 JSON

func JSON(rw http.ResponseWriter, status int, v any) error

JSON renders a response with given status and JSON serialized data

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

func SetMarshalOptions added in v1.10.0

func SetMarshalOptions(opt *protojson.MarshalOptions)

Types

type Config

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

Config for the router module

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) 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. 1. If err is a httperror.HTTPErrorCode, only the error status code is returned, without a body 2. If the route is not an API route, m.ErrorPage is called to show an error page 3. If err is a httperror.HTTPError, its ToProto function will be called for the return JSON 4. Otherwise, we will return a JSON response without any detail (probably a 500 unless err implements GetCode) * If the final status code is 500, we will report the original err with m.Logger.ErrorCtx

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