2023-03-22 22:13:23 +01:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2023-04-13 00:07:08 +02:00
|
|
|
"embed"
|
2023-03-22 22:13:23 +01:00
|
|
|
|
2023-05-03 02:18:29 +02:00
|
|
|
"git.dotya.ml/mirre-mt/pcmt/app/settings"
|
2023-04-13 00:07:08 +02:00
|
|
|
"git.dotya.ml/mirre-mt/pcmt/ent"
|
2023-04-19 05:30:52 +02:00
|
|
|
"git.dotya.ml/mirre-mt/pcmt/slogging"
|
2023-03-22 22:13:23 +01:00
|
|
|
"github.com/labstack/echo/v4"
|
2023-05-11 04:48:24 +02:00
|
|
|
gommonlog "github.com/labstack/gommon/log"
|
2023-05-09 17:35:00 +02:00
|
|
|
"golang.org/x/exp/slog"
|
2023-03-22 22:13:23 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type App struct {
|
2023-04-13 00:07:08 +02:00
|
|
|
e *echo.Echo
|
2023-05-11 17:06:20 +02:00
|
|
|
slogger *slogging.Slogger
|
|
|
|
logger slogging.Slogger
|
2023-04-13 00:07:08 +02:00
|
|
|
initialised bool
|
|
|
|
embeds Embeds
|
2023-04-28 23:16:58 +02:00
|
|
|
templatesPath string
|
|
|
|
assetsPath string
|
2023-05-03 02:18:29 +02:00
|
|
|
setting *settings.Settings
|
2023-04-19 21:46:56 +02:00
|
|
|
jwtkey string //nolint:unused
|
|
|
|
encryptionKey string //nolint:unused
|
|
|
|
dbConnect string //nolint:unused
|
2023-04-13 00:07:08 +02:00
|
|
|
db *ent.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
type Embeds struct {
|
|
|
|
templates embed.FS
|
|
|
|
assets embed.FS
|
2023-03-22 22:13:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Init allows setting App's important fields - once.
|
2023-05-11 17:06:20 +02:00
|
|
|
func (a *App) Init(s *settings.Settings, logger *slogging.Slogger, dbclient *ent.Client) error {
|
2023-03-22 22:13:23 +01:00
|
|
|
if !a.initialised {
|
|
|
|
e := echo.New()
|
|
|
|
|
|
|
|
a.e = e
|
2023-05-09 17:35:00 +02:00
|
|
|
a.slogger = logger
|
|
|
|
a.logger = *a.slogger
|
|
|
|
a.logger.Logger = a.logger.Logger.With(
|
|
|
|
slog.Group("pcmt extra", slog.String("module", "app")),
|
|
|
|
)
|
2023-04-13 00:07:08 +02:00
|
|
|
|
2023-05-03 02:18:29 +02:00
|
|
|
a.setting = s
|
2023-04-13 00:07:08 +02:00
|
|
|
|
2023-05-03 02:18:29 +02:00
|
|
|
if a.setting.IsDevel() {
|
|
|
|
a.setDevel()
|
|
|
|
}
|
|
|
|
|
|
|
|
a.Logger().Infof("app version: %s", a.setting.Version())
|
2023-04-13 00:07:08 +02:00
|
|
|
|
2023-05-04 16:35:41 +02:00
|
|
|
a.Logger().Debug("saving db client object")
|
2023-04-13 00:07:08 +02:00
|
|
|
a.db = dbclient
|
|
|
|
|
2023-04-28 23:16:58 +02:00
|
|
|
if a.templatesPath == "" {
|
|
|
|
a.templatesPath = "templates"
|
2023-05-03 02:18:29 +02:00
|
|
|
a.setting.SetTemplatesPath("templates")
|
|
|
|
} else {
|
|
|
|
a.setting.SetTemplatesPath(a.templatesPath)
|
2023-04-28 23:16:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if a.assetsPath == "" {
|
2023-05-11 04:45:33 +02:00
|
|
|
a.Logger().Debug("setting assets path as unset")
|
2023-05-08 00:13:33 +02:00
|
|
|
a.assetsPath = "assets/public"
|
2023-05-03 02:18:29 +02:00
|
|
|
a.setting.SetAssetsPath("assets")
|
|
|
|
} else {
|
|
|
|
a.setting.SetAssetsPath(a.assetsPath)
|
2023-04-28 23:16:58 +02:00
|
|
|
}
|
|
|
|
|
2023-03-22 22:13:23 +01:00
|
|
|
a.initialised = true
|
2023-04-19 05:30:52 +02:00
|
|
|
a.Logger().Info("app initialised")
|
2023-03-22 22:13:23 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-04-13 00:07:08 +02:00
|
|
|
|
2023-05-04 16:38:15 +02:00
|
|
|
return ErrAppAlreadyInitialised
|
2023-03-22 22:13:23 +01:00
|
|
|
}
|
|
|
|
|
2023-04-19 05:30:52 +02:00
|
|
|
// E returns app's *echo.Echo instance.
|
2023-03-22 22:13:23 +01:00
|
|
|
func (a *App) E() *echo.Echo {
|
|
|
|
return a.e
|
|
|
|
}
|
|
|
|
|
2023-05-09 17:35:00 +02:00
|
|
|
// Logger returns app's pointer to the global logger instance.
|
2023-05-11 17:06:20 +02:00
|
|
|
func (a *App) Logger() *slogging.Slogger {
|
2023-05-11 04:42:38 +02:00
|
|
|
return &a.logger
|
2023-03-22 22:13:23 +01:00
|
|
|
}
|
2023-04-13 00:07:08 +02:00
|
|
|
|
2023-05-03 02:18:29 +02:00
|
|
|
// PrintConfiguration outputs relevant settings of the application to console.
|
|
|
|
func (a *App) PrintConfiguration() {
|
|
|
|
if a.setting != nil {
|
|
|
|
if a.setting.IsLive() {
|
2023-05-09 17:35:00 +02:00
|
|
|
a.Logger().Debug("live mode enabled")
|
2023-05-03 02:18:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if a.setting.IsDevel() {
|
2023-05-09 17:35:00 +02:00
|
|
|
a.Logger().Debug("devel mode enabled - make sure that browser-sync is running")
|
2023-05-03 02:18:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-04 16:38:15 +02:00
|
|
|
panic(ErrAppSettingsUnset)
|
2023-05-03 02:18:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) SetSettings(s *settings.Settings) {
|
|
|
|
a.setting = s
|
|
|
|
}
|
|
|
|
|
2023-04-13 00:07:08 +02:00
|
|
|
// SetEmbeds saves the embedded files to application state.
|
|
|
|
func (a *App) SetEmbeds(templates, assets embed.FS) {
|
2023-05-11 04:45:33 +02:00
|
|
|
a.Logger().Debug("saving embeds in app struct")
|
2023-04-13 00:07:08 +02:00
|
|
|
a.embeds.templates = templates
|
|
|
|
a.embeds.assets = assets
|
|
|
|
}
|
|
|
|
|
2023-05-03 02:18:29 +02:00
|
|
|
// setDevel puts the app in devel mode, which loads a browser-sync script in
|
2023-04-13 00:07:08 +02:00
|
|
|
// templates and expects browser-sync running.
|
2023-05-03 02:18:29 +02:00
|
|
|
func (a *App) setDevel() {
|
2023-05-09 17:35:00 +02:00
|
|
|
a.Logger().Debug("enabling debug logging for Echo")
|
2023-04-28 23:05:32 +02:00
|
|
|
a.e.Debug = true
|
2023-05-11 04:48:24 +02:00
|
|
|
a.e.Logger.SetLevel(gommonlog.DEBUG)
|
2023-04-13 00:07:08 +02:00
|
|
|
}
|