mirror of
https://github.com/hrfee/jfa-go.git
synced 2024-12-28 20:10:11 +00:00
use embed.fs wrapper on data
This commit is contained in:
parent
bfab71c24b
commit
d9354c7d6b
@ -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
|
||||
}
|
||||
|
23
embed.py
23
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"))
|
||||
}""")
|
||||
|
6
main.go
6
main.go
@ -83,7 +83,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)
|
||||
@ -263,7 +263,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")
|
||||
}
|
||||
@ -427,7 +427,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)
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user