diff --git a/bootserver/config/config.go b/bootserver/config/config.go index 8db8fbb..f5cfea8 100644 --- a/bootserver/config/config.go +++ b/bootserver/config/config.go @@ -43,7 +43,9 @@ type jsonConf struct { Level string `json:"level"` } `json:"log"` Storage struct { - Path string `json:"path"` + Path string `json:"path"` + TemplateDir string `json:"templatePath"` + StaticDir string `json:"staticPath"` } `json:"storage"` Server struct { Host string `json:"host"` @@ -63,6 +65,7 @@ type AppConfig struct { LogLevel logrus.Level ServerMode ListeningMode DataFilepath string + StaticDir string Host string Port int SockPath string @@ -101,6 +104,7 @@ func (ac *AppConfig) UnmarshalJSON(data []byte) error { ac.UDPPort = jsonConf.BootProvider.Port ac.UDPSrcAddr = jsonConf.BootProvider.SrcAddr ac.DataFilepath = jsonConf.Storage.Path + ac.StaticDir = jsonConf.Storage.StaticDir return nil } @@ -113,6 +117,7 @@ var defaultConfig AppConfig = AppConfig{ UPDMcastGroup: "ff02::abcd:1234", UDPPort: 42, UDPIface: "eth0", + StaticDir: "./static", } func New(filepath string) (*AppConfig, error) { diff --git a/bootserver/controllers/ui/static.go b/bootserver/controllers/ui/static.go index 8c5d687..80a2c82 100644 --- a/bootserver/controllers/ui/static.go +++ b/bootserver/controllers/ui/static.go @@ -7,9 +7,14 @@ import ( const StaticRoute = "/static/" type StaticController struct { + staticDir string +} + +func NewStaticController(staticDir string) *StaticController { + return &StaticController{staticDir: staticDir} } func (sc *StaticController) ServeHTTP(w http.ResponseWriter, r *http.Request) { - fs := http.FileServer(http.Dir("./static")) + fs := http.FileServer(http.Dir(sc.staticDir)) http.StripPrefix(StaticRoute, fs).ServeHTTP(w, r) } diff --git a/bootserver/controllers/ui/ui.go b/bootserver/controllers/ui/ui.go index e4c881f..9284351 100644 --- a/bootserver/controllers/ui/ui.go +++ b/bootserver/controllers/ui/ui.go @@ -36,17 +36,19 @@ type templateData struct { type UIController struct { clientService *services.ClientHandlerService l *logrus.Logger + staticDir string } -func NewUIController(l *logrus.Logger, service *services.ClientHandlerService) *UIController { +func NewUIController(l *logrus.Logger, service *services.ClientHandlerService, staticDir string) *UIController { return &UIController{ clientService: service, l: l, + staticDir: staticDir, } } func (uc *UIController) serveUI(w http.ResponseWriter, r *http.Request) (int, int, error) { - lp := filepath.Join("templates", "index.html") + lp := filepath.Join(uc.staticDir, "templates", "index.html") tmpl, err := template.ParseFiles(lp) if err != nil { return http.StatusInternalServerError, -1, fmt.Errorf("failed to init template: %w", err) diff --git a/bootserver/server/server.go b/bootserver/server/server.go index d0dcf37..5830b4b 100644 --- a/bootserver/server/server.go +++ b/bootserver/server/server.go @@ -70,8 +70,8 @@ func New(appConf *config.AppConfig, logger *logrus.Logger) (*Server, error) { client.EnrollRoute: middlewares.WithLogger(client.NewEnrollController(logger, service, appConf.UDPPort, appConf.UPDMcastGroup), logger), client.ConfigRoute: middlewares.WithLogger(client.NewGetConfigController(logger, service, appConf), logger), client.SetBootRoute: middlewares.WithLogger(client.NewBootController(logger, service), logger), - ui.StaticRoute: &ui.StaticController{}, - ui.UIRoute: middlewares.WithLogger(ui.NewUIController(logger, service), logger), + ui.StaticRoute: middlewares.WithLogger(ui.NewStaticController(appConf.StaticDir), logger), + ui.UIRoute: middlewares.WithLogger(ui.NewUIController(logger, service, appConf.StaticDir), logger), } m := http.NewServeMux()