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
|
|
|
"errors"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
2023-04-13 00:07:08 +02:00
|
|
|
"git.dotya.ml/mirre-mt/pcmt/config"
|
|
|
|
"git.dotya.ml/mirre-mt/pcmt/ent"
|
2023-03-22 22:13:23 +01:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
type App struct {
|
2023-04-13 00:07:08 +02:00
|
|
|
e *echo.Echo
|
|
|
|
logger *log.Logger
|
|
|
|
initialised bool
|
|
|
|
embeds Embeds
|
|
|
|
config *config.Config
|
|
|
|
version string
|
|
|
|
develMode bool
|
|
|
|
jwtkey string
|
|
|
|
encryptionKey string
|
|
|
|
dbConnect string
|
|
|
|
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-04-13 00:07:08 +02:00
|
|
|
func (a *App) Init(version string, conf *config.Config, dbclient *ent.Client) error {
|
2023-03-22 22:13:23 +01:00
|
|
|
if !a.initialised {
|
|
|
|
e := echo.New()
|
|
|
|
|
|
|
|
a.e = e
|
2023-04-13 00:07:08 +02:00
|
|
|
a.logger = log.New(os.Stderr, "*** pcmt:", log.Ldate|log.Ltime|log.Lshortfile)
|
|
|
|
|
|
|
|
a.Logger().Printf("app version: %s", version)
|
|
|
|
a.version = version
|
|
|
|
|
|
|
|
a.Logger().Printf("setting app config to %#v", conf)
|
|
|
|
a.config = conf
|
|
|
|
a.Logger().Printf("app config set to %#v", a.config)
|
|
|
|
|
|
|
|
a.Logger().Print("saving client conn to db")
|
|
|
|
a.db = dbclient
|
|
|
|
|
2023-03-22 22:13:23 +01:00
|
|
|
a.initialised = true
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-04-13 00:07:08 +02:00
|
|
|
|
2023-03-22 22:13:23 +01:00
|
|
|
return errors.New("ErrAppAlreadyInitialised")
|
|
|
|
}
|
|
|
|
|
|
|
|
// E returns app's *echo.Echo.
|
|
|
|
func (a *App) E() *echo.Echo {
|
|
|
|
return a.e
|
|
|
|
}
|
|
|
|
|
|
|
|
// Logger returns app's *log.Logger.
|
|
|
|
func (a *App) Logger() *log.Logger {
|
|
|
|
return a.logger
|
|
|
|
}
|
2023-04-13 00:07:08 +02:00
|
|
|
|
|
|
|
// SetEmbeds saves the embedded files to application state.
|
|
|
|
func (a *App) SetEmbeds(templates, assets embed.FS) {
|
|
|
|
a.embeds.templates = templates
|
|
|
|
a.embeds.assets = assets
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDevel puts the app in devel mode, which loads browser-sync script in
|
|
|
|
// templates and expects browser-sync running.
|
|
|
|
func (a *App) SetDevel() {
|
|
|
|
if !a.Config().DevelMode {
|
|
|
|
a.Logger().Print("overriding configuration value of DevelMode based on a flag")
|
|
|
|
a.Config().DevelMode = true
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a.Logger().Print("setting DevelMode based on a flag")
|
|
|
|
a.develMode = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// func (a *App) SetVersion(version string) {
|
|
|
|
// a.Logger().Printf("setting app version to %s", version)
|
|
|
|
// a.version = version
|
|
|
|
// }
|