-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
120 lines (95 loc) · 2.31 KB
/
server.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
package server
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/diegodesousas/clean-boilerplate-go/infra/http/middlewares"
"github.com/julienschmidt/httprouter"
"github.com/spf13/viper"
)
var DefaultMonitorWrapper = func(path string, handler http.Handler) http.Handler {
return handler
}
type (
Config func(server *Server)
MonitorWrapper func(path string, handler http.Handler) http.Handler
)
type Server struct {
http.Server
routes []Route
router *httprouter.Router
monitorWrapper MonitorWrapper
}
type Route struct {
Path string
Method string
Handler http.Handler
}
func NewServer(configs ...Config) *Server {
server := &Server{
Server: http.Server{
Addr: ":" + viper.GetString("HTTP_PORT"),
},
router: httprouter.New(),
monitorWrapper: DefaultMonitorWrapper,
}
server.Server.Handler = middlewares.Middlewares(
server,
middlewares.PanicRecoveryMiddleware,
middlewares.Logger,
middlewares.LogRouteMiddleware,
)
for _, config := range configs {
config(server)
}
server.buildRoutes()
return server
}
type ShutdownHandler func(ctx context.Context) error
func (s *Server) ListenAndServe(ctx context.Context, handlers ...ShutdownHandler) error {
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
go func() {
if err := s.Server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Print(err)
}
interrupt <- syscall.SIGTERM
}()
<-interrupt
log.Println("shutdown application")
log.Println("shutdown http server")
if err := s.Shutdown(ctx); err != nil {
log.Printf("http: %s", err)
}
for _, handler := range handlers {
err := handler(ctx)
if err != nil {
log.Print(err)
}
}
return nil
}
func (s *Server) Route(r Route) {
s.routes = append(s.routes, r)
}
func (s *Server) buildRoutes() {
for _, r := range s.routes {
s.router.Handler(r.Method, r.Path, s.monitorWrapper(r.Path, r.Handler))
}
s.routes = []Route{}
}
func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
s.router.ServeHTTP(w, req)
}
func WithMonitorWrapper(wrapper MonitorWrapper) Config {
return func(server *Server) {
if wrapper == nil {
return
}
server.monitorWrapper = wrapper
}
}