@@ -9,22 +9,21 @@ import (
99 "strings"
1010)
1111
12- // StaticDir is the directory containing built dashboard files (set at build time)
13- const StaticDir = "dashboard"
14-
1512// Config holds the runtime configuration for the dashboard
1613type Config struct {
17- BasePath string
18- APIUrl string
19- GraphQLUrl string
14+ BasePath string
15+ APIUrl string
16+ GraphQLUrl string
17+ DashboardPath string // Path to dashboard files (defaults to "dashboard")
2018}
2119
2220// DefaultConfig returns the default configuration
2321func DefaultConfig () Config {
2422 return Config {
25- BasePath : "/" ,
26- APIUrl : "/api" ,
27- GraphQLUrl : "/graphql" ,
23+ BasePath : "/" ,
24+ APIUrl : "/api" ,
25+ GraphQLUrl : "/graphql" ,
26+ DashboardPath : "dashboard" ,
2827 }
2928}
3029
@@ -39,6 +38,11 @@ window.__ENV__ = {
3938
4039// Handler returns an http.Handler that serves the dashboard with the given config
4140func Handler (cfg Config ) http.Handler {
41+ // Set default dashboard path if not provided
42+ if cfg .DashboardPath == "" {
43+ cfg .DashboardPath = "dashboard"
44+ }
45+
4246 // Normalize base path - keep empty string for root, or ensure it starts with / and has no trailing /
4347 if cfg .BasePath != "" {
4448 if ! strings .HasPrefix (cfg .BasePath , "/" ) {
@@ -51,10 +55,14 @@ func Handler(cfg Config) http.Handler {
5155 tmpl := template .Must (template .New ("config.js" ).Parse (configJSTemplate ))
5256
5357 // Read index.html and prepare it with base path replacements
54- indexPath := filepath .Join (StaticDir , "index.html" )
58+ indexPath := filepath .Join (cfg . DashboardPath , "index.html" )
5559 indexHTML , err := os .ReadFile (indexPath )
5660 if err != nil {
57- panic ("Failed to read index.html: " + err .Error ())
61+ // Return a minimal handler that serves a placeholder
62+ return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
63+ w .WriteHeader (http .StatusServiceUnavailable )
64+ w .Write ([]byte ("Dashboard not available" ))
65+ })
5866 }
5967 // Replace {{BASE_PATH}} placeholder with actual base path
6068 indexContent := string (indexHTML )
@@ -68,7 +76,7 @@ func Handler(cfg Config) http.Handler {
6876 }
6977
7078 // Create file server for static files
71- fileServer := http .FileServer (http .Dir (StaticDir ))
79+ fileServer := http .FileServer (http .Dir (cfg . DashboardPath ))
7280
7381 return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
7482 // Serve dynamically generated config.js
@@ -88,7 +96,7 @@ func Handler(cfg Config) http.Handler {
8896 }
8997
9098 // Try to check if file exists on disk
91- filePath := filepath .Join (StaticDir , filepath .Clean (urlPath ))
99+ filePath := filepath .Join (cfg . DashboardPath , filepath .Clean (urlPath ))
92100 if info , err := os .Stat (filePath ); err == nil && ! info .IsDir () {
93101 // Special handling for index.html - serve with BASE_PATH replacements
94102 if urlPath == "index.html" {
0 commit comments