You've already forked Go-Template
Please no changes this time.
This commit is contained in:
13
http_handlers/errors/badrequest.go
Normal file
13
http_handlers/errors/badrequest.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package errors
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo/v4"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func NewBadRequestError(c echo.Context, fields map[string]string) error {
|
||||
return c.JSON(http.StatusBadRequest, map[string]interface{}{
|
||||
"error": "Bad Request",
|
||||
"fields": fields,
|
||||
})
|
||||
}
|
||||
10
http_handlers/errors/notfound.go
Normal file
10
http_handlers/errors/notfound.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package errors
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo/v4"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func NewNotFoundError(c echo.Context) error {
|
||||
return c.JSON(http.StatusNotFound, map[string]string{"error": "Not Found"})
|
||||
}
|
||||
11
http_handlers/handler.go
Normal file
11
http_handlers/handler.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package http_handlers
|
||||
|
||||
import "github.com/labstack/echo/v4"
|
||||
|
||||
type Handler interface {
|
||||
Get(c echo.Context) error
|
||||
Post(c echo.Context) error
|
||||
Put(c echo.Context) error
|
||||
Delete(c echo.Context) error
|
||||
Patch(c echo.Context) error
|
||||
}
|
||||
40
http_handlers/index/index.go
Normal file
40
http_handlers/index/index.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package index
|
||||
|
||||
import (
|
||||
"gitea.siteworxpro.com/Siteworxpro/Go-Template/http_handlers/errors"
|
||||
"gitea.siteworxpro.com/Siteworxpro/Go-Template/logger"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func Register(g *echo.Group) {
|
||||
index := new(index)
|
||||
|
||||
g.GET("", index.Get)
|
||||
g.POST("", index.Post)
|
||||
g.PUT("", index.Put)
|
||||
g.DELETE("", index.Delete)
|
||||
g.PATCH("", index.Patch)
|
||||
}
|
||||
|
||||
type index struct{}
|
||||
|
||||
func (i *index) Get(c echo.Context) error {
|
||||
c.Get("logger").(logger.Interface).Info("Index handler called")
|
||||
return c.JSON(200, map[string]string{"message": "Hello, World!"})
|
||||
}
|
||||
|
||||
func (i *index) Post(c echo.Context) error {
|
||||
return errors.NewBadRequestError(c, map[string]string{"test": "Test is a required field"})
|
||||
}
|
||||
|
||||
func (i *index) Put(c echo.Context) error {
|
||||
return c.JSON(200, map[string]string{"message": "Hello, World!"})
|
||||
}
|
||||
|
||||
func (i *index) Delete(c echo.Context) error {
|
||||
return c.JSON(200, map[string]string{"message": "Hello, World!"})
|
||||
}
|
||||
|
||||
func (i *index) Patch(c echo.Context) error {
|
||||
return errors.NewNotFoundError(c)
|
||||
}
|
||||
Reference in New Issue
Block a user