-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.go
47 lines (37 loc) · 963 Bytes
/
controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package gin
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/yolkhovyy/go-userv/internal/contract/domain"
)
type Controller struct {
domain domain.Contract
handler *gin.Engine
}
func New(config Config, domain domain.Contract, handlers ...gin.HandlerFunc) *Controller {
controller := Controller{
domain: domain,
}
// Gin routing engine.
gin.SetMode(config.Mode)
engine := gin.New()
engine.RedirectTrailingSlash = false
engine.Use(gin.Recovery(), Logger())
engine.Use(handlers...)
// Health check.
engine.GET("/health", controller.health)
// API endpoints.
group := engine.Group("/api/v1")
{
group.POST("/user", controller.create)
group.GET("/user/:id", controller.get)
group.GET("/users", controller.list)
group.PUT("/user/:id", controller.update)
group.DELETE("/user/:id", controller.delete)
}
controller.handler = engine
return &controller
}
func (c *Controller) Handler() http.Handler {
return c.handler
}