Skip to content

Commit

Permalink
allow root path to be configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
joostvdg committed Jan 2, 2022
1 parent 9bcc5bf commit 527e2ab
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
44 changes: 34 additions & 10 deletions cmd/webserver/webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"net/http"
"os"
"runtime"
"strings"
"time"
)

Expand All @@ -22,7 +23,9 @@ const (
envSentry = "SENTRY_DSN"
envSegmentKey = "SEGMENT_KEY"
envLogLevel = "LOG_LEVEL"
envRootPath = "ROOT_PATH"

defaultRootPath = "/"
defaultPort = "8080"
debugLogLevel = "DEBUG"
defaultLogLevel = "INFO"
Expand All @@ -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{})
Expand Down Expand Up @@ -70,6 +82,7 @@ func StartWebserver() {
// Echo instance
e := echo.New()
log.WithFields(log.Fields{
"RootPath": rootPath,
"Port": port,
"LogFormatter": logFormat,
"LogLevel": logLevel,
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 527e2ab

Please sign in to comment.