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
|
|
|
"context"
|
2021-12-10 17:44:23 -08:00
|
|
|
"database/sql"
|
2021-12-05 17:56:38 -08:00
|
|
|
"fmt"
|
2024-06-14 18:01:48 -07:00
|
|
|
"log/slog"
|
2021-12-05 17:56:38 -08:00
|
|
|
|
2021-12-10 17:44:23 -08:00
|
|
|
"entgo.io/ent/dialect"
|
|
|
|
entsql "entgo.io/ent/dialect/sql"
|
2022-01-28 05:45:16 -08:00
|
|
|
"entgo.io/ent/dialect/sql/schema"
|
2022-02-10 05:56:07 -08:00
|
|
|
|
|
|
|
// Required by ent
|
2021-12-10 17:44:23 -08:00
|
|
|
_ "github.com/jackc/pgx/v4/stdlib"
|
2021-12-03 03:11:01 -08:00
|
|
|
"github.com/labstack/echo/v4"
|
2022-01-01 07:44:18 -08:00
|
|
|
"github.com/mikestefanello/pagoda/config"
|
|
|
|
"github.com/mikestefanello/pagoda/ent"
|
2022-02-10 05:56:07 -08:00
|
|
|
|
|
|
|
// Require by ent
|
2022-01-01 07:44:18 -08:00
|
|
|
_ "github.com/mikestefanello/pagoda/ent/runtime"
|
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 {
|
2021-12-24 21:11:59 -08:00
|
|
|
// 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
|
|
|
|
|
2022-01-13 18:13:41 -08:00
|
|
|
// Cache contains the cache client
|
|
|
|
Cache *CacheClient
|
2021-12-22 16:18:33 -08:00
|
|
|
|
|
|
|
// Database stores the connection to the database
|
|
|
|
Database *sql.DB
|
|
|
|
|
|
|
|
// ORM stores a client to the ORM
|
|
|
|
ORM *ent.Client
|
|
|
|
|
|
|
|
// 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
|
2021-12-21 08:00:32 -08:00
|
|
|
TemplateRenderer *TemplateRenderer
|
2022-02-02 18:24:52 -08:00
|
|
|
|
|
|
|
// Tasks stores the task client
|
|
|
|
Tasks *TaskClient
|
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()
|
2021-12-24 21:11:59 -08:00
|
|
|
c.initValidator()
|
2021-12-13 09:51:00 -08:00
|
|
|
c.initWeb()
|
2021-12-12 18:28:53 -08:00
|
|
|
c.initCache()
|
|
|
|
c.initDatabase()
|
|
|
|
c.initORM()
|
2021-12-15 06:29:43 -08:00
|
|
|
c.initAuth()
|
2021-12-19 17:09:01 -08:00
|
|
|
c.initTemplateRenderer()
|
2021-12-19 17:37:51 -08:00
|
|
|
c.initMail()
|
2022-02-02 18:24:52 -08:00
|
|
|
c.initTasks()
|
2021-12-12 18:28:53 -08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2021-12-22 16:18:33 -08:00
|
|
|
// Shutdown shuts the Container down and disconnects all connections
|
2021-12-19 10:22:44 -08:00
|
|
|
func (c *Container) Shutdown() error {
|
2022-02-06 07:07:25 -08:00
|
|
|
if err := c.Tasks.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-13 18:13:41 -08:00
|
|
|
if err := c.Cache.Close(); err != nil {
|
2021-12-19 10:22:44 -08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.ORM.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.Database.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2024-06-14 18:01:48 -07:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2021-12-24 21:11:59 -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()
|
2024-06-14 18:01:48 -07:00
|
|
|
c.Web.HideBanner = true
|
2021-12-24 21:11:59 -08:00
|
|
|
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() {
|
2022-01-13 18:13:41 -08:00
|
|
|
var err error
|
2022-02-06 07:07:25 -08:00
|
|
|
if c.Cache, err = NewCacheClient(c.Config); err != nil {
|
2022-01-13 18:13:41 -08:00
|
|
|
panic(err)
|
2021-12-05 17:56:38 -08:00
|
|
|
}
|
2021-12-11 10:18:32 -08:00
|
|
|
}
|
|
|
|
|
2021-12-22 16:18:33 -08:00
|
|
|
// initDatabase initializes the database
|
|
|
|
// If the environment is set to test, the test database will be used and will be dropped, recreated and migrated
|
2021-12-11 10:18:32 -08:00
|
|
|
func (c *Container) initDatabase() {
|
|
|
|
var err error
|
2021-12-05 17:56:38 -08:00
|
|
|
|
2021-12-13 09:51:00 -08:00
|
|
|
getAddr := func(dbName string) string {
|
2022-02-08 17:21:05 -08:00
|
|
|
return fmt.Sprintf("postgresql://%s:%s@%s:%d/%s",
|
2021-12-13 09:51:00 -08:00
|
|
|
c.Config.Database.User,
|
|
|
|
c.Config.Database.Password,
|
|
|
|
c.Config.Database.Hostname,
|
2022-02-08 17:21:05 -08:00
|
|
|
c.Config.Database.Port,
|
2021-12-13 09:51:00 -08:00
|
|
|
dbName,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Database, err = sql.Open("pgx", getAddr(c.Config.Database.Database))
|
2021-12-10 17:44:23 -08:00
|
|
|
if err != nil {
|
2021-12-13 09:51:00 -08:00
|
|
|
panic(fmt.Sprintf("failed to connect to database: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if this is a test environment
|
|
|
|
if c.Config.App.Environment == config.EnvTest {
|
|
|
|
// Drop the test database, ignoring errors in case it doesn't yet exist
|
|
|
|
_, _ = c.Database.Exec("DROP DATABASE " + c.Config.Database.TestDatabase)
|
|
|
|
|
|
|
|
// Create the test database
|
|
|
|
if _, err = c.Database.Exec("CREATE DATABASE " + c.Config.Database.TestDatabase); err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to create test database: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect to the test database
|
|
|
|
if err = c.Database.Close(); err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to close database connection: %v", err))
|
|
|
|
}
|
|
|
|
c.Database, err = sql.Open("pgx", getAddr(c.Config.Database.TestDatabase))
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to connect to database: %v", err))
|
|
|
|
}
|
2021-12-10 17:44:23 -08:00
|
|
|
}
|
2021-12-11 10:18:32 -08:00
|
|
|
}
|
2021-12-10 17:44:23 -08:00
|
|
|
|
2021-12-22 16:18:33 -08:00
|
|
|
// initORM initializes the ORM
|
2021-12-11 10:18:32 -08:00
|
|
|
func (c *Container) initORM() {
|
2021-12-10 17:44:23 -08:00
|
|
|
drv := entsql.OpenDB(dialect.Postgres, c.Database)
|
2021-12-11 10:18:32 -08:00
|
|
|
c.ORM = ent.NewClient(ent.Driver(drv))
|
2022-01-28 05:45:16 -08:00
|
|
|
if err := c.ORM.Schema.Create(context.Background(), schema.WithAtlas(true)); err != nil {
|
2021-12-13 09:51:00 -08:00
|
|
|
panic(fmt.Sprintf("failed to create database schema: %v", err))
|
2021-12-10 17:44:23 -08:00
|
|
|
}
|
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() {
|
2021-12-17 17:58:51 -08:00
|
|
|
c.Auth = NewAuthClient(c.Config, c.ORM)
|
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() {
|
2024-06-14 09:35:35 -07:00
|
|
|
c.TemplateRenderer = NewTemplateRenderer(c.Config, c.Web)
|
2021-12-19 17:09:01 -08:00
|
|
|
}
|
2021-12-19 17:37:51 -08:00
|
|
|
|
2021-12-22 16:18:33 -08:00
|
|
|
// initMail initialize the mail client
|
2021-12-19 17:37:51 -08:00
|
|
|
func (c *Container) initMail() {
|
|
|
|
var err error
|
2021-12-21 08:00:32 -08:00
|
|
|
c.Mail, err = NewMailClient(c.Config, c.TemplateRenderer)
|
2021-12-19 17:37:51 -08:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to create mail client: %v", err))
|
|
|
|
}
|
|
|
|
}
|
2022-02-02 18:24:52 -08:00
|
|
|
|
|
|
|
// initTasks initializes the task client
|
|
|
|
func (c *Container) initTasks() {
|
2022-02-06 07:07:25 -08:00
|
|
|
c.Tasks = NewTaskClient(c.Config)
|
2022-02-02 18:24:52 -08:00
|
|
|
}
|