Skip to content

Commit 9fe6a2e

Browse files
committed
Added --ui flag
1 parent 48e1729 commit 9fe6a2e

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

cmd/pgmanager/server.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type ServerCommands struct {
2323

2424
type RunServer struct {
2525
URL string `arg:"" name:"url" help:"Database URL" default:""`
26+
UI bool `name:"ui" help:"Enable frontend UI" default:"false"`
2627

2728
// Postgres options
2829
PG struct {
@@ -76,7 +77,7 @@ func (cmd *RunServer) Run(ctx *Globals) error {
7677
// Register HTTP handlers
7778
router := http.NewServeMux()
7879
httphandler.RegisterBackendHandlers(router, ctx.HTTP.Prefix, manager)
79-
httphandler.RegisterFrontendHandler(router, "")
80+
httphandler.RegisterFrontendHandler(router, "", cmd.UI)
8081

8182
// Create a TLS config
8283
var tlsconfig *tls.Config

pkg/manager/httphandler/frontend_excluded.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
// RegisterFrontendHandler registers a fallback handler when frontend is not included
13-
func RegisterFrontendHandler(router *http.ServeMux, prefix string) {
13+
func RegisterFrontendHandler(router *http.ServeMux, prefix string, enabled bool) {
1414
// Catch all handler returns a "not found" error
1515
router.HandleFunc(joinPath(prefix, "/"), func(w http.ResponseWriter, r *http.Request) {
1616
_ = httpresponse.Error(w, httpresponse.ErrNotFound, r.URL.String())

pkg/manager/httphandler/frontend_included.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,25 @@ import (
88
"embed"
99
"io/fs"
1010
"net/http"
11+
12+
// Packages
13+
"github.com/mutablelogic/go-server/pkg/httpresponse"
1114
)
1215

1316
//go:embed frontend/*
1417
var frontendFS embed.FS
1518

16-
// RegisterFrontendHandler registers the frontend static file handler
17-
func RegisterFrontendHandler(router *http.ServeMux, prefix string) {
19+
// RegisterFrontendHandler registers the frontend static file handler if enabled,
20+
// otherwise registers a fallback handler that returns a not found error.
21+
func RegisterFrontendHandler(router *http.ServeMux, prefix string, enabled bool) {
22+
if !enabled {
23+
// Fallback handler returns a "not found" error
24+
router.HandleFunc(joinPath(prefix, "/"), func(w http.ResponseWriter, r *http.Request) {
25+
_ = httpresponse.Error(w, httpresponse.ErrNotFound, r.URL.String())
26+
})
27+
return
28+
}
29+
1830
// Get the subdirectory to strip the "frontend" prefix
1931
subFS, err := fs.Sub(frontendFS, "frontend")
2032
if err != nil {

0 commit comments

Comments
 (0)