Misc cleanup.

This commit is contained in:
mikestefanello 2021-12-22 19:18:33 -05:00
parent 00515185cd
commit 3b41e1dfd8
4 changed files with 58 additions and 24 deletions

View file

@ -104,12 +104,12 @@ func TestDeletePasswordTokens(t *testing.T) {
} }
func TestRandomToken(t *testing.T) { func TestRandomToken(t *testing.T) {
length := 64 length := c.Config.App.PasswordToken.Length
a, err := c.Auth.RandomToken(length) a, err := c.Auth.RandomToken(length)
require.NoError(t, err) require.NoError(t, err)
b, err := c.Auth.RandomToken(length) b, err := c.Auth.RandomToken(length)
require.NoError(t, err) require.NoError(t, err)
assert.Len(t, a, 64) assert.Len(t, a, length)
assert.Len(t, b, 64) assert.Len(t, b, length)
assert.NotEqual(t, a, b) assert.NotEqual(t, a, b)
} }

View file

@ -18,18 +18,38 @@ import (
"goweb/ent" "goweb/ent"
) )
// Container contains all services used by the application and provides an easy way to handle dependency
// injection including within tests
type Container struct { type Container struct {
Web *echo.Echo // Web stores the web framework
Config *config.Config Web *echo.Echo
Cache *cache.Cache
cacheClient *redis.Client // Config stores the application configuration
Database *sql.DB Config *config.Config
ORM *ent.Client
Mail *MailClient // Cache contains the cache interface
Auth *AuthClient Cache *cache.Cache
// cacheClient stores the client to the underlying cache service
cacheClient *redis.Client
// 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
TemplateRenderer *TemplateRenderer TemplateRenderer *TemplateRenderer
} }
// NewContainer creates and initializes a new Container
func NewContainer() *Container { func NewContainer() *Container {
c := new(Container) c := new(Container)
c.initConfig() c.initConfig()
@ -43,6 +63,7 @@ func NewContainer() *Container {
return c return c
} }
// Shutdown shuts the Container down and disconnects all connections
func (c *Container) Shutdown() error { func (c *Container) Shutdown() error {
if err := c.cacheClient.Close(); err != nil { if err := c.cacheClient.Close(); err != nil {
return err return err
@ -57,6 +78,7 @@ func (c *Container) Shutdown() error {
return nil return nil
} }
// initConfig initializes configuration
func (c *Container) initConfig() { func (c *Container) initConfig() {
cfg, err := config.GetConfig() cfg, err := config.GetConfig()
if err != nil { if err != nil {
@ -65,6 +87,7 @@ func (c *Container) initConfig() {
c.Config = &cfg c.Config = &cfg
} }
// initWeb initializes the web framework
func (c *Container) initWeb() { func (c *Container) initWeb() {
c.Web = echo.New() c.Web = echo.New()
@ -77,6 +100,7 @@ func (c *Container) initWeb() {
} }
} }
// initCache initializes the cache
func (c *Container) initCache() { func (c *Container) initCache() {
c.cacheClient = redis.NewClient(&redis.Options{ c.cacheClient = redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", c.Config.Cache.Hostname, c.Config.Cache.Port), Addr: fmt.Sprintf("%s:%d", c.Config.Cache.Hostname, c.Config.Cache.Port),
@ -89,6 +113,8 @@ func (c *Container) initCache() {
c.Cache = cache.New(cacheStore) c.Cache = cache.New(cacheStore)
} }
// initDatabase initializes the database
// If the environment is set to test, the test database will be used and will be dropped, recreated and migrated
func (c *Container) initDatabase() { func (c *Container) initDatabase() {
var err error var err error
@ -127,6 +153,7 @@ func (c *Container) initDatabase() {
} }
} }
// initORM initializes the ORM
func (c *Container) initORM() { func (c *Container) initORM() {
drv := entsql.OpenDB(dialect.Postgres, c.Database) drv := entsql.OpenDB(dialect.Postgres, c.Database)
c.ORM = ent.NewClient(ent.Driver(drv)) c.ORM = ent.NewClient(ent.Driver(drv))
@ -135,14 +162,17 @@ func (c *Container) initORM() {
} }
} }
// initAuth initializes the authentication client
func (c *Container) initAuth() { func (c *Container) initAuth() {
c.Auth = NewAuthClient(c.Config, c.ORM) c.Auth = NewAuthClient(c.Config, c.ORM)
} }
// initTemplateRenderer initializes the template renderer
func (c *Container) initTemplateRenderer() { func (c *Container) initTemplateRenderer() {
c.TemplateRenderer = NewTemplateRenderer(c.Config) c.TemplateRenderer = NewTemplateRenderer(c.Config)
} }
// initMail initialize the mail client
func (c *Container) initMail() { func (c *Container) initMail() {
var err error var err error
c.Mail, err = NewMailClient(c.Config, c.TemplateRenderer) c.Mail, err = NewMailClient(c.Config, c.TemplateRenderer)

View file

@ -16,6 +16,7 @@ type MailClient struct {
// config stores application configuration // config stores application configuration
config *config.Config config *config.Config
// templates stores the template renderer
templates *TemplateRenderer templates *TemplateRenderer
} }
@ -51,7 +52,7 @@ func (c *MailClient) SendTemplate(ctx echo.Context, to, template string, data in
"mail", "mail",
template, template,
template, template,
[]string{fmt.Sprintf("email/%s", template)}, []string{fmt.Sprintf("emails/%s", template)},
[]string{}, []string{},
data, data,
) )

View file

@ -14,6 +14,8 @@ import (
"goweb/funcmap" "goweb/funcmap"
) )
// TemplateRenderer provides a flexible and easy to use method of rendering simple templates or complex sets of
// templates while also providing caching and/or hot-reloading depending on your current environment
type TemplateRenderer struct { type TemplateRenderer struct {
// templateCache stores a cache of parsed page templates // templateCache stores a cache of parsed page templates
templateCache sync.Map templateCache sync.Map
@ -28,6 +30,7 @@ type TemplateRenderer struct {
config *config.Config config *config.Config
} }
// NewTemplateRenderer creates a new TemplateRenderer
func NewTemplateRenderer(cfg *config.Config) *TemplateRenderer { func NewTemplateRenderer(cfg *config.Config) *TemplateRenderer {
t := &TemplateRenderer{ t := &TemplateRenderer{
templateCache: sync.Map{}, templateCache: sync.Map{},
@ -44,26 +47,26 @@ func NewTemplateRenderer(cfg *config.Config) *TemplateRenderer {
return t return t
} }
func (t *TemplateRenderer) ParseAndExecute(module, key, name string, files []string, directories []string, data interface{}) (*bytes.Buffer, error) { func (t *TemplateRenderer) ParseAndExecute(group, id, name string, files []string, directories []string, data interface{}) (*bytes.Buffer, error) {
var buf *bytes.Buffer var buf *bytes.Buffer
var err error var err error
if err = t.Parse(module, key, name, files, directories); err != nil { if err = t.Parse(group, id, name, files, directories); err != nil {
return nil, err return nil, err
} }
if buf, err = t.Execute(module, key, name, data); err != nil { if buf, err = t.Execute(group, id, name, data); err != nil {
return nil, err return nil, err
} }
return buf, nil return buf, nil
} }
func (t *TemplateRenderer) Parse(module, key, name string, files []string, directories []string) error { func (t *TemplateRenderer) Parse(group, id, name string, files []string, directories []string) error {
cacheKey := t.getCacheKey(module, key) cacheKey := t.getCacheKey(group, id)
// Check if the template has not yet been parsed or if the app environment is local, so that // Check if the template has not yet been parsed or if the app environment is local, so that
// templates reflect changes without having the restart the server // templates reflect changes without having the restart the server
if _, err := t.Load(module, key); err != nil || t.config.App.Environment == config.EnvLocal { if _, err := t.Load(group, id); err != nil || t.config.App.Environment == config.EnvLocal {
// Initialize the parsed template with the function map // Initialize the parsed template with the function map
parsed := template.New(name + config.TemplateExt). parsed := template.New(name + config.TemplateExt).
Funcs(t.funcMap) Funcs(t.funcMap)
@ -96,8 +99,8 @@ func (t *TemplateRenderer) Parse(module, key, name string, files []string, direc
return nil return nil
} }
func (t *TemplateRenderer) Execute(module, key, name string, data interface{}) (*bytes.Buffer, error) { func (t *TemplateRenderer) Execute(group, id, name string, data interface{}) (*bytes.Buffer, error) {
tmpl, err := t.Load(module, key) tmpl, err := t.Load(group, id)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -111,8 +114,8 @@ func (t *TemplateRenderer) Execute(module, key, name string, data interface{}) (
return buf, nil return buf, nil
} }
func (t *TemplateRenderer) Load(module, key string) (*template.Template, error) { func (t *TemplateRenderer) Load(group, id string) (*template.Template, error) {
load, ok := t.templateCache.Load(t.getCacheKey(module, key)) load, ok := t.templateCache.Load(t.getCacheKey(group, id))
if !ok { if !ok {
return nil, errors.New("uncached page template requested") return nil, errors.New("uncached page template requested")
} }
@ -129,6 +132,6 @@ func (t *TemplateRenderer) GetTemplatesPath() string {
return t.templatesPath return t.templatesPath
} }
func (t *TemplateRenderer) getCacheKey(module, key string) string { func (t *TemplateRenderer) getCacheKey(group, id string) string {
return fmt.Sprintf("%s:%s", module, key) return fmt.Sprintf("%s:%s", group, id)
} }