-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
64 lines (54 loc) · 1.62 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
package http
import (
"embed"
"github.com/blueskyfish/server-users/configuration"
"github.com/blueskyfish/server-users/http/log"
"github.com/blueskyfish/server-users/http/route"
"github.com/labstack/echo/v4"
mw "github.com/labstack/echo/v4/middleware"
"io/fs"
"net/http"
)
// Server the server struct
type Server struct {
// Name the server name
Name string
http *echo.Echo
}
func NewServer(conf *configuration.Configuration) Server {
server := Server{}
server.Name = conf.Name()
server.http = echo.New()
server.http.Logger = log.NewHttpLogger(conf, "server")
server.http.HideBanner = true
return server
}
// Init initialize the server middlewares, routes and embed resources
func (server Server) Init(conf *configuration.Configuration, webFiles *embed.FS) error {
serverLogger := conf.WithGroup("server")
// Middlewares
server.http.Use(LogMiddleware(conf, "context"))
server.http.Use(RequestMiddleware(conf))
server.http.Use(mw.Recover())
serverLogger.Debug("add middlewares")
// Routes
server.http.GET("/api/about", route.GetAbout)
serverLogger.Debug("add routes")
// Embed resources
fileSys, err := fs.Sub(webFiles, "web")
if err != nil {
return err
}
assetHandler := http.FileServer(http.FS(fileSys))
server.http.GET("/*", echo.WrapHandler(assetHandler))
serverLogger.Debug("setup asset handler")
return nil
}
// Start starts the http server. Before starting, it is connected with the embed resources
func (server Server) Start(conf *configuration.Configuration) error {
return server.http.Start(conf.Address())
}
// Http returns the http instance
func (server Server) Http() *echo.Echo {
return server.http
}