saasitone/pkg/services/container.go

162 lines
3.8 KiB
Go
Raw Permalink Normal View History

2021-12-18 07:07:12 -08:00
package services
2021-12-03 03:11:01 -08:00
import (
2021-12-05 17:56:38 -08:00
"fmt"
"log/slog"
2021-12-05 17:56:38 -08:00
2021-12-03 03:11:01 -08:00
"github.com/labstack/echo/v4"
2024-07-09 17:57:05 -07:00
_ "github.com/mattn/go-sqlite3"
"github.com/mikestefanello/backlite"
2022-02-10 05:56:07 -08:00
2024-07-09 17:57:05 -07:00
"git.grosinger.net/tgrosinger/saasitone/config"
"git.grosinger.net/tgrosinger/saasitone/pkg/funcmap"
"git.grosinger.net/tgrosinger/saasitone/pkg/log"
2021-12-03 03:11:01 -08:00
)
2021-12-22 16:18:33 -08:00
// Container contains all services used by the application and provides an easy way to handle dependency
// injection including within tests
2021-12-03 03:11:01 -08:00
type Container struct {
// Validator stores a validator
Validator *Validator
2021-12-22 16:18:33 -08:00
// Web stores the web framework
Web *echo.Echo
// Config stores the application configuration
Config *config.Config
// Cache contains the cache client
Cache *CacheClient
2021-12-22 16:18:33 -08:00
2024-07-11 21:09:15 -07:00
// DB is the connection to the database and models for interacting with it
DB *DBClient
2021-12-22 16:18:33 -08:00
// 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
2021-12-03 03:11:01 -08:00
}
2021-12-22 16:18:33 -08:00
// NewContainer creates and initializes a new Container
2021-12-12 18:28:53 -08:00
func NewContainer() *Container {
c := new(Container)
c.initConfig()
c.initValidator()
2021-12-13 09:51:00 -08:00
c.initWeb()
2021-12-12 18:28:53 -08:00
c.initCache()
c.initDatabase()
2021-12-15 06:29:43 -08:00
c.initAuth()
2021-12-19 17:09:01 -08:00
c.initTemplateRenderer()
c.initMail()
c.initTasks()
2021-12-12 18:28:53 -08:00
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.
2021-12-19 10:22:44 -08:00
func (c *Container) Shutdown() error {
2024-07-11 21:09:15 -07:00
if err := c.DB.Close(); err != nil {
2021-12-19 10:22:44 -08:00
return err
}
c.Cache.Close()
2021-12-19 10:22:44 -08:00
return nil
}
2021-12-22 16:18:33 -08:00
// initConfig initializes configuration
2021-12-11 10:18:32 -08:00
func (c *Container) initConfig() {
2021-12-03 03:11:01 -08:00
cfg, err := config.GetConfig()
if err != nil {
2021-12-13 09:51:00 -08:00
panic(fmt.Sprintf("failed to load config: %v", err))
2021-12-03 03:11:01 -08:00
}
c.Config = &cfg
// Configure logging
switch cfg.App.Environment {
case config.EnvProduction:
slog.SetLogLoggerLevel(slog.LevelInfo)
default:
slog.SetLogLoggerLevel(slog.LevelDebug)
}
2021-12-11 10:18:32 -08:00
}
2021-12-03 03:11:01 -08:00
// initValidator initializes the validator
func (c *Container) initValidator() {
c.Validator = NewValidator()
}
2021-12-22 16:18:33 -08:00
// initWeb initializes the web framework
2021-12-13 09:51:00 -08:00
func (c *Container) initWeb() {
c.Web = echo.New()
c.Web.HideBanner = true
c.Web.Validator = c.Validator
2021-12-13 09:51:00 -08:00
}
2021-12-22 16:18:33 -08:00
// initCache initializes the cache
2021-12-11 10:18:32 -08:00
func (c *Container) initCache() {
store, err := newInMemoryCache(c.Config.Cache.Capacity)
if err != nil {
panic(err)
2021-12-05 17:56:38 -08:00
}
c.Cache = NewCacheClient(store)
2021-12-11 10:18:32 -08:00
}
2021-12-22 16:18:33 -08:00
// initDatabase initializes the database
2021-12-11 10:18:32 -08:00
func (c *Container) initDatabase() {
client, err := NewDBClient(c.Config)
2021-12-10 17:44:23 -08:00
if err != nil {
panic(err)
2021-12-10 17:44:23 -08:00
}
2024-07-11 21:09:15 -07:00
c.DB = client
2021-12-11 10:18:32 -08:00
}
2021-12-14 08:29:45 -08:00
2021-12-22 16:18:33 -08:00
// initAuth initializes the authentication client
2021-12-15 06:29:43 -08:00
func (c *Container) initAuth() {
2024-07-11 21:09:15 -07:00
c.Auth = NewAuthClient(c.Config, c.DB)
2021-12-15 06:29:43 -08:00
}
2021-12-19 17:09:01 -08:00
2021-12-22 16:18:33 -08:00
// initTemplateRenderer initializes the template renderer
2021-12-19 17:09:01 -08:00
func (c *Container) initTemplateRenderer() {
c.TemplateRenderer = NewTemplateRenderer(c.Config, c.Cache, funcmap.NewFuncMap(c.Web))
2021-12-19 17:09:01 -08:00
}
2021-12-22 16:18:33 -08:00
// 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))
}
}