-
Notifications
You must be signed in to change notification settings - Fork 1
/
features.go
77 lines (63 loc) · 1.64 KB
/
features.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
package server
import (
"fmt"
"os"
"time"
"github.com/getsentry/sentry-go"
sentrygin "github.com/getsentry/sentry-go/gin"
"github.com/gin-contrib/cors"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
"github.com/mandrigin/gin-spa/spa"
"github.com/simiancreative/simiango/logger"
)
// SetSPA hosts a Single Page App directory at the specified url
func SetSPA(url string, dir string) {
if _, err := os.Stat(dir); os.IsNotExist(err) {
panic(fmt.Sprintf("spa dir does not exist: %v", dir))
}
logger.Debug("Server: adding spa route", logger.Fields{
"path": url,
"dir": dir,
})
router.Use(spa.Middleware(url, dir))
}
// SetCORS applies CORS config to the gin router
func SetCORS() {
router.Use(cors.New(cors.Config{
AllowAllOrigins: true,
AllowMethods: []string{"*"},
AllowHeaders: []string{"*"},
ExposeHeaders: []string{"*"},
AllowCredentials: true,
MaxAge: 12 * time.Hour,
}))
}
// AddSentry adds the Sentry middleware to the gin router
func AddSentry(scopeFunc func(*gin.Context, *sentry.Scope)) {
router.Use(sentrygin.New(sentrygin.Options{
Repanic: true,
WaitForDelivery: false,
}))
router.Use(sentrySetup(scopeFunc))
}
func sentrySetup(scopeFunc func(*gin.Context, *sentry.Scope)) gin.HandlerFunc {
return func(c *gin.Context) {
c.Set("sentryScopeFunc", scopeFunc)
c.Next()
}
}
func healthGET(c *gin.Context) {
c.JSON(200, gin.H{
"status": "UP",
})
}
func EnableHealthCheck() {
router.Handle("GET", "/status", healthGET)
}
func EnableCustomHealthCheck(path string, check gin.HandlerFunc) {
router.Handle("GET", path, check)
}
func AddPprof() {
pprof.Register(router)
}