HTTP Controller
EinarCLI allows you to generate custom controllers with the necessary files and imports to handle your http requests.
π¨βπ» Generate a New Controller
Inside your project directory, run the following command to create a new controller:
einar generate post-controller post-customerHereβs an example of how the generated code will look:
func init() { ioc.Registry(postCustomer, httpserver.New)}func postCustomer(s httpserver.Server) { fuego.Post(s.Manager, "/insert-your-custom-pattern-here", func(c *fuego.ContextNoBody) (any, error) {
return "unimplemented", nil }, option.Summary("postCustomer"))}s httpserver.Server is a wrapper for the official fuego package. After generating the controller, you must manually replace the placeholder /insert-your-custom-pattern-here with the actual path you intend to use in your application. The file post_customer.go will be created in the following directory structure:
/app /adapter /in /api - post_customer.goπ Check The Controller Response
Execute the following request to retrieve the default response of your generated controller:
curl -X POST http://localhost:8080/insert-your-custom-pattern-hereInvoke-WebRequest -Uri http://localhost:8080/insert-your-custom-pattern-here -Method POSTResponseBody:
{ "message": "Unimplemented"}einar generate get-controller get-customerHereβs an example of how the generated code will look:
func init() { ioc.Registry(getCustomer, httpserver.New)}func getCustomer(s httpserver.Server) { fuego.Get(s.Manager, "/insert-your-custom-pattern-here", func(c *fuego.ContextNoBody) (any, error) {
return "unimplemented", nil }, option.Summary("getCustomer"))}s httpserver.Server is a wrapper for the official fuego package. After generating the controller, you must manually replace the placeholder /insert-your-custom-pattern-here with the actual path you intend to use in your application. The file get_customer.go will be created in the following directory structure:
/app /adapter /in /api - get_customer.goπ Check The Controller Response
Execute the following request to retrieve the default response:
curl -X GET http://localhost:8080/insert-your-custom-pattern-hereInvoke-WebRequest -Uri http://localhost:8080/insert-your-custom-pattern-here -Method GETResponseBody:
{ "message": "Unimplemented"}einar generate put-controller put-customerHereβs an example of how the generated code will look:
func init() { ioc.Registry(putCustomer, httpserver.New)}func putCustomer(s httpserver.Server) { fuego.Put(s.Manager, "/insert-your-custom-pattern-here", func(c *fuego.ContextNoBody) (any, error) {
return "unimplemented", nil }, option.Summary("putCustomer"))}s httpserver.Server is a wrapper for the official fuego package. After generating the controller, you must manually replace the placeholder /insert-your-custom-pattern-here with the actual path you intend to use in your application. The file put_customer.go will be created in the following directory structure:
/app /adapter /in /api - put_customer.goπ Check The Controller Response
Execute the following requests to retrieve the default response:
curl -X PUT http://localhost:8080/insert-your-custom-pattern-hereInvoke-WebRequest -Uri http://localhost:8080/insert-your-custom-pattern-here -Method PUTResponseBody:
{ "message": "Unimplemented"}einar generate delete-controller delete-customerHereβs an example of how the generated code will look:
func init() { ioc.Registry(deleteCustomer, httpserver.New)}func deleteCustomer(s httpserver.Server) { fuego.Delete(s.Manager, "/insert-your-custom-pattern-here", func(c *fuego.ContextNoBody) (any, error) {
return "unimplemented", nil }, option.Summary("deleteCustomer"))}s httpserver.Server is a wrapper for the official fuego package. After generating the controller, you must manually replace the placeholder /insert-your-custom-pattern-here with the actual path you intend to use in your application. The file delete_customer.go will be created in the following directory structure:
/app /adapter /in /api - delete_customer.goπ Check The Controller Response
Execute the following requests to retrieve the default response:
curl -X DELETE http://localhost:8080/insert-your-custom-pattern-hereInvoke-WebRequest -Uri http://localhost:8080/insert-your-custom-pattern-here -Method DELETEResponseBody:
{ "message": "Unimplemented"}einar generate patch-controller patch-customerHereβs an example of how the generated code will look:
func init() { ioc.Registry(patchCustomer, httpserver.New)}func patchCustomer(s httpserver.Server) { fuego.Patch(s.Manager, "/insert-your-custom-pattern-here", func(c *fuego.ContextNoBody) (any, error) {
return "unimplemented", nil }, option.Summary("patchCustomer"))}s httpserver.Server is a wrapper for the official fuego package. After generating the controller, you must manually replace the placeholder /insert-your-custom-pattern-here with the actual path you intend to use in your application. The file patch_customer.go will be created in the following directory structure:
/app /adapter /in /api - patch_customer.goπ Check The Controller Response
Execute the following requests to retrieve the default response:
curl -X PATCH http://localhost:8080/insert-your-custom-pattern-hereInvoke-WebRequest -Uri http://localhost:8080/insert-your-custom-pattern-here -Method PATCHResponseBody:
{ "message": "Unimplemented"}Bind & Validate Data
By default, Fuego includes go-playground/validator framework for validation, as shown in the example below.
type Customer struct { Name string `json:"name" validate:"required"` Email string `json:"email" validate:"required,email"`}
func init() { ioc.Registry(postCustomer, httpserver.New)}func postCustomer(s httpserver.Server) { fuego.Post(s.Manager, "/api/customer", func(c *fuego.ContextWithBody[Customer]) (any, error) { _, err := c.Body() if err != nil { return nil, err } return "unimplemented", nil }, option.Summary("postCustomer"))}CURL
curl --location 'http://localhost:8080/api/customer' \--header 'Content-Type: application/json' \--data '{ "name":"", "email":""}'ResponseBody
{ "title": "Validation Error", "status": 400, "detail": "Name is required, Email is required", "errors": [ { "name": "Customer.Name", "reason": "Key: 'Customer.Name' Error:Field validation for 'Name' failed on the 'required' tag", "more": { "field": "Name", "nsField": "Customer.Name", "param": "", "tag": "required", "value": "" } }, { "name": "Customer.Email", "reason": "Key: 'Customer.Email' Error:Field validation for 'Email' failed on the 'required' tag", "more": { "field": "Email", "nsField": "Customer.Email", "param": "", "tag": "required", "value": "" } } ]}einar generate post-controller post-customerHereβs an example of how the generated code will look:
func init() { ioc.Registry(postCustomer, serverwrapper.NewEchoWrapper)}func postCustomer(e serverwrapper.EchoWrapper) { e.POST("/insert-your-custom-pattern-here", func(c echo.Context) error { return c.JSON(http.StatusOK, map[string]string{ "message": "Unimplemented", }) })}e serverwrapper.EchoWrapper is a wrapper for the official echo package. After generating the controller, you must manually replace the placeholder /insert-your-custom-pattern-here with the actual path you intend to use in your application. The file post_customer.go will be created in the following directory structure:
/app /adapter /in /api - post_customer.goπ Check The Controller Response
Execute the following request to retrieve the default response of your generated controller:
curl -X POST http://localhost:8080/insert-your-custom-pattern-hereInvoke-WebRequest -Uri http://localhost:8080/insert-your-custom-pattern-here -Method POSTResponseBody:
{ "message": "Unimplemented"}einar generate get-controller get-customerHereβs an example of how the generated code will look:
func init() { ioc.Registry(getCustomer, serverwrapper.NewEchoWrapper)}func getCustomer(e serverwrapper.EchoWrapper) { e.GET("/insert-your-custom-pattern-here", func(c echo.Context) error { return c.JSON(http.StatusOK, map[string]string{ "message": "Unimplemented", }) })}e serverwrapper.EchoWrapper is a wrapper for the official echo package. After generating the controller, you must manually replace the placeholder /insert-your-custom-pattern-here with the actual path you intend to use in your application. The file get_customer.go will be created in the following directory structure:
/app /adapter /in /api - get_customer.goπ Check The Controller Response
Execute the following request to retrieve the default response:
curl -X GET http://localhost:8080/insert-your-custom-pattern-hereInvoke-WebRequest -Uri http://localhost:8080/insert-your-custom-pattern-here -Method GETResponseBody:
{ "message": "Unimplemented"}einar generate put-controller put-customerHereβs an example of how the generated code will look:
func init() { ioc.Registry(putCustomer, serverwrapper.NewEchoWrapper)}func putCustomer(e serverwrapper.EchoWrapper) { e.PUT("/insert-your-custom-pattern-here", func(c echo.Context) error { return c.JSON(http.StatusOK, map[string]string{ "message": "Unimplemented", }) })}e serverwrapper.EchoWrapper is a wrapper for the official echo package. After generating the controller, you must manually replace the placeholder /insert-your-custom-pattern-here with the actual path you intend to use in your application. The file put_customer.go will be created in the following directory structure:
/app /adapter /in /api - put_customer.goπ Check The Controller Response
Execute the following requests to retrieve the default response:
curl -X PUT http://localhost:8080/insert-your-custom-pattern-hereInvoke-WebRequest -Uri http://localhost:8080/insert-your-custom-pattern-here -Method PUTResponseBody:
{ "message": "Unimplemented"}einar generate delete-controller delete-customerHereβs an example of how the generated code will look:
func init() { ioc.Registry(deleteCustomer, serverwrapper.NewEchoWrapper)}func deleteCustomer(e serverwrapper.EchoWrapper) { e.DELETE("/insert-your-custom-pattern-here", func(c echo.Context) error { return c.JSON(http.StatusOK, map[string]string{ "message": "Unimplemented", }) })}e serverwrapper.EchoWrapper is a wrapper for the official echo package. After generating the controller, you must manually replace the placeholder /insert-your-custom-pattern-here with the actual path you intend to use in your application. The file delete_customer.go will be created in the following directory structure:
/app /adapter /in /api - delete_customer.goπ Check The Controller Response
Execute the following requests to retrieve the default response:
curl -X DELETE http://localhost:8080/insert-your-custom-pattern-hereInvoke-WebRequest -Uri http://localhost:8080/insert-your-custom-pattern-here -Method DELETEResponseBody:
{ "message": "Unimplemented"}einar generate patch-controller patch-customerHereβs an example of how the generated code will look:
func init() { ioc.Registry(patchCustomer, serverwrapper.NewEchoWrapper)}func patchCustomer(e serverwrapper.EchoWrapper) { e.PATCH("/insert-your-custom-pattern-here", func(c echo.Context) error { return c.JSON(http.StatusOK, map[string]string{ "message": "Unimplemented", }) })}e serverwrapper.EchoWrapper is a wrapper for the official echo package. After generating the controller, you must manually replace the placeholder /insert-your-custom-pattern-here with the actual path you intend to use in your application. The file patch_customer.go will be created in the following directory structure:
/app /adapter /in /api - patch_customer.goπ Check The Controller Response
Execute the following requests to retrieve the default response:
curl -X PATCH http://localhost:8080/insert-your-custom-pattern-hereInvoke-WebRequest -Uri http://localhost:8080/insert-your-custom-pattern-here -Method PATCHResponseBody:
{ "message": "Unimplemented"}Bind & Validate Data
By default, Einar includes inside Echo the go-playground/validator framework for validation, as shown in the example below.
type Customer struct { Name string `json:"name" validate:"required"` Email string `json:"email" validate:"required,email"`}
func init() { ioc.Registry(postCustomer, serverwrapper.NewEchoWrapper)}func postCustomer(e serverwrapper.EchoWrapper) { e.POST("/api/customer", func(c echo.Context) error { var customer Customer if err := c.Bind(&customer); err != nil { return echo.NewHTTPError(http.StatusBadRequest, err.Error()) } if err := c.Validate(customer); err != nil { return echo.NewHTTPError(http.StatusBadRequest, map[string]string{ "message": err.Error(), }) } return c.JSON(http.StatusOK, map[string]string{ "message": "Unimplemented", }) })}CURL
curl --location 'http://localhost:8080/api/customer' \--header 'Content-Type: application/json' \--data '{ "name":"", "email":""}'ResponseBody
{ "message": "Key: 'Customer.Name' Error:Field validation for 'Name' failed on the 'required' tag\nKey: 'Customer.Email' Error:Field validation for 'Email' failed on the 'required' tag"}