/
httpsrv.go
134 lines (118 loc) · 3.54 KB
/
httpsrv.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package http
import (
"context"
"io"
"net/http/pprof"
"github.com/labstack/echo/v4"
"github.com/dalmarcogd/mobstore/users/internal/services"
)
type (
httpService struct {
services.NoopHealth
serviceManager services.ServiceManager
ctx context.Context
echo *echo.Echo
address string
}
)
func New() *httpService {
return &httpService{}
}
func (s *httpService) WithAddress(address string) *httpService {
s.address = address
return s
}
func (s *httpService) Init(ctx context.Context) error {
s.ctx = ctx
if s.echo == nil {
s.echo = echo.New()
s.echo.Logger.SetOutput(io.Discard)
s.echo.Use(
CidContextMiddleware(),
SpanMiddleware(s.ServiceManager().Spans(), s.ServiceManager().Environment().Service(), []string{"/readiness", "/liveness"}),
LogMiddleware(s.ServiceManager().Logger(), []string{"/readiness", "/liveness"}),
)
}
s.RegisterRoutes()
return nil
}
func (s *httpService) Close() error {
if err := s.echo.Shutdown(s.ctx); err != nil {
return err
}
return s.echo.Close()
}
func (s *httpService) WithServiceManager(c services.ServiceManager) services.HttpServer {
s.serviceManager = c
return s
}
func (s *httpService) ServiceManager() services.ServiceManager {
return s.serviceManager
}
func (s *httpService) RegisterRoutes() *httpService {
s.echo.GET("/readiness", s.handleReadiness)
s.echo.GET("/liveness", s.handleLiveness)
gV1 := s.echo.Group("/v1")
gV1.POST("/users", s.handleCreateV1)
gV1.GET("/users", s.handleGetV1)
gV1.GET("/users/:userId", s.handleGetByIdV1)
gV1.PATCH("/users/:userId", s.handleUpdateV1)
gV1.DELETE("/users/:userId", s.handleDeleteV1)
//routes of pprof
if s.ServiceManager().Environment().DebugPprof() {
gDebug := s.echo.Group("/debug")
gDebug.GET("/pprof/", func(c echo.Context) error {
pprof.Index(c.Response().Writer, c.Request())
return nil
})
gDebug.GET("/pprof/allocs", func(c echo.Context) error {
pprof.Handler("allocs").ServeHTTP(c.Response().Writer, c.Request())
return nil
})
gDebug.GET("/pprof/heap", func(c echo.Context) error {
pprof.Handler("heap").ServeHTTP(c.Response().Writer, c.Request())
return nil
})
gDebug.GET("/pprof/goroutine", func(c echo.Context) error {
pprof.Handler("goroutine").ServeHTTP(c.Response().Writer, c.Request())
return nil
})
gDebug.GET("/pprof/block", func(c echo.Context) error {
pprof.Handler("block").ServeHTTP(c.Response().Writer, c.Request())
return nil
})
gDebug.GET("/pprof/threadcreate", func(c echo.Context) error {
pprof.Handler("threadcreate").ServeHTTP(c.Response().Writer, c.Request())
return nil
})
gDebug.GET("/pprof/cmdline", func(c echo.Context) error {
pprof.Cmdline(c.Response().Writer, c.Request())
return nil
})
gDebug.GET("/pprof/profile", func(c echo.Context) error {
pprof.Profile(c.Response().Writer, c.Request())
return nil
})
gDebug.GET("/pprof/symbol", func(c echo.Context) error {
pprof.Symbol(c.Response().Writer, c.Request())
return nil
})
gDebug.POST("/pprof/symbol", func(c echo.Context) error {
pprof.Trace(c.Response().Writer, c.Request())
return nil
})
gDebug.GET("/pprof/trace", func(c echo.Context) error {
pprof.Handler("block").ServeHTTP(c.Response().Writer, c.Request())
return nil
})
gDebug.GET("/pprof/mutex", func(c echo.Context) error {
pprof.Handler("mutex").ServeHTTP(c.Response().Writer, c.Request())
return nil
})
s.ServiceManager().Logger().Info(s.ctx, "Registered route of /debug/pprof")
}
return s
}
func (s *httpService) Run() error {
return s.echo.Start(s.address)
}