onasty/internal/transport/http/http.go (view raw)
Smirnov Oleksandr
Smirnov Oleksandr
ss2316544@gmail.com feat: impl the core of the app, notes (#5)..., 1 year ago
ss2316544@gmail.com feat: impl the core of the app, notes (#5)..., 1 year ago
| 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 | ) |
| 11 | |
| 12 | type Transport struct { |
| 13 | usersrv usersrv.UserServicer |
| 14 | notesrv notesrv.NoteServicer |
| 15 | } |
| 16 | |
| 17 | func NewTransport( |
| 18 | us usersrv.UserServicer, |
| 19 | ns notesrv.NoteServicer, |
| 20 | ) *Transport { |
| 21 | return &Transport{ |
| 22 | usersrv: us, |
| 23 | notesrv: ns, |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | func (t *Transport) Handler() http.Handler { |
| 28 | r := gin.New() |
| 29 | r.Use( |
| 30 | gin.Recovery(), |
| 31 | t.logger(), |
| 32 | ) |
| 33 | |
| 34 | api := r.Group("/api") |
| 35 | api.GET("/ping", t.pingHandler) |
| 36 | apiv1.NewAPIV1(t.usersrv, t.notesrv).Routes(api.Group("/v1")) |
| 37 | |
| 38 | return r.Handler() |
| 39 | } |
| 40 | |
| 41 | func (*Transport) pingHandler(c *gin.Context) { |
| 42 | c.JSON(http.StatusOK, gin.H{"message": "pong"}) |
| 43 | } |