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"`
} `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) {

View file

@ -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)
}

View file

@ -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)

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.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()