pcmt/app/app.go
leo f129606b8f
All checks were successful
continuous-integration/drone/push Build is passing
add bulk changes
* add handlers for signin,singup,logout...
* introduce ent ORM and add user schema
* add live mode, devel mode to selectively turn on features via
  config/flags
* add templates, handle embedding moar smarter:
  * live mode uses live folder structure, else embedded templates are
    used
* start using tailwindcss to style stuff
* add development goodies for hot-reloading (browser-sync - bs.js)
* pimp-up config.dhall with actual custom config Type (enables remote
  schema and local values only as needed)
* add justfile (alternative to makefile for process automation)
2023-04-13 00:07:08 +02:00

93 lines
1.9 KiB
Go

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
// }