-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
117 lines (97 loc) · 2.47 KB
/
router.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 main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/ystv/computing_site/link"
"github.com/ystv/computing_site/team"
"github.com/ystv/computing_site/templates"
)
type (
Router struct {
address string
templater *templates.Templater
link *link.Link
team *[]team.Member
commit string
version string
cert []byte
key []byte
router *echo.Echo
}
RouterConf struct {
Address string
Link *link.Link
Team *[]team.Member
Commit string
Version string
cert []byte
key []byte
}
)
func NewRouter(conf *RouterConf) *Router {
r := &Router{
address: conf.Address,
link: conf.Link,
team: conf.Team,
commit: conf.Commit,
version: conf.Version,
cert: cert,
key: key,
router: echo.New(),
}
r.router.HideBanner = true
r.middleware()
r.loadRoutes()
return r
}
func (r *Router) Start() error {
// r.router.Logger.Error(r.router.StartTLS(r.address, r.cert, r.key))
r.router.Logger.Error(r.router.Start(r.address))
return fmt.Errorf("failed to start router on address %s", r.address)
}
// middleware initialises web server middleware
func (r *Router) middleware() {
r.router.Pre(middleware.RemoveTrailingSlash())
r.router.Use(middleware.Recover())
r.router.Use(middleware.BodyLimit("15M"))
r.router.Use(middleware.GzipWithConfig(middleware.GzipConfig{
Level: 5,
}))
}
func (r *Router) loadRoutes() {
r.router.RouteNotFound("/*", func(c echo.Context) error { return c.Redirect(http.StatusFound, "/") })
assetHandler := http.FileServer(http.FS(echo.MustSubFS(embeddedFiles, "public")))
r.router.GET("/public/*", echo.WrapHandler(http.StripPrefix("/public/", assetHandler)))
base := r.router.Group("/")
base.GET("api/health", func(c echo.Context) error {
marshal, err := json.Marshal(struct {
Status int `json:"status"`
}{
Status: http.StatusOK,
})
if err != nil {
log.Println(err)
return &echo.HTTPError{
Code: http.StatusBadRequest,
Message: err.Error(),
Internal: err,
}
}
c.Response().Header().Set("Content-Type", "application/json")
return c.JSON(http.StatusOK, marshal)
})
base.GET("", r.HomeFunc)
}
func (r *Router) HomeFunc(c echo.Context) error {
params := &templates.DashboardParams{
Link: r.link,
Team: r.team,
Commit: r.commit,
Version: r.version,
}
return r.templater.RenderTemplate(c.Response(), params, "dashboard.tmpl")
}