/
server.go
60 lines (49 loc) · 1.4 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
package restserver
import (
"errors"
"log"
"github.com/colibri-project-io/colibri-sdk-go/pkg/base/config"
"github.com/colibri-project-io/colibri-sdk-go/pkg/base/logging"
"github.com/colibri-project-io/colibri-sdk-go/pkg/base/observer"
)
var (
srvRoutes []Route
customMiddlewares []CustomMiddleware
srv Server
customAuth CustomAuthenticationMiddleware
UserUnauthenticatedError = errors.New("user not authenticated")
)
// Server is the contract to http server implementation
type Server interface {
initialize()
shutdown() error
injectMiddlewares()
injectCustomMiddlewares()
injectRoutes()
listenAndServe() error
}
// AddRoutes add list of routes in the webrest server
func AddRoutes(routes []Route) {
srvRoutes = append(srvRoutes, routes...)
}
func CustomAuthMiddleware(fn CustomAuthenticationMiddleware) {
customAuth = fn
}
func Use(m CustomMiddleware) {
customMiddlewares = append(customMiddlewares, m)
}
// ListenAndServe initialize, configure and expose the web rest server
func ListenAndServe() {
addHealthCheckRoute()
addDocumentationRoute()
srv = createFiberServer()
srv.initialize()
srv.injectMiddlewares()
srv.injectCustomMiddlewares()
srv.injectRoutes()
observer.Attach(restObserver{})
logging.Info("Service '%s' running in %d port", "WEB-REST", config.PORT)
if err := srv.listenAndServe(); err != nil {
log.Fatalf("Error rest server: %v", err)
}
}