Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
vincetse committed Dec 10, 2017
1 parent dda31a8 commit e40a49c
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions hello_world.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,38 @@ import (
"github.com/zsais/go-gin-prometheus"
)

var hostname = getHostname()

func getHostname() string {
var name, err = os.Hostname()
func ServiceInfoMiddleware() gin.HandlerFunc {
var hostname, err = os.Hostname()
if err != nil {
panic(err)
}
return name
return func(c *gin.Context) {
c.Writer.Header().Set("X-Hostname", hostname)
c.Next()
}
}

func getVersion() string {
return "$Id$"
}

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

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

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

func setupRouter(routePrefix string) *gin.Engine {
router := gin.Default()
ginprom := ginprometheus.NewPrometheus("gin")
ginprom.Use(router)
router.Use(ServiceInfoMiddleware())
router.GET("/health", healthFunc)

rg := router.Group(routePrefix)
Expand Down

0 comments on commit e40a49c

Please sign in to comment.