/
start.go
75 lines (62 loc) · 1.56 KB
/
start.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
package app
import (
"net/http"
"strings"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/sirupsen/logrus"
"github.com/typical-go/typical-rest-server/internal/app/domain/mylibrary"
"github.com/typical-go/typical-rest-server/internal/app/domain/mymusic"
"github.com/typical-go/typical-rest-server/internal/app/infra"
"github.com/typical-go/typical-rest-server/internal/app/infra/log"
"github.com/typical-go/typical-rest-server/pkg/echokit"
"go.uber.org/dig"
// enable `/debug/vars`
_ "expvar"
// enable `/debug/pprof` API
_ "net/http/pprof"
)
const (
healthCheckPath = "/application/health"
)
type (
app struct {
dig.In
*echo.Echo
Config *infra.AppCfg
MyLibrary mylibrary.Router
MyMusic mymusic.Router
HealthCheck HealthCheck
}
)
// Start app
func Start(a app) (err error) {
setMiddleware(a)
setRoute(a)
setProfiler(a)
if a.Config.Debug {
routes := echokit.DumpEcho(a.Echo)
logrus.Debugf("Print routes:\n %s\n\n", strings.Join(routes, "\n "))
}
return a.StartServer(&http.Server{
Addr: a.Config.Address,
ReadTimeout: a.Config.ReadTimeout,
WriteTimeout: a.Config.WriteTimeout,
})
}
func setMiddleware(a app) {
a.Use(log.Middleware)
a.Use(middleware.Recover())
}
func setRoute(a app) {
echokit.SetRoute(a,
&a.MyLibrary,
&a.MyMusic,
)
}
func setProfiler(a app) {
a.GET(healthCheckPath, a.HealthCheck.Handle)
a.HEAD(healthCheckPath, a.HealthCheck.Handle)
a.GET("/debug/*", echo.WrapHandler(http.DefaultServeMux))
a.GET("/debug/*/*", echo.WrapHandler(http.DefaultServeMux))
}