1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-12-21 16:40:11 +00:00

use embed.fs wrapper on data

This commit is contained in:
Harvey Tindall 2021-02-02 15:44:30 +00:00
parent e6775cd2d1
commit aaed272bf2
Signed by: hrfee
GPG Key ID: BBC65952848FB1A2
4 changed files with 19 additions and 16 deletions

View File

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

View File

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

View File

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

View File

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