You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This can be done by embedding a httprouter.Router
(a light weight HTTP router that supports variables in the routing pattern and matches against the request method)
Required services should be exported on the struct
// ThingHandler represents an HTTP API handler for things.typeThingHandlerstruct {
// embedded httprouter.Router as a lazy way to implement http.Handler*httprouter.RouterThingService platform.ThingServiceAuthorizationService platform.AuthorizationServiceLogger*zap.Logger
}
HTTP Handler Constructor
Routes should be declared in the constructor
// NewThingHandler returns a new instance of ThingHandler.funcNewThingHandler() *ThingHandler {
h:=&ThingHandler{
Router: httprouter.New(),
Logger: zap.Nop(),
}
h.HandlerFunc("POST", "/api/v2/things", h.handlePostThing)
h.HandlerFunc("GET", "/api/v2/things", h.handleGetThings)
returnh
}
Route handlers (http.HandlerFuncs)
Each route handler should have an associated request struct and decode function
The decode function should take a context.Context and an *http.Request and return the associated route request struct