From aaed272bf27c5e2cf5f990f2d562f09cc199c201 Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Tue, 2 Feb 2021 15:44:30 +0000 Subject: [PATCH] use embed.fs wrapper on data --- config.go | 2 +- embed.py | 23 +++++++++++++---------- main.go | 6 +++--- static.go | 4 ++-- 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/config.go b/config.go index 4c6a48e..4957d99 100644 --- a/config.go +++ b/config.go @@ -15,7 +15,7 @@ var emailEnabled = false func (app *appContext) GetPath(sect, key string) (fs.FS, string) { val := app.config.Section(sect).Key(key).MustString("") if strings.HasPrefix(val, "jfa-go:") { - return localFS, "data/" + strings.TrimPrefix(val, "jfa-go:") + return localFS, strings.TrimPrefix(val, "jfa-go:") } return app.systemFS, val } diff --git a/embed.py b/embed.py index b066c77..80a2a08 100755 --- a/embed.py +++ b/embed.py @@ -19,23 +19,26 @@ import ( ) //go:embed data data/html data/web data/web/css data/web/js -var localFS embed.FS +var loFS embed.FS //go:embed lang/common lang/admin lang/email lang/form lang/setup -var lFS embed.FS +var laFS embed.FS -var langFS LangFS +var langFS rewriteFS +var localFS rewriteFS -type LangFS struct { - fs embed.FS +type rewriteFS struct { + fs embed.FS + prefix string } -func (l LangFS) Open(name string) (fs.File, error) { return l.fs.Open("lang/" + name) } -func (l LangFS) ReadDir(name string) ([]fs.DirEntry, error) { return l.fs.ReadDir("lang/" + name) } -func (l LangFS) ReadFile(name string) ([]byte, error) { return l.fs.ReadFile("lang/" + name) } +func (l rewriteFS) Open(name string) (fs.File, error) { return l.fs.Open(l.prefix + name) } +func (l rewriteFS) ReadDir(name string) ([]fs.DirEntry, error) { return l.fs.ReadDir(l.prefix + name) } +func (l rewriteFS) ReadFile(name string) ([]byte, error) { return l.fs.ReadFile(l.prefix + name) } func loadLocalFS() { - langFS = LangFS{lFS} + langFS = rewriteFS{laFS, "lang/"} + localFS = rewriteFS{loFS, "data/"} log.Println("Using internal storage") }""") elif EMBED in falses: @@ -53,6 +56,6 @@ var langFS fs.FS func loadLocalFS() { log.Println("Using external storage") executable, _ := os.Executable() - localFS = os.DirFS(filepath.Dir(executable)) + localFS = os.DirFS(filepath.Join(filepath.Dir(executable), "data")) langFS = os.DirFS(filepath.Join(filepath.Dir(executable), "data", "lang")) }""") diff --git a/main.go b/main.go index ed3ce47..a5433dc 100644 --- a/main.go +++ b/main.go @@ -84,7 +84,7 @@ type appContext struct { func (app *appContext) loadHTML(router *gin.Engine) { customPath := app.config.Section("files").Key("html_templates").MustString("") - templatePath := "data/html" + templatePath := "html" htmlFiles, err := fs.ReadDir(localFS, templatePath) if err != nil { app.err.Fatalf("Couldn't access template directory: \"%s\"", templatePath) @@ -264,7 +264,7 @@ func start(asDaemon, firstCall bool) { } if _, err := os.Stat(app.configPath); os.IsNotExist(err) { firstRun = true - dConfig, err := fs.ReadFile(localFS, "data/config-default.ini") + dConfig, err := fs.ReadFile(localFS, "config-default.ini") if err != nil { app.err.Fatalf("Couldn't find default config file") } @@ -428,7 +428,7 @@ func start(asDaemon, firstCall bool) { } - app.configBasePath = "data/config-base.json" + app.configBasePath = "config-base.json" configBase, _ := fs.ReadFile(localFS, app.configBasePath) json.Unmarshal(configBase, &app.configBase) diff --git a/static.go b/static.go index 41c1c00..6102d83 100644 --- a/static.go +++ b/static.go @@ -14,12 +14,12 @@ type httpFS struct { } func (f httpFS) Open(name string) (http.File, error) { - return f.hfs.Open("data/web" + name) + return f.hfs.Open("web" + name) } func (f httpFS) Exists(prefix string, filepath string) bool { if p := strings.TrimPrefix(filepath, prefix); len(p) < len(filepath) { - stats, err := fs.Stat(f.fs, "data/web/"+p) + stats, err := fs.Stat(f.fs, "web/"+p) if err != nil { return false }