-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
58 lines (47 loc) · 1.34 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
package handler
import (
"fmt"
"net/http"
"github.com/arl/statsviz"
"github.com/gin-gonic/gin"
"github.com/hiago-balbino/random-luck/v2/config"
"github.com/hiago-balbino/random-luck/v2/internal/game"
"github.com/hiago-balbino/random-luck/v2/internal/pkg/logger"
"github.com/spf13/viper"
)
var log = logger.GetLogger()
// Server is an structure to support the handler.
type Server struct {
handler Handler
}
// NewServer create a new instante of Server structure.
func NewServer() Server {
config.InitConfigurations()
randomizer := game.NewGameRandomizer()
return Server{
handler: NewWeb(randomizer),
}
}
// Start initialize the router.
func (s Server) Start() {
router := s.setupRoutes("web/templates/*")
if err := router.Run(fmt.Sprintf(":%s", viper.GetString("PORT"))); err != nil {
log.Fatal("error while server starting", logger.FieldError(err))
}
}
func (s Server) setupRoutes(templatePath string) *gin.Engine {
router := gin.Default()
router.LoadHTMLGlob(templatePath)
router.GET("/index", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", nil)
})
router.GET("/metrics/*filepath", func(c *gin.Context) {
if c.Param("filepath") == "/ws" {
statsviz.Ws(c.Writer, c.Request)
return
}
statsviz.IndexAtRoot("/metrics").ServeHTTP(c.Writer, c.Request)
})
router.GET("/process", s.handler.Process)
return router
}