saasitone/pkg/services/container.go

162 lines
3.8 KiB
Go

package services
import (
"fmt"
"log/slog"
"github.com/labstack/echo/v4"
_ "github.com/mattn/go-sqlite3"
"github.com/mikestefanello/backlite"
"git.grosinger.net/tgrosinger/saasitone/config"
"git.grosinger.net/tgrosinger/saasitone/pkg/funcmap"
"git.grosinger.net/tgrosinger/saasitone/pkg/log"
)
// Container contains all services used by the application and provides an easy way to handle dependency
// injection including within tests
type Container struct {
// Validator stores a validator
Validator *Validator
// Web stores the web framework
Web *echo.Echo
// Config stores the application configuration
Config *config.Config
// Cache contains the cache client
Cache *CacheClient
// DB is the connection to the database and models for interacting with it
DB *DBClient
// Mail stores an email sending client
Mail *MailClient
// Auth stores an authentication client
Auth *AuthClient
// TemplateRenderer stores a service to easily render and cache templates
TemplateRenderer *TemplateRenderer
// Tasks stores the task client
Tasks *backlite.Client
}
// NewContainer creates and initializes a new Container
func NewContainer() *Container {
c := new(Container)
c.initConfig()
c.initValidator()
c.initWeb()
c.initCache()
c.initDatabase()
c.initAuth()
c.initTemplateRenderer()
c.initMail()
c.initTasks()
return c
}
// Shutdown shuts the Container down and disconnects all connections.
// If the task runner was started, cancel the context to shut it down prior to calling this.
func (c *Container) Shutdown() error {
if err := c.DB.Close(); err != nil {
return err
}
c.Cache.Close()
return nil
}
// initConfig initializes configuration
func (c *Container) initConfig() {
cfg, err := config.GetConfig()
if err != nil {
panic(fmt.Sprintf("failed to load config: %v", err))
}
c.Config = &cfg
// Configure logging
switch cfg.App.Environment {
case config.EnvProduction:
slog.SetLogLoggerLevel(slog.LevelInfo)
default:
slog.SetLogLoggerLevel(slog.LevelDebug)
}
}
// initValidator initializes the validator
func (c *Container) initValidator() {
c.Validator = NewValidator()
}
// initWeb initializes the web framework
func (c *Container) initWeb() {
c.Web = echo.New()
c.Web.HideBanner = true
c.Web.Validator = c.Validator
}
// initCache initializes the cache
func (c *Container) initCache() {
store, err := newInMemoryCache(c.Config.Cache.Capacity)
if err != nil {
panic(err)
}
c.Cache = NewCacheClient(store)
}
// initDatabase initializes the database
func (c *Container) initDatabase() {
client, err := NewDBClient(c.Config)
if err != nil {
panic(err)
}
c.DB = client
}
// initAuth initializes the authentication client
func (c *Container) initAuth() {
c.Auth = NewAuthClient(c.Config, c.DB)
}
// initTemplateRenderer initializes the template renderer
func (c *Container) initTemplateRenderer() {
c.TemplateRenderer = NewTemplateRenderer(c.Config, c.Cache, funcmap.NewFuncMap(c.Web))
}
// initMail initialize the mail client
func (c *Container) initMail() {
var err error
c.Mail, err = NewMailClient(c.Config, c.TemplateRenderer)
if err != nil {
panic(fmt.Sprintf("failed to create mail client: %v", err))
}
}
// initTasks initializes the task client
func (c *Container) initTasks() {
var err error
// You could use a separate database for tasks, if you'd like. but using one
// makes transaction support easier
c.Tasks, err = backlite.NewClient(backlite.ClientConfig{
DB: c.DB.DB(),
Logger: log.Default(),
NumWorkers: c.Config.Tasks.Goroutines,
ReleaseAfter: c.Config.Tasks.ReleaseAfter,
CleanupInterval: c.Config.Tasks.CleanupInterval,
})
if err != nil {
panic(fmt.Sprintf("failed to create task client: %v", err))
}
if err = c.Tasks.Install(); err != nil {
panic(fmt.Sprintf("failed to install task schema: %v", err))
}
}