Documentation ¶
Index ¶
- func EmptyJSON(rw http.ResponseWriter, status int) error
- func JSON(rw http.ResponseWriter, status int, v any) error
- func Proto(rw http.ResponseWriter, status int, pb proto.Message) error
- func ProtoOK(rw http.ResponseWriter, pb proto.Message) error
- func SetMarshalOptions(opt *protojson.MarshalOptions)
- type Config
- type Handle
- type HandleError
- type Module
- func (m *Module) DELETE(path string, h Handle)
- func (m *Module) GET(path string, h Handle)
- func (m *Module) Handle(method, path string, h http.HandlerFunc)
- func (m *Module) HandleError(rw http.ResponseWriter, req *http.Request, err error) int
- func (m *Module) Init(c *service.Config)
- func (m *Module) PATCH(path string, h Handle)
- func (m *Module) POST(path string, h Handle)
- func (m *Module) PUT(path string, h Handle)
- func (m *Module) Shutdown(ctx context.Context)
- func (m *Module) Subrouter(path string) *httprouter.Router
- func (m *Module) WrappedHandle(method, path string, h Handle)
- type Params
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 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 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) Handle ¶
func (m *Module) Handle(method, path string, h http.HandlerFunc)
Handle is a shortcut for m.HTTPRouter.Handle
func (*Module) HandleError ¶
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) Subrouter ¶
func (m *Module) Subrouter(path string) *httprouter.Router
Subrouter creates a new router rooted at path
func (*Module) WrappedHandle ¶
WrappedHandle is a shortcut for m.HTTPRouter.Handle
type Params ¶
type Params = httprouter.Params