/
httpserver.go
65 lines (55 loc) · 1.31 KB
/
httpserver.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package http
import (
"context"
"github.com/dalmarcogd/bpl-go/internal/services"
"github.com/labstack/echo/v4"
"io/ioutil"
)
type (
ServiceImpl struct {
serviceManager services.Sis
ctx context.Context
echo *echo.Echo
address string
}
)
func New() *ServiceImpl {
return &ServiceImpl{}
}
func (s *ServiceImpl) WithAddress(address string) *ServiceImpl {
s.address = address
return s
}
func (s *ServiceImpl) Init(ctx context.Context) error {
s.ctx = ctx
s.echo = echo.New()
s.echo.Logger.SetOutput(ioutil.Discard)
s.echo.Use(LogMiddleware(s.Sis().Logger()))
s.RegisterRoutes()
return nil
}
func (s *ServiceImpl) Close() error {
if err := s.echo.Shutdown(s.ctx); err != nil {
return err
}
return s.echo.Close()
}
func (s *ServiceImpl) WithSis(c services.Sis) services.HttpServer {
s.serviceManager = c
return s
}
func (s *ServiceImpl) Sis() services.Sis {
return s.serviceManager
}
func (s *ServiceImpl) RegisterRoutes() *ServiceImpl {
group := s.echo.Group("/v1")
group.POST("/users", s.handleCreateUser)
group.PATCH("/users/:userId", s.handleUpdateUser)
group.GET("/users/:userId", s.handleGetUserById)
group.GET("/users", s.handleGetUsers)
group.DELETE("/users/:userId", s.handleDeleteUser)
return s
}
func (s *ServiceImpl) Run() error {
return s.echo.Start(s.address)
}