diff --git a/hello_world.go b/hello_world.go index 7b90b68..fc2bd0f 100644 --- a/hello_world.go +++ b/hello_world.go @@ -1,6 +1,7 @@ package main import ( + "flag" "net/http" "os" @@ -28,16 +29,21 @@ func healthFunc(c *gin.Context) { c.String(http.StatusOK, "") } -func setupRouter() *gin.Engine { +func setupRouter(routePrefix string) *gin.Engine { router := gin.Default() ginprom := ginprometheus.NewPrometheus("gin") ginprom.Use(router) - router.GET("/", helloFunc) router.GET("/health", healthFunc) + + rg := router.Group(routePrefix) + rg.GET("/", helloFunc) return router } func main() { - router := setupRouter() + optRoutePrefix := flag.String("route-prefix", "/", "Route prefix") + flag.Parse() + + router := setupRouter(*optRoutePrefix) router.Run() } diff --git a/hello_world_test.go b/hello_world_test.go index 3b69268..3c85bae 100644 --- a/hello_world_test.go +++ b/hello_world_test.go @@ -9,7 +9,7 @@ import ( ) func TestDefaultRoute(t *testing.T) { - router := setupRouter() + router := setupRouter("/") w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/", nil) @@ -20,7 +20,7 @@ func TestDefaultRoute(t *testing.T) { } func TestHealthRoute(t *testing.T) { - router := setupRouter() + router := setupRouter("/") w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/health", nil) @@ -31,7 +31,7 @@ func TestHealthRoute(t *testing.T) { } func TestMetricsRoute(t *testing.T) { - router := setupRouter() + router := setupRouter("/") w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/metrics", nil) @@ -39,3 +39,15 @@ func TestMetricsRoute(t *testing.T) { assert.Equal(t, 200, w.Code) } + +func TestRoutePrefix(t *testing.T) { + prefix := "/foobar/" + router := setupRouter(prefix) + + w := httptest.NewRecorder() + req, _ := http.NewRequest("GET", prefix, nil) + router.ServeHTTP(w, req) + + assert.Equal(t, 200, w.Code) + assert.Equal(t, "Hello, World!", w.Body.String()) +}