-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
58 lines (48 loc) · 1.27 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
51
52
53
54
55
56
57
58
package main
import (
"fmt"
"health/handlers"
"log"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
var serviceName string = "accesscontrol"
// conditionalLogger logs requests only if the status code is not 200
func conditionalLogger() gin.HandlerFunc {
return func(c *gin.Context) {
c.Next()
// Only log if the status code is not 200
if c.Writer.Status() != 200 {
log.Printf("[GIN] %s | %d | %s | %s",
c.ClientIP(),
c.Writer.Status(),
c.Request.Method,
c.Request.URL.Path,
)
}
}
}
func main() {
logger, _ := zap.NewProduction()
defer logger.Sync()
// Set Gin mode to ReleaseMode for production
gin.SetMode(gin.ReleaseMode)
// Initialize Gin router without default middleware
router := gin.New()
// Add custom logging middleware
router.Use(conditionalLogger())
// Readiness probe handler
router.GET("/readiness", handlers.ReadinessProbeHandler(serviceName))
// Liveness probe handler
router.GET("/liveness", handlers.LivenessProbeHandler(serviceName))
go func() {
// To run in localhost
//router.Run("localhost:8081")
if err := router.Run(); err != nil {
// Handle error if server fails to start
logger.Error("Failed to start server", zap.Error(err))
}
}()
fmt.Println("Server is running on localhost:8081")
select {}
}