allow setting path to static files in config

This commit is contained in:
Melora Hugues 2024-03-24 13:37:40 +01:00
parent e730e40a4a
commit b1e9a2099e
4 changed files with 18 additions and 6 deletions

View file

@ -43,7 +43,9 @@ type jsonConf struct {
Level string `json:"level"` Level string `json:"level"`
} `json:"log"` } `json:"log"`
Storage struct { Storage struct {
Path string `json:"path"` Path string `json:"path"`
TemplateDir string `json:"templatePath"`
StaticDir string `json:"staticPath"`
} `json:"storage"` } `json:"storage"`
Server struct { Server struct {
Host string `json:"host"` Host string `json:"host"`
@ -63,6 +65,7 @@ type AppConfig struct {
LogLevel logrus.Level LogLevel logrus.Level
ServerMode ListeningMode ServerMode ListeningMode
DataFilepath string DataFilepath string
StaticDir string
Host string Host string
Port int Port int
SockPath string SockPath string
@ -101,6 +104,7 @@ func (ac *AppConfig) UnmarshalJSON(data []byte) error {
ac.UDPPort = jsonConf.BootProvider.Port ac.UDPPort = jsonConf.BootProvider.Port
ac.UDPSrcAddr = jsonConf.BootProvider.SrcAddr ac.UDPSrcAddr = jsonConf.BootProvider.SrcAddr
ac.DataFilepath = jsonConf.Storage.Path ac.DataFilepath = jsonConf.Storage.Path
ac.StaticDir = jsonConf.Storage.StaticDir
return nil return nil
} }
@ -113,6 +117,7 @@ var defaultConfig AppConfig = AppConfig{
UPDMcastGroup: "ff02::abcd:1234", UPDMcastGroup: "ff02::abcd:1234",
UDPPort: 42, UDPPort: 42,
UDPIface: "eth0", UDPIface: "eth0",
StaticDir: "./static",
} }
func New(filepath string) (*AppConfig, error) { func New(filepath string) (*AppConfig, error) {

View file

@ -7,9 +7,14 @@ import (
const StaticRoute = "/static/" const StaticRoute = "/static/"
type StaticController struct { type StaticController struct {
staticDir string
}
func NewStaticController(staticDir string) *StaticController {
return &StaticController{staticDir: staticDir}
} }
func (sc *StaticController) ServeHTTP(w http.ResponseWriter, r *http.Request) { 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) http.StripPrefix(StaticRoute, fs).ServeHTTP(w, r)
} }

View file

@ -36,17 +36,19 @@ type templateData struct {
type UIController struct { type UIController struct {
clientService *services.ClientHandlerService clientService *services.ClientHandlerService
l *logrus.Logger 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{ return &UIController{
clientService: service, clientService: service,
l: l, l: l,
staticDir: staticDir,
} }
} }
func (uc *UIController) serveUI(w http.ResponseWriter, r *http.Request) (int, int, error) { 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) tmpl, err := template.ParseFiles(lp)
if err != nil { if err != nil {
return http.StatusInternalServerError, -1, fmt.Errorf("failed to init template: %w", err) return http.StatusInternalServerError, -1, fmt.Errorf("failed to init template: %w", err)

View file

@ -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.EnrollRoute: middlewares.WithLogger(client.NewEnrollController(logger, service, appConf.UDPPort, appConf.UPDMcastGroup), logger),
client.ConfigRoute: middlewares.WithLogger(client.NewGetConfigController(logger, service, appConf), logger), client.ConfigRoute: middlewares.WithLogger(client.NewGetConfigController(logger, service, appConf), logger),
client.SetBootRoute: middlewares.WithLogger(client.NewBootController(logger, service), logger), client.SetBootRoute: middlewares.WithLogger(client.NewBootController(logger, service), logger),
ui.StaticRoute: &ui.StaticController{}, ui.StaticRoute: middlewares.WithLogger(ui.NewStaticController(appConf.StaticDir), logger),
ui.UIRoute: middlewares.WithLogger(ui.NewUIController(logger, service), logger), ui.UIRoute: middlewares.WithLogger(ui.NewUIController(logger, service, appConf.StaticDir), logger),
} }
m := http.NewServeMux() m := http.NewServeMux()