Cleanup container init.

This commit is contained in:
mikestefanello 2021-12-11 13:18:32 -05:00
parent 8657380530
commit e5038f7bbf
3 changed files with 27 additions and 16 deletions

View File

@ -22,34 +22,36 @@ type Container struct {
Config *config.Config
Cache *cache.Cache
Database *sql.DB
Ent *ent.Client
ORM *ent.Client
}
func NewContainer() *Container {
var c Container
// Web
func (c *Container) initWeb() {
c.Web = echo.New()
}
// Configuration
func (c *Container) initConfig() {
cfg, err := config.GetConfig()
if err != nil {
c.Web.Logger.Fatalf("failed to load configuration: %v", err)
}
c.Config = &cfg
}
// Cache
func (c *Container) initCache() {
cacheClient := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", c.Config.Cache.Hostname, c.Config.Cache.Port),
Password: c.Config.Cache.Password,
})
if _, err = cacheClient.Ping(context.Background()).Result(); err != nil {
if _, err := cacheClient.Ping(context.Background()).Result(); err != nil {
c.Web.Logger.Fatalf("failed to connect to cache server: %v", err)
}
cacheStore := store.NewRedis(cacheClient, nil)
c.Cache = cache.New(cacheStore)
}
func (c *Container) initDatabase() {
var err error
// Database
addr := fmt.Sprintf("postgresql://%s:%s@%s/%s",
c.Config.Database.User,
c.Config.Database.Password,
@ -60,13 +62,22 @@ func NewContainer() *Container {
if err != nil {
c.Web.Logger.Fatalf("failed to connect to database: %v", err)
}
}
// Ent
func (c *Container) initORM() {
drv := entsql.OpenDB(dialect.Postgres, c.Database)
c.Ent = ent.NewClient(ent.Driver(drv))
if err := c.Ent.Schema.Create(context.Background()); err != nil {
c.ORM = ent.NewClient(ent.Driver(drv))
if err := c.ORM.Schema.Create(context.Background()); err != nil {
c.Web.Logger.Fatalf("failed to create database schema: %v", err)
}
return &c
}
func NewContainer() *Container {
c := new(Container)
c.initWeb()
c.initConfig()
c.initCache()
c.initDatabase()
c.initORM()
return c
}

View File

@ -31,7 +31,7 @@ func (l *Login) Post(c echo.Context) error {
return l.Get(c)
}
u, err := l.Container.Ent.User.
u, err := l.Container.ORM.User.
Query().
Where(user.Username(name)).
First(c.Request().Context())

View File

@ -56,7 +56,7 @@ func (r *Register) Post(c echo.Context) error {
}
// Attempt creating the user
u, err := r.Container.Ent.User.
u, err := r.Container.ORM.User.
Create().
SetUsername(form.Username).
SetPassword(string(pwHash)).