pcmt/app/app.go

93 lines
1.9 KiB
Go
Raw Normal View History

package app
import (
"embed"
"errors"
"log"
"os"
"git.dotya.ml/mirre-mt/pcmt/config"
"git.dotya.ml/mirre-mt/pcmt/ent"
"github.com/labstack/echo/v4"
)
type App struct {
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
}
// Init allows setting App's important fields - once.
func (a *App) Init(version string, conf *config.Config, dbclient *ent.Client) error {
if !a.initialised {
e := echo.New()
a.e = e
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
a.initialised = true
return nil
}
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
}
// 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
// }