diff --git a/README.md b/README.md index 2541d3a..34bb0db 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,14 @@ TEMPLATES_PATH=custom-templates ./machine-admin server TEMPLATES_PATH=custom-templates make run-server ``` +Note that running `make runlive` will cause your `TEMPLATES_PATH` environment variable to be ignored, so that the templates directory at `web/templates` (relative to the root of this repository) is always used. + +You can also provide multiple directories delimited by colons, for example: +```bash +TEMPLATES_PATH=~/.local/share/machine-admin/template-overrides:custom-templates ./machine-admin server +``` +Then the directories will be overlaid (like in Linux overlay filesystems) from left to right, with the leftmost directory on top. + #### HTTP Server You can override the default port (`3001`) or base path (`/`) of the HTTP server with the `HTTP_PORT` and `HTTP_BASEPATH` environment variables, respectively. For example, you could run the web server on port 3002 with base path `/admin/panel/` by running the following command: diff --git a/internal/app/server/server.go b/internal/app/server/server.go index 143ae8c..8de1dda 100644 --- a/internal/app/server/server.go +++ b/internal/app/server/server.go @@ -23,6 +23,7 @@ import ( "github.com/openUC2/machine-admin/internal/app/server/routes" "github.com/openUC2/machine-admin/internal/app/server/routes/assets" "github.com/openUC2/machine-admin/internal/app/server/tmplfunc" + "github.com/openUC2/machine-admin/internal/clients/templates" "github.com/openUC2/machine-admin/web" ) @@ -41,7 +42,7 @@ func New(config conf.Config, logger godest.Logger) (s *Server, err error) { } s.Embeds = web.NewEmbeds() - templatesOverlay := &OverlayFS{ + templatesOverlay := &templates.OverlayFS{ Upper: s.Globals.Base.Templates.GetFS(), Lower: s.Embeds.TemplatesFS, } diff --git a/internal/clients/templates/client.go b/internal/clients/templates/client.go index 16637e9..dec2c40 100644 --- a/internal/clients/templates/client.go +++ b/internal/clients/templates/client.go @@ -17,9 +17,25 @@ func NewClient(c Config) *Client { } func (c *Client) GetFS() fs.FS { - path := c.Config.Path - if path == "" { + paths := c.Config.Paths + switch len(paths) { + case 0: return nil + case 1: + return os.DirFS(paths[0]) } - return os.DirFS(path) + + overlay := &OverlayFS{} + suboverlay := overlay + for i, path := range paths { + suboverlay.Upper = os.DirFS(path) + if i+1 == len(paths)-1 { + suboverlay.Lower = os.DirFS(paths[i+1]) + break + } + nextSuboverlay := &OverlayFS{} + suboverlay.Lower = nextSuboverlay + suboverlay = nextSuboverlay + } + return overlay } diff --git a/internal/clients/templates/conf.go b/internal/clients/templates/conf.go index b361585..7aaac3c 100644 --- a/internal/clients/templates/conf.go +++ b/internal/clients/templates/conf.go @@ -1,19 +1,24 @@ package templates import ( + "strings" + "github.com/sargassum-world/godest/env" ) const envPrefix = "TEMPLATES_" type Config struct { - Path string + Paths []string } func GetConfig() (c Config, err error) { // This is a file path specific to ImSwitch OS const defaultPath = "" - c.Path = env.GetString(envPrefix+"PATH", defaultPath) + c.Paths = strings.Split(env.GetString(envPrefix+"PATH", defaultPath), ":") + if len(c.Paths) == 1 && c.Paths[0] == "" { + c.Paths = nil + } return c, nil } diff --git a/internal/app/server/overlayfs-errors.go b/internal/clients/templates/overlayfs-errors.go similarity index 92% rename from internal/app/server/overlayfs-errors.go rename to internal/clients/templates/overlayfs-errors.go index b33b10d..7d66295 100644 --- a/internal/app/server/overlayfs-errors.go +++ b/internal/clients/templates/overlayfs-errors.go @@ -1,4 +1,4 @@ -package server +package templates const ( PathErrorOpLstat = "lstat" diff --git a/internal/app/server/overlayfs-structures.go b/internal/clients/templates/overlayfs-structures.go similarity index 97% rename from internal/app/server/overlayfs-structures.go rename to internal/clients/templates/overlayfs-structures.go index 1985b60..f87c30e 100644 --- a/internal/app/server/overlayfs-structures.go +++ b/internal/clients/templates/overlayfs-structures.go @@ -1,4 +1,4 @@ -package server +package templates import ( "iter" diff --git a/internal/app/server/overlayfs.go b/internal/clients/templates/overlayfs.go similarity index 99% rename from internal/app/server/overlayfs.go rename to internal/clients/templates/overlayfs.go index c8cbd53..7ec90c3 100644 --- a/internal/app/server/overlayfs.go +++ b/internal/clients/templates/overlayfs.go @@ -1,4 +1,4 @@ -package server +package templates import ( "cmp"