-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
50 lines (39 loc) · 1.37 KB
/
main.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
package main
import (
"log"
"time"
"github.com/gin-gonic/gin"
"github.com/moesif/moesifgin"
)
func main() {
r := gin.New()
moesifOptions := map[string]interface{}{
"Application_Id": "<Your Moesif Application Id>",
"Log_Body": true,
"Identify_User": func(c *gin.Context) string { return c.Request.Header.Get("X-User-Id") },
"Identify_Company": func(c *gin.Context) string { return c.Request.Header.Get("X-Company-Id") },
"Get_Metadata": func(c *gin.Context) map[string]interface{} { return map[string]interface{}{"example": "metadata"} },
}
r.Use(moesifgin.MoesifMiddleware(moesifOptions))
r.Use(ExampleOtherMiddleware()) // Example to show the usage of other middleware
r.POST("/test", func(c *gin.Context) {
log.Println("Executing test endpoint")
c.JSON(201, gin.H{"message": "hello world"})
log.Println("Test endpoint executed successfully")
})
// Listen and serve on 0.0.0.0:8080
r.Run(":8080")
}
// ExampleOtherMiddleware is just used to show the successful integration with other middleware
// It is not required for Moesif to work.
func ExampleOtherMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
t := time.Now()
log.Println("example middleware before request")
c.Next()
latency := time.Since(t)
log.Print(latency)
status := c.Writer.Status()
log.Println("example middleware writing status: ", status)
}
}