Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions server/admin_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"

Expand Down Expand Up @@ -51,21 +52,35 @@
graphServices := services.NewGraph()
handler := handlers.NewAdmin(mockServices, graphServices)

// Admin Routes
mocksGroup := adminServerEngine.Group("/mocks")
// Clean base path prefix
basePrefix := strings.TrimRight(config.ConfigBasePath, "/")

// Create root or prefixed group
var adminGroup *echo.Group
if basePrefix != "" {
adminGroup = adminServerEngine.Group(basePrefix)
adminServerEngine.GET(basePrefix, func(c echo.Context) error {
return c.Redirect(http.StatusMovedPermanently, basePrefix+"/")
})
} else {
adminGroup = adminServerEngine.Group("")
}

// Admin Routes under adminGroup
mocksGroup := adminGroup.Group("/mocks")
mocksGroup.GET("", handler.GetMocks)
mocksGroup.POST("", handler.AddMocks)
mocksGroup.POST("/lock", handler.LockMocks)
mocksGroup.POST("/unlock", handler.UnlockMocks)
mocksGroup.PUT("/:id", handler.UpdateMock)
mocksGroup.DELETE("/:id", handler.DeleteMock)

historyGroup := adminServerEngine.Group("/history")
historyGroup := adminGroup.Group("/history")
historyGroup.GET("", handler.GetHistory)
historyGroup.GET("/summary", handler.SummarizeHistory)
historyGroup.DELETE("", handler.DeleteHistory)

sessionsGroup := adminServerEngine.Group("/sessions")
sessionsGroup := adminGroup.Group("/sessions")
sessionsGroup.GET("", handler.GetSessions)
sessionsGroup.POST("", handler.NewSession)
sessionsGroup.PUT("", handler.UpdateSession)
Expand All @@ -74,10 +89,10 @@
sessionsGroup.GET("/summary", handler.SummarizeSessions)
sessionsGroup.POST("/import", handler.ImportSession)

adminServerEngine.POST("/reset", handler.Reset)
adminGroup.POST("/reset", handler.Reset)

// Health Check Route
adminServerEngine.GET("/version", func(c echo.Context) error {
adminGroup.GET("/version", func(c echo.Context) error {
return c.JSON(http.StatusOK, config.Build)
})

Expand All @@ -86,13 +101,13 @@
// released binary is self-contained. Vite emits hashed assets under <root>/assets (served at
// /assets/*); index.html sits at the root and is rendered as a Go template (see renderIndex).
if fileExists(config.StaticFiles + "/index.html") {
adminServerEngine.Static("/assets", config.StaticFiles+"/assets")
adminGroup.Static("/assets", config.StaticFiles+"/assets")

Check failure on line 104 in server/admin_server.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal "/assets" 3 times.

See more on https://sonarcloud.io/project/issues?id=smocker-dev_smocker&issues=AaFhtBZvaWihG3HW8Fa5&open=AaFhtBZvaWihG3HW8Fa5&pullRequest=363
} else if dist, ok := frontend.FS(); ok {
if assets, err := fs.Sub(dist, "assets"); err == nil {
adminServerEngine.StaticFS("/assets", assets)
adminGroup.StaticFS("/assets", assets)
}
}
adminServerEngine.GET("/*", renderIndex(adminServerEngine, config))
adminGroup.GET("/*", renderIndex(adminServerEngine, config))

slog.Info("Starting admin server", "port", config.ConfigListenPort)
slog.Info("Starting mock server", "port", config.MockServerListenPort)
Expand Down Expand Up @@ -193,8 +208,13 @@
e.Renderer = templateRenderer
}

basePath := cfg.ConfigBasePath
if basePath != "/" && !strings.HasSuffix(basePath, "/") {
basePath += "/"
}

return c.Render(http.StatusOK, "index.html", echo.Map{
"basePath": cfg.ConfigBasePath,
"basePath": basePath,
"version": cfg.Build.BuildVersion,
})
}
Expand Down