diff --git a/hello_world.go b/hello_world.go index 2b8cd01..7b90b68 100644 --- a/hello_world.go +++ b/hello_world.go @@ -2,42 +2,42 @@ package main import ( "net/http" - "os" + "os" - "github.com/gin-gonic/gin" - "github.com/zsais/go-gin-prometheus" + "github.com/gin-gonic/gin" + "github.com/zsais/go-gin-prometheus" ) var hostname = getHostname() func getHostname() string { - var name, err = os.Hostname() - if err != nil { - panic(err) - } - return name + var name, err = os.Hostname() + if err != nil { + panic(err) + } + return name } func helloFunc(c *gin.Context) { - c.Writer.Header().Set("X-Hostname", hostname) - c.String(http.StatusOK, "Hello, World!") + 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, "") + c.Writer.Header().Set("X-Hostname", hostname) + c.String(http.StatusOK, "") } func setupRouter() *gin.Engine { - router := gin.Default() - ginprom := ginprometheus.NewPrometheus("gin") - ginprom.Use(router) - router.GET("/", helloFunc) - router.GET("/health", healthFunc) - return router + router := gin.Default() + ginprom := ginprometheus.NewPrometheus("gin") + ginprom.Use(router) + router.GET("/", helloFunc) + router.GET("/health", healthFunc) + return router } func main() { - router := setupRouter() - router.Run() + router := setupRouter() + router.Run() } diff --git a/hello_world_test.go b/hello_world_test.go index 3b430d7..3b69268 100644 --- a/hello_world_test.go +++ b/hello_world_test.go @@ -1,4 +1,3 @@ - package main import ( @@ -10,33 +9,33 @@ import ( ) func TestDefaultRoute(t *testing.T) { - router := setupRouter() + router := setupRouter() w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/", nil) router.ServeHTTP(w, req) - assert.Equal(t, 200, w.Code) + assert.Equal(t, 200, w.Code) assert.Equal(t, "Hello, World!", w.Body.String()) } func TestHealthRoute(t *testing.T) { - router := setupRouter() + router := setupRouter() w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/health", nil) router.ServeHTTP(w, req) - assert.Equal(t, 200, w.Code) + assert.Equal(t, 200, w.Code) assert.Equal(t, "", w.Body.String()) } func TestMetricsRoute(t *testing.T) { - router := setupRouter() + router := setupRouter() w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/metrics", nil) router.ServeHTTP(w, req) - assert.Equal(t, 200, w.Code) + assert.Equal(t, 200, w.Code) }