pcmt/app/app.go

89 lines
1.9 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
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
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
return
}
a.Logger().Info("setting DevelMode based on a flag - option was already set in the config")
a.develMode = true
}