From 527e2abde84ae8dbe4c5085d7ef74f85c64565e5 Mon Sep 17 00:00:00 2001 From: Joost van der Griendt Date: Sun, 2 Jan 2022 12:57:28 +0100 Subject: [PATCH] allow root path to be configurable --- Dockerfile | 1 + cmd/webserver/webserver.go | 44 +++++++++++++++++++++++++++++--------- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/Dockerfile b/Dockerfile index a120de2..388f403 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,6 +9,7 @@ FROM alpine:3 RUN apk --no-cache add ca-certificates EXPOSE 8080 ENV PORT=8080 +ENV ROOT_PATH="/" ENTRYPOINT ["/usr/bin/cmg"] CMD ["serve"] COPY --from=builder /go/src/cmg/bin/$TARGETARCH/cmg /usr/bin/cmg \ No newline at end of file diff --git a/cmd/webserver/webserver.go b/cmd/webserver/webserver.go index cf49e39..d15a330 100644 --- a/cmd/webserver/webserver.go +++ b/cmd/webserver/webserver.go @@ -13,6 +13,7 @@ import ( "net/http" "os" "runtime" + "strings" "time" ) @@ -22,7 +23,9 @@ const ( envSentry = "SENTRY_DSN" envSegmentKey = "SEGMENT_KEY" envLogLevel = "LOG_LEVEL" + envRootPath = "ROOT_PATH" + defaultRootPath = "/" defaultPort = "8080" debugLogLevel = "DEBUG" defaultLogLevel = "INFO" @@ -39,6 +42,15 @@ func StartWebserver() { port = defaultPort } + rootPath, rootPathOk := os.LookupEnv(envRootPath) + if !rootPathOk || rootPath == "" { + rootPath = defaultRootPath + } + // ensure we end with a "/" so all derived paths will work + if !strings.HasSuffix(rootPath, "/") { + rootPath += "/" + } + logFormat, logFormatOk := os.LookupEnv(envLogFormatter) if logFormatOk && logFormat == jsonLogFormatter { log.SetFormatter(&log.JSONFormatter{}) @@ -70,6 +82,7 @@ func StartWebserver() { // Echo instance e := echo.New() log.WithFields(log.Fields{ + "RootPath": rootPath, "Port": port, "LogFormatter": logFormat, "LogLevel": logLevel, @@ -109,21 +122,32 @@ func StartWebserver() { e.Use(sentryecho.New(sentryecho.Options{})) // Routes - e.GET("/", hello) - e.GET("/rollout", rolloutDemo) - e.GET("/api/map", webserver.GetMap) - e.GET("/api/v1/map", webserver.GetMapViaCodeGeneration) - e.GET("/api/map/code", webserver.GetMapCode) - e.GET("/api/map/code/:code", webserver.GetMapByCode) - e.GET("/api/legend", webserver.GetMapLegend) + g := e.Group(rootPath) + g.GET("", handleRoutes) + g.GET("routes", handleRoutes) + g.GET("rollout", rolloutDemo) + + g.GET("api/map", webserver.GetMap) + g.GET("api/v1/map", webserver.GetMapViaCodeGeneration) + g.GET("api/map/code", webserver.GetMapCode) + g.GET("api/map/code/:code", webserver.GetMapByCode) + g.GET("api/legend", webserver.GetMapLegend) // Start server e.Logger.Fatal(e.Start(":" + port)) } -// Handler -func hello(c echo.Context) error { - return c.String(http.StatusOK, "Hello, World!!") +// handleRoutes shows the routes that are handled +func handleRoutes(c echo.Context) error { + content := c.Echo().Routes() + + callback := c.QueryParam("callback") + jsonp := c.QueryParam("jsonp") + + if jsonp == "true" { + return c.JSONP(http.StatusOK, callback, &content) + } + return c.JSON(http.StatusOK, &content) } func rolloutDemo(c echo.Context) error {