-
Notifications
You must be signed in to change notification settings - Fork 5
HTTP
Zord can has many entry points, but our main entry point is http. in http entrypoint we have some features that can be used to develop a http based system they are:
- filters
- routes map
- middlewares
to use filters on zord you need to declare a new filters struct and parse it in request constructor in the desired handler, after that you need to map the allowed filters and data to be filtered on request Validate function.
Parse Example on validate function:
r.Filters.Parse(
map[string]string{
"client_id": "eql,lik",
},
map[string]filters.FilterData{
"client_id": {
Value: r.Data.ClientID,
IsString: true,
},
},
)
Getting query params and parsing to request data on handler example:
func (hs *DummyHandlers) HandleListDummy(context echo.Context) error {
s := dummyList.NewService(
hs.logger,
hs.DummyRepository,
pagination.NewPaginationProvider[dummy.Dummy](hs.DummyRepository),
)
data := new(dummyList.Data)
bindErr := echo.QueryParamsBinder(context).
String("client_id", &data.ClientID).
BindErrors()
if bindErr != nil {
s.CustomError(http.StatusBadRequest, bindErr)
return context.JSON(http.StatusBadRequest, s.Error)
}
s.Execute(
dummyList.NewRequest(data),
)
response, err := s.GetResponse()
if err != nil {
return context.JSON(err.Status, err)
}
return context.JSON(http.StatusOK, response)
}
All possible filters types is mapped in internal/application/providers/filters
The routes map is declared on cmd/http/routes/declarable, it receives a Declarable struct with echo.Group server declaration Example:
type DummyRoutes struct {
hand *handlers.DummyHandlers
}
func NewDummyRoutes(reg *registry.Registry) *DummyRoutes {
hand := handlers.NewDummyHandlers(reg)
return &DummyRoutes{
hand: hand,
}
}
func (hs *DummyRoutes) DeclarePublicRoutes(server *echo.Group, apiPrefix string) {}
func (hs *DummyRoutes) DeclarePrivateRoutes(server *echo.Group, apiPrefix string) {
server.GET(apiPrefix+"/dummy", hs.hand.HandleListDummy)
server.GET(apiPrefix+"/dummy/:id", hs.hand.HandleGetDummy)
server.POST(apiPrefix+"/dummy", hs.hand.HandleCreateDummy)
server.PUT(apiPrefix+"/dummy/:id", hs.hand.HandleEditDummy)
server.DELETE(apiPrefix+"/dummy/:id", hs.hand.HandleDeleteDummy)
}
OBs: we dont have a default authorization implemetation but we have a route split for private and public routes, if you need private routes just registry a middleware in echo pattern on private group (cmd/http/server/server.go:39)
We dont have middlewares examples but you can use or create any middleware of echo golang lib: https://echo.labstack.com/docs/category/middleware
if you need create a middleware we have the middleware folder cmd/http/middlewares