use embed.fs wrapper on data

pull/61/head
Harvey Tindall 4 years ago
parent e6775cd2d1
commit aaed272bf2
No known key found for this signature in database
GPG Key ID: BBC65952848FB1A2

@ -15,7 +15,7 @@ var emailEnabled = false
func (app *appContext) GetPath(sect, key string) (fs.FS, string) { func (app *appContext) GetPath(sect, key string) (fs.FS, string) {
val := app.config.Section(sect).Key(key).MustString("") val := app.config.Section(sect).Key(key).MustString("")
if strings.HasPrefix(val, "jfa-go:") { 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 return app.systemFS, val
} }

@ -19,23 +19,26 @@ import (
) )
//go:embed data data/html data/web data/web/css data/web/js //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 //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 { type rewriteFS struct {
fs embed.FS fs embed.FS
prefix string
} }
func (l LangFS) Open(name string) (fs.File, error) { return l.fs.Open("lang/" + name) } func (l rewriteFS) Open(name string) (fs.File, error) { return l.fs.Open(l.prefix + name) }
func (l LangFS) ReadDir(name string) ([]fs.DirEntry, error) { return l.fs.ReadDir("lang/" + name) } func (l rewriteFS) ReadDir(name string) ([]fs.DirEntry, error) { return l.fs.ReadDir(l.prefix + name) }
func (l LangFS) ReadFile(name string) ([]byte, error) { return l.fs.ReadFile("lang/" + name) } func (l rewriteFS) ReadFile(name string) ([]byte, error) { return l.fs.ReadFile(l.prefix + name) }
func loadLocalFS() { func loadLocalFS() {
langFS = LangFS{lFS} langFS = rewriteFS{laFS, "lang/"}
localFS = rewriteFS{loFS, "data/"}
log.Println("Using internal storage") log.Println("Using internal storage")
}""") }""")
elif EMBED in falses: elif EMBED in falses:
@ -53,6 +56,6 @@ var langFS fs.FS
func loadLocalFS() { func loadLocalFS() {
log.Println("Using external storage") log.Println("Using external storage")
executable, _ := os.Executable() 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")) langFS = os.DirFS(filepath.Join(filepath.Dir(executable), "data", "lang"))
}""") }""")

@ -84,7 +84,7 @@ type appContext struct {
func (app *appContext) loadHTML(router *gin.Engine) { func (app *appContext) loadHTML(router *gin.Engine) {
customPath := app.config.Section("files").Key("html_templates").MustString("") customPath := app.config.Section("files").Key("html_templates").MustString("")
templatePath := "data/html" templatePath := "html"
htmlFiles, err := fs.ReadDir(localFS, templatePath) htmlFiles, err := fs.ReadDir(localFS, templatePath)
if err != nil { if err != nil {
app.err.Fatalf("Couldn't access template directory: \"%s\"", templatePath) 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) { if _, err := os.Stat(app.configPath); os.IsNotExist(err) {
firstRun = true firstRun = true
dConfig, err := fs.ReadFile(localFS, "data/config-default.ini") dConfig, err := fs.ReadFile(localFS, "config-default.ini")
if err != nil { if err != nil {
app.err.Fatalf("Couldn't find default config file") 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) configBase, _ := fs.ReadFile(localFS, app.configBasePath)
json.Unmarshal(configBase, &app.configBase) json.Unmarshal(configBase, &app.configBase)

@ -14,12 +14,12 @@ type httpFS struct {
} }
func (f httpFS) Open(name string) (http.File, error) { 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 { func (f httpFS) Exists(prefix string, filepath string) bool {
if p := strings.TrimPrefix(filepath, prefix); len(p) < len(filepath) { 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 { if err != nil {
return false return false
} }

Loading…
Cancel
Save