go: have a dynamic {asset,tmpl}Path
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
* handle errors more specifically when loading assets/tmpls
This commit is contained in:
parent
ba869f9a65
commit
1d421465f5
10
app/app.go
10
app/app.go
@ -17,6 +17,8 @@ type App struct {
|
||||
embeds Embeds
|
||||
config *config.Config
|
||||
version string
|
||||
templatesPath string
|
||||
assetsPath string
|
||||
develMode bool
|
||||
jwtkey string //nolint:unused
|
||||
encryptionKey string //nolint:unused
|
||||
@ -46,6 +48,14 @@ func (a *App) Init(version string, logger *slogging.Logger, conf *config.Config,
|
||||
a.Logger().Info("saving db connection string")
|
||||
a.db = dbclient
|
||||
|
||||
if a.templatesPath == "" {
|
||||
a.templatesPath = "templates"
|
||||
}
|
||||
|
||||
if a.assetsPath == "" {
|
||||
a.assetsPath = "assets"
|
||||
}
|
||||
|
||||
a.initialised = true
|
||||
a.Logger().Info("app initialised")
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"os"
|
||||
@ -12,7 +13,18 @@ func (a *App) getAssets() http.FileSystem {
|
||||
if live {
|
||||
a.logger.Info("assets loaded in live mode")
|
||||
|
||||
return http.FS(os.DirFS("assets"))
|
||||
_, err := os.ReadDir(a.assetsPath)
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
a.Logger().Error("assets dir does not exist")
|
||||
} else if errors.Is(err, os.ErrPermission) {
|
||||
a.Logger().Error("assets dir is not accessible, check permissions")
|
||||
}
|
||||
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return http.FS(os.DirFS(a.assetsPath))
|
||||
}
|
||||
|
||||
a.logger.Info("assets loaded in embed mode")
|
||||
@ -31,13 +43,18 @@ func (a *App) getTemplates() fs.FS {
|
||||
if live {
|
||||
a.logger.Info("templates loaded in live mode")
|
||||
|
||||
_, err := os.ReadDir("templates")
|
||||
_, err := os.ReadDir(a.templatesPath)
|
||||
if err != nil {
|
||||
a.Logger().Error("templates dir does not exist or is not accessible, check permissions")
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
a.Logger().Error("templates dir does not exist")
|
||||
} else if errors.Is(err, os.ErrPermission) {
|
||||
a.Logger().Error("templates dir is not accessible, check permissions")
|
||||
}
|
||||
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return os.DirFS("templates")
|
||||
return os.DirFS(a.templatesPath)
|
||||
}
|
||||
|
||||
a.logger.Info("templates loaded in embed mode")
|
||||
|
@ -15,7 +15,7 @@ func (a *App) SetupRoutes() {
|
||||
tmpls := a.getTemplates()
|
||||
|
||||
// run this before declaring any handler funcs.
|
||||
handlers.InitHandlers(a.version, conf, tmpls)
|
||||
handlers.InitHandlers(a.version, a.templatesPath, conf, tmpls)
|
||||
|
||||
// keep /static/* as a compatibility fallback for /assets.
|
||||
e.GET(
|
||||
|
@ -8,9 +8,10 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
conf *config.Config
|
||||
appver string
|
||||
log *slogging.Logger
|
||||
conf *config.Config
|
||||
appver string
|
||||
log *slogging.Logger
|
||||
tmplPath string
|
||||
)
|
||||
|
||||
func setConfig(c *config.Config) {
|
||||
@ -28,8 +29,9 @@ func setAppVer(v string) {
|
||||
appver = v
|
||||
}
|
||||
|
||||
func InitHandlers(version string, appconf *config.Config, tmpls fs.FS) {
|
||||
func InitHandlers(version, templatesPath string, appconf *config.Config, tmpls fs.FS) {
|
||||
log = slogging.GetLogger()
|
||||
tmplPath = templatesPath
|
||||
|
||||
setConfig(appconf)
|
||||
setAppVer(version)
|
||||
|
@ -26,13 +26,13 @@ var (
|
||||
)
|
||||
|
||||
func listAllTmpls() []string {
|
||||
files, err := filepath.Glob("templates/*.tmpl")
|
||||
files, err := filepath.Glob(tmplPath + "/*.tmpl")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for i, v := range files {
|
||||
files[i] = strings.TrimPrefix(v, "templates/")
|
||||
files[i] = strings.TrimPrefix(v, tmplPath+"/")
|
||||
}
|
||||
|
||||
log.Info("returning files")
|
||||
|
Loading…
Reference in New Issue
Block a user