fix: allow setting base path for static files

This commit is contained in:
Melora Hugues 2024-05-08 16:07:40 +02:00
parent db665a30fa
commit 9cf1428517
4 changed files with 29 additions and 16 deletions

View file

@ -15,22 +15,31 @@ import (
const StaticRoute = "/static/"
type StaticController struct {
baseDir string
}
func NewStaticController(baseDir string) *StaticController {
return &StaticController{
baseDir: baseDir,
}
}
func (sc *StaticController) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fs := http.FileServer(http.Dir("./static"))
fs := http.FileServer(http.Dir(sc.baseDir + "/static"))
http.StripPrefix(StaticRoute, fs).ServeHTTP(w, r)
}
type IndexController struct {
l *logrus.Logger
downstreamConstroller http.Handler
baseDir string
}
func NewIndexController(l *logrus.Logger, downstream http.Handler) *IndexController {
func NewIndexController(l *logrus.Logger, downstream http.Handler, baseDir string) *IndexController {
return &IndexController{
l: l,
downstreamConstroller: downstream,
baseDir: baseDir,
}
}
@ -39,9 +48,9 @@ func (ic IndexController) serveUI(w http.ResponseWriter, r *http.Request) (int,
"issuer": func() string { return "toto" },
}
lp := filepath.Join("templates", "index.html")
hdrTpl := filepath.Join("templates", "header.html")
footTpl := filepath.Join("templates", "footer.html")
lp := filepath.Join(ic.baseDir, "templates", "index.html")
hdrTpl := filepath.Join(ic.baseDir, "templates", "header.html")
footTpl := filepath.Join(ic.baseDir, "templates", "footer.html")
tmpl, err := template.New("index.html").Funcs(funcs).ParseFiles(hdrTpl, footTpl, lp)
if err != nil {
return http.StatusInternalServerError, -1, fmt.Errorf("failed to init template: %w", err)