onasty/internal/transport/http/http.go (view raw)
| 1 | package http |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | |
| 6 | "github.com/gin-gonic/gin" |
| 7 | "github.com/olexsmir/onasty/internal/service/notesrv" |
| 8 | "github.com/olexsmir/onasty/internal/service/usersrv" |
| 9 | "github.com/olexsmir/onasty/internal/transport/http/apiv1" |
| 10 | "github.com/olexsmir/onasty/internal/transport/http/ratelimit" |
| 11 | "github.com/olexsmir/onasty/internal/transport/http/reqid" |
| 12 | ) |
| 13 | |
| 14 | type Transport struct { |
| 15 | usersrv usersrv.UserServicer |
| 16 | notesrv notesrv.NoteServicer |
| 17 | |
| 18 | ratelimitCfg ratelimit.Config |
| 19 | } |
| 20 | |
| 21 | func NewTransport( |
| 22 | us usersrv.UserServicer, |
| 23 | ns notesrv.NoteServicer, |
| 24 | ratelimitCfg ratelimit.Config, |
| 25 | ) *Transport { |
| 26 | return &Transport{ |
| 27 | usersrv: us, |
| 28 | notesrv: ns, |
| 29 | ratelimitCfg: ratelimitCfg, |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | func (t *Transport) Handler() http.Handler { |
| 34 | r := gin.New() |
| 35 | r.Use( |
| 36 | gin.Recovery(), |
| 37 | reqid.Middleware(), |
| 38 | t.logger(), |
| 39 | ratelimit.MiddlewareWithConfig(t.ratelimitCfg), |
| 40 | ) |
| 41 | |
| 42 | api := r.Group("/api") |
| 43 | api.GET("/ping", t.pingHandler) |
| 44 | apiv1.NewAPIV1(t.usersrv, t.notesrv).Routes(api.Group("/v1")) |
| 45 | |
| 46 | return r.Handler() |
| 47 | } |
| 48 | |
| 49 | func (*Transport) pingHandler(c *gin.Context) { |
| 50 | c.JSON(http.StatusOK, gin.H{"message": "pong"}) |
| 51 | } |