/
middleware.go
54 lines (50 loc) · 1.76 KB
/
middleware.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
package middleware
import (
"net/http"
"strings"
"github.com/labstack/echo-contrib/prometheus"
"github.com/labstack/echo/v4"
echoMw "github.com/labstack/echo/v4/middleware"
)
// New intialises web server middleware
func New(e *echo.Echo, domainName string) {
config := echoMw.CORSConfig{
AllowCredentials: true,
Skipper: echoMw.DefaultSkipper,
AllowOrigins: []string{
"http://creator." + domainName,
"https://creator." + domainName,
"http://my." + domainName,
"https://my." + domainName,
"http://local." + domainName + ":3000",
"https://local." + domainName + ":3000",
"http://local." + domainName + ":8080",
"https://local." + domainName + ":8080",
"http://ystv-development.localhost:3000",
"https://ystv-development.localhost:3000",
"http://" + strings.Join(strings.Split(domainName, ".")[1:], "."),
"https://" + strings.Join(strings.Split(domainName, ".")[1:], "."),
"http://" + domainName,
"https://" + domainName,
"https://streamer." + domainName},
AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept, echo.HeaderAccessControlAllowCredentials, echo.HeaderAccessControlAllowOrigin, echo.HeaderAuthorization},
AllowMethods: []string{http.MethodGet, http.MethodHead, http.MethodPut, http.MethodPatch, http.MethodPost, http.MethodDelete},
}
e.Pre(echoMw.RemoveTrailingSlash())
e.Use(echoMw.Logger())
e.Use(echoMw.Recover())
e.Use(echoMw.CORSWithConfig(config))
e.Use(echoMw.GzipWithConfig(echoMw.GzipConfig{
Skipper: func(c echo.Context) bool {
path := c.Path()
if path == "/metrics" || strings.Contains(path, "swagger") {
return true
}
return false
},
}))
// TODO secure this
// /metrics, view using curl
p := prometheus.NewPrometheus("echo", nil)
p.Use(e)
}