-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
117 lines (94 loc) · 3.16 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
package server
import (
"crypto/tls"
"net/http"
"strconv"
"time"
"github.com/Festivals-App/festivals-database/server/config"
"github.com/Festivals-App/festivals-database/server/handler"
token "github.com/Festivals-App/festivals-identity-server/jwt"
festivalspki "github.com/Festivals-App/festivals-pki"
servertools "github.com/Festivals-App/festivals-server-tools"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/rs/zerolog/log"
)
// Server has router and db instances
type Server struct {
Router *chi.Mux
Config *config.Config
TLSConfig *tls.Config
Validator *token.ValidationService
}
func NewServer(config *config.Config) *Server {
server := &Server{}
server.initialize(config)
return server
}
// Initialize the server with predefined configuration
func (s *Server) initialize(config *config.Config) {
s.Router = chi.NewRouter()
s.Config = config
s.setIdentityService()
s.setTLSHandling()
s.setMiddleware()
s.setRoutes()
}
func (s *Server) setIdentityService() {
config := s.Config
val := token.NewValidationService(config.IdentityEndpoint, config.TLSCert, config.TLSKey, config.ServiceKey, false)
if val == nil {
log.Fatal().Msg("Failed to create validator.")
}
s.Validator = val
}
func (s *Server) setTLSHandling() {
tlsConfig := &tls.Config{
ClientAuth: tls.RequireAndVerifyClientCert,
GetCertificate: festivalspki.LoadServerCertificateHandler(s.Config.TLSCert, s.Config.TLSKey, s.Config.TLSRootCert),
}
s.TLSConfig = tlsConfig
}
func (s *Server) setMiddleware() {
// tell the ruter which middleware to use
s.Router.Use(
// used to log the request to the console | development
servertools.Middleware(servertools.TraceLogger("/var/log/festivals-database-node/trace.log")),
// tries to recover after panics
middleware.Recoverer,
)
}
// setRouters sets the all required routers
func (s *Server) setRoutes() {
s.Router.Get("/version", s.handleRequest(handler.GetVersion))
s.Router.Get("/info", s.handleRequest(handler.GetInfo))
s.Router.Get("/health", s.handleRequest(handler.GetHealth))
s.Router.Post("/update", s.handleRequest(handler.MakeUpdate))
s.Router.Get("/log", s.handleRequest(handler.GetLog))
s.Router.Get("/log/trace", s.handleRequest(handler.GetTraceLog))
}
func (s *Server) Run(conf *config.Config) {
server := http.Server{
ReadTimeout: 10 * time.Second,
WriteTimeout: 25 * time.Second,
IdleTimeout: 60 * time.Second,
ReadHeaderTimeout: 5 * time.Second,
Addr: conf.ServiceBindHost + ":" + strconv.Itoa(conf.ServicePort),
Handler: s.Router,
TLSConfig: s.TLSConfig,
}
if err := server.ListenAndServeTLS("", ""); err != nil {
log.Fatal().Err(err).Str("type", "server").Msg("Failed to run server")
}
}
type JWTAuthenticatedHandlerFunction func(claims *token.UserClaims, w http.ResponseWriter, r *http.Request)
func (s *Server) handleRequest(requestHandler JWTAuthenticatedHandlerFunction) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
claims := token.GetValidClaims(r, s.Validator)
if claims == nil {
servertools.UnauthorizedResponse(w)
return
}
requestHandler(claims, w, r)
})
}