leo
1d421465f5
All checks were successful
continuous-integration/drone/push Build is passing
* handle errors more specifically when loading assets/tmpls
103 lines
2.2 KiB
Go
103 lines
2.2 KiB
Go
package app
|
|
|
|
import (
|
|
"embed"
|
|
"errors"
|
|
|
|
"git.dotya.ml/mirre-mt/pcmt/config"
|
|
"git.dotya.ml/mirre-mt/pcmt/ent"
|
|
"git.dotya.ml/mirre-mt/pcmt/slogging"
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
type App struct {
|
|
e *echo.Echo
|
|
logger *slogging.Logger
|
|
initialised bool
|
|
embeds Embeds
|
|
config *config.Config
|
|
version string
|
|
templatesPath string
|
|
assetsPath string
|
|
develMode bool
|
|
jwtkey string //nolint:unused
|
|
encryptionKey string //nolint:unused
|
|
dbConnect string //nolint:unused
|
|
db *ent.Client
|
|
}
|
|
|
|
type Embeds struct {
|
|
templates embed.FS
|
|
assets embed.FS
|
|
}
|
|
|
|
// Init allows setting App's important fields - once.
|
|
func (a *App) Init(version string, logger *slogging.Logger, conf *config.Config, dbclient *ent.Client) error {
|
|
if !a.initialised {
|
|
e := echo.New()
|
|
|
|
a.e = e
|
|
a.logger = logger
|
|
|
|
a.Logger().Infof("app version: %s", version)
|
|
a.version = version
|
|
|
|
a.config = conf
|
|
a.Logger().Infof("app config set to %#v", a.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")
|
|
|
|
return nil
|
|
}
|
|
|
|
return errors.New("ErrAppAlreadyInitialised")
|
|
}
|
|
|
|
// E returns app's *echo.Echo instance.
|
|
func (a *App) E() *echo.Echo {
|
|
return a.e
|
|
}
|
|
|
|
// Logger returns app's logger instance.
|
|
func (a *App) Logger() *slogging.Logger {
|
|
return a.logger
|
|
}
|
|
|
|
// SetEmbeds saves the embedded files to application state.
|
|
func (a *App) SetEmbeds(templates, assets embed.FS) {
|
|
a.logger.Info("setting embeds")
|
|
a.embeds.templates = templates
|
|
a.embeds.assets = assets
|
|
}
|
|
|
|
// SetDevel puts the app in devel mode, which loads a browser-sync script in
|
|
// templates and expects browser-sync running.
|
|
func (a *App) SetDevel() {
|
|
if !a.Config().DevelMode {
|
|
a.Logger().Info("overriding configuration value of DevelMode based on a flag")
|
|
|
|
a.Config().DevelMode = true
|
|
} else {
|
|
a.Logger().Info("DevelMode already set in the config")
|
|
}
|
|
|
|
a.develMode = true
|
|
a.Logger().Info("enabling debug logging for Echo")
|
|
a.e.Debug = true
|
|
|
|
a.logger = slogging.SetLevel(slogging.LevelDebug)
|
|
a.Logger().Debug("enabled debug logging for the default logger")
|
|
}
|