|
1 | 1 | package secretmessage |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "net/http" |
4 | 5 | "os" |
| 6 | + "time" |
5 | 7 |
|
| 8 | + ginzap "github.com/gin-contrib/zap" |
6 | 9 | "github.com/gin-gonic/gin" |
| 10 | + "github.com/neufeldtech/secretmessage-go/pkg/secretslack" |
7 | 11 | "go.elastic.co/apm/module/apmgin" |
| 12 | + "go.elastic.co/apm/module/apmhttp" |
8 | 13 | "go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin" |
| 14 | + "go.uber.org/zap" |
9 | 15 | "gorm.io/gorm" |
10 | 16 | ) |
11 | 17 |
|
12 | 18 | type PublicController struct { |
13 | | - db *gorm.DB |
14 | | - config Config |
| 19 | + db *gorm.DB |
| 20 | + config Config |
| 21 | + logger *zap.Logger |
| 22 | + slackService *secretslack.SlackService |
15 | 23 | } |
16 | 24 |
|
17 | | -func NewController(config Config, db *gorm.DB) *PublicController { |
| 25 | +func NewController(config Config, db *gorm.DB, logger *zap.Logger) *PublicController { |
| 26 | + if logger == nil { |
| 27 | + logger = zap.Must(zap.NewProduction()) |
| 28 | + } |
| 29 | + |
| 30 | + slackService := secretslack.NewSlackService(). |
| 31 | + WithHTTPClient( |
| 32 | + apmhttp.WrapClient( |
| 33 | + &http.Client{ |
| 34 | + Timeout: 5 * time.Second}, |
| 35 | + )). |
| 36 | + WithLogger(logger) |
| 37 | + |
18 | 38 | return &PublicController{ |
19 | | - db: db, |
20 | | - config: config, |
| 39 | + db: db, |
| 40 | + config: config, |
| 41 | + logger: logger, |
| 42 | + slackService: slackService, |
21 | 43 | } |
22 | 44 | } |
23 | 45 |
|
24 | 46 | func (ctl *PublicController) ConfigureRoutes() *gin.Engine { |
25 | 47 |
|
26 | | - r := gin.Default() |
| 48 | + r := gin.New() |
| 49 | + r.Use(ginzap.Ginzap(ctl.logger, time.RFC3339, true)) |
| 50 | + r.Use(ginzap.RecoveryWithZap(ctl.logger, true)) |
| 51 | + |
27 | 52 | r.Use(otelgin.Middleware(os.Getenv("HOSTNAME"))) |
28 | 53 | r.Use(apmgin.Middleware(r)) |
29 | | - |
| 54 | + r.Use(func(c *gin.Context) { |
| 55 | + c.Next() |
| 56 | + db, err := ctl.db.DB() |
| 57 | + if err == nil { |
| 58 | + stats := db.Stats() |
| 59 | + ctl.logger.Info("DB Stats", |
| 60 | + zap.Int("OpenConnections", stats.OpenConnections), |
| 61 | + zap.Int("InUse", stats.InUse), |
| 62 | + zap.Int("Idle", stats.Idle), |
| 63 | + zap.Int64("WaitCount", stats.WaitCount), |
| 64 | + zap.Duration("WaitDuration", stats.WaitDuration), |
| 65 | + zap.Int("MaxOpenConnections", stats.MaxOpenConnections), |
| 66 | + zap.Int64("MaxIdleClosed", stats.MaxIdleClosed), |
| 67 | + zap.Int64("MaxIdleTimeClosed", stats.MaxIdleTimeClosed), |
| 68 | + zap.Int64("MaxLifetimeClosed", stats.MaxLifetimeClosed), |
| 69 | + ) |
| 70 | + } |
| 71 | + }) |
30 | 72 | r.GET("/health", ctl.HandleHealth) |
31 | 73 |
|
32 | 74 | r.GET("/auth/slack", ctl.HandleOauthBegin) |
33 | 75 | r.GET("/auth/slack/callback", ctl.HandleOauthCallback) |
34 | 76 |
|
35 | 77 | // Signature validation required |
36 | | - r.POST("/slash", ValidateSignature(ctl.config), ctl.HandleSlash) |
37 | | - r.POST("/interactive", ValidateSignature(ctl.config), ctl.HandleInteractive) |
| 78 | + r.POST("/slash", ctl.ValidateSignature(), ctl.HandleSlash) |
| 79 | + r.POST("/interactive", ctl.ValidateSignature(), ctl.HandleInteractive) |
38 | 80 |
|
39 | 81 | return r |
40 | 82 | } |
0 commit comments