Skip to content

Commit e40a49c

Browse files
committed
Refactor
1 parent dda31a8 commit e40a49c

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

hello_world.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,38 @@ import (
99
"github.com/zsais/go-gin-prometheus"
1010
)
1111

12-
var hostname = getHostname()
13-
14-
func getHostname() string {
15-
var name, err = os.Hostname()
12+
func ServiceInfoMiddleware() gin.HandlerFunc {
13+
var hostname, err = os.Hostname()
1614
if err != nil {
1715
panic(err)
1816
}
19-
return name
17+
return func(c *gin.Context) {
18+
c.Writer.Header().Set("X-Hostname", hostname)
19+
c.Next()
20+
}
2021
}
2122

2223
func getVersion() string {
2324
return "$Id$"
2425
}
2526

2627
func helloFunc(c *gin.Context) {
27-
c.Writer.Header().Set("X-Hostname", hostname)
2828
c.String(http.StatusOK, "Hello, World!")
2929
}
3030

3131
func healthFunc(c *gin.Context) {
32-
c.Writer.Header().Set("X-Hostname", hostname)
3332
c.String(http.StatusOK, "")
3433
}
3534

3635
func versionFunc(c *gin.Context) {
37-
c.Writer.Header().Set("X-Hostname", hostname)
3836
c.String(http.StatusOK, getVersion())
3937
}
4038

4139
func setupRouter(routePrefix string) *gin.Engine {
4240
router := gin.Default()
4341
ginprom := ginprometheus.NewPrometheus("gin")
4442
ginprom.Use(router)
43+
router.Use(ServiceInfoMiddleware())
4544
router.GET("/health", healthFunc)
4645

4746
rg := router.Group(routePrefix)

0 commit comments

Comments
 (0)