/
middleware.go
58 lines (47 loc) · 1.49 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
55
56
57
58
package transport
import (
"log"
"github.com/gilperopiola/go-rest-example/pkg/config"
"github.com/gilperopiola/go-rest-example/pkg/logger"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/newrelic/go-agent/v3/integrations/nrgin"
"github.com/newrelic/go-agent/v3/newrelic"
)
// This is exported because the monitoring is initialized in the main.go file
func NewMonitoringMiddleware(config config.ConfigI) gin.HandlerFunc {
cfg := config.GetMonitoringConfig()
if !cfg.ENABLED {
return nil
}
// If monitoring is enabled, use license to create New Relic app
license := cfg.SECRET
if license == "" {
log.Fatalf("New Relic license not found")
}
newRelicApp, err := newrelic.NewApplication(
newrelic.ConfigAppName(cfg.APP_NAME),
newrelic.ConfigLicense(license),
newrelic.ConfigAppLogForwardingEnabled(true),
)
// Panic on failure
if err != nil {
log.Fatalf("Failed to start New Relic: %v", err)
}
return nrgin.Middleware(newRelicApp)
}
func newLoggerToContextMiddleware(logger logger.LoggerI) gin.HandlerFunc {
return func(c *gin.Context) {
c.Set("logger", logger)
c.Next()
}
}
func newCORSConfigMiddleware() gin.HandlerFunc {
return cors.New(cors.Config{
AllowAllOrigins: true,
AllowCredentials: true,
AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"},
AllowHeaders: []string{"Authentication", "Authorization", "Content-Type"},
ExposeHeaders: []string{"Authentication", "Authorization", "Content-Type"},
})
}