From 3b41e1dfd85e0f8054203e18e0c6f6b37ab43d27 Mon Sep 17 00:00:00 2001 From: mikestefanello Date: Wed, 22 Dec 2021 19:18:33 -0500 Subject: [PATCH] Misc cleanup. --- services/auth_test.go | 6 ++--- services/container.go | 46 +++++++++++++++++++++++++++++------ services/mail.go | 3 ++- services/template_renderer.go | 27 +++++++++++--------- 4 files changed, 58 insertions(+), 24 deletions(-) diff --git a/services/auth_test.go b/services/auth_test.go index 806415d..bc335c4 100644 --- a/services/auth_test.go +++ b/services/auth_test.go @@ -104,12 +104,12 @@ func TestDeletePasswordTokens(t *testing.T) { } func TestRandomToken(t *testing.T) { - length := 64 + length := c.Config.App.PasswordToken.Length a, err := c.Auth.RandomToken(length) require.NoError(t, err) b, err := c.Auth.RandomToken(length) require.NoError(t, err) - assert.Len(t, a, 64) - assert.Len(t, b, 64) + assert.Len(t, a, length) + assert.Len(t, b, length) assert.NotEqual(t, a, b) } diff --git a/services/container.go b/services/container.go index 4a3853b..dd8509e 100644 --- a/services/container.go +++ b/services/container.go @@ -18,18 +18,38 @@ import ( "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 { - Web *echo.Echo - Config *config.Config - Cache *cache.Cache - cacheClient *redis.Client - Database *sql.DB - ORM *ent.Client - Mail *MailClient - Auth *AuthClient + // Web stores the web framework + Web *echo.Echo + + // Config stores the application configuration + Config *config.Config + + // Cache contains the cache interface + 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 } +// NewContainer creates and initializes a new Container func NewContainer() *Container { c := new(Container) c.initConfig() @@ -43,6 +63,7 @@ func NewContainer() *Container { return c } +// Shutdown shuts the Container down and disconnects all connections func (c *Container) Shutdown() error { if err := c.cacheClient.Close(); err != nil { return err @@ -57,6 +78,7 @@ func (c *Container) Shutdown() error { return nil } +// initConfig initializes configuration func (c *Container) initConfig() { cfg, err := config.GetConfig() if err != nil { @@ -65,6 +87,7 @@ func (c *Container) initConfig() { c.Config = &cfg } +// initWeb initializes the web framework func (c *Container) initWeb() { c.Web = echo.New() @@ -77,6 +100,7 @@ func (c *Container) initWeb() { } } +// initCache initializes the cache func (c *Container) initCache() { c.cacheClient = redis.NewClient(&redis.Options{ 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) } +// 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() { var err error @@ -127,6 +153,7 @@ func (c *Container) initDatabase() { } } +// initORM initializes the ORM func (c *Container) initORM() { drv := entsql.OpenDB(dialect.Postgres, c.Database) c.ORM = ent.NewClient(ent.Driver(drv)) @@ -135,14 +162,17 @@ func (c *Container) initORM() { } } +// initAuth initializes the authentication client func (c *Container) initAuth() { c.Auth = NewAuthClient(c.Config, c.ORM) } +// initTemplateRenderer initializes the template renderer func (c *Container) initTemplateRenderer() { c.TemplateRenderer = NewTemplateRenderer(c.Config) } +// initMail initialize the mail client func (c *Container) initMail() { var err error c.Mail, err = NewMailClient(c.Config, c.TemplateRenderer) diff --git a/services/mail.go b/services/mail.go index 69a7e81..d498bb2 100644 --- a/services/mail.go +++ b/services/mail.go @@ -16,6 +16,7 @@ type MailClient struct { // config stores application configuration config *config.Config + // templates stores the template renderer templates *TemplateRenderer } @@ -51,7 +52,7 @@ func (c *MailClient) SendTemplate(ctx echo.Context, to, template string, data in "mail", template, template, - []string{fmt.Sprintf("email/%s", template)}, + []string{fmt.Sprintf("emails/%s", template)}, []string{}, data, ) diff --git a/services/template_renderer.go b/services/template_renderer.go index 7d0c118..347a798 100644 --- a/services/template_renderer.go +++ b/services/template_renderer.go @@ -14,6 +14,8 @@ import ( "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 { // templateCache stores a cache of parsed page templates templateCache sync.Map @@ -28,6 +30,7 @@ type TemplateRenderer struct { config *config.Config } +// NewTemplateRenderer creates a new TemplateRenderer func NewTemplateRenderer(cfg *config.Config) *TemplateRenderer { t := &TemplateRenderer{ templateCache: sync.Map{}, @@ -44,26 +47,26 @@ func NewTemplateRenderer(cfg *config.Config) *TemplateRenderer { 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 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 } - 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 buf, nil } -func (t *TemplateRenderer) Parse(module, key, name string, files []string, directories []string) error { - cacheKey := t.getCacheKey(module, key) +func (t *TemplateRenderer) Parse(group, id, name string, files []string, directories []string) error { + cacheKey := t.getCacheKey(group, id) // 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 - 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 parsed := template.New(name + config.TemplateExt). Funcs(t.funcMap) @@ -96,8 +99,8 @@ func (t *TemplateRenderer) Parse(module, key, name string, files []string, direc return nil } -func (t *TemplateRenderer) Execute(module, key, name string, data interface{}) (*bytes.Buffer, error) { - tmpl, err := t.Load(module, key) +func (t *TemplateRenderer) Execute(group, id, name string, data interface{}) (*bytes.Buffer, error) { + tmpl, err := t.Load(group, id) if err != nil { return nil, err } @@ -111,8 +114,8 @@ func (t *TemplateRenderer) Execute(module, key, name string, data interface{}) ( return buf, nil } -func (t *TemplateRenderer) Load(module, key string) (*template.Template, error) { - load, ok := t.templateCache.Load(t.getCacheKey(module, key)) +func (t *TemplateRenderer) Load(group, id string) (*template.Template, error) { + load, ok := t.templateCache.Load(t.getCacheKey(group, id)) if !ok { return nil, errors.New("uncached page template requested") } @@ -129,6 +132,6 @@ func (t *TemplateRenderer) GetTemplatesPath() string { return t.templatesPath } -func (t *TemplateRenderer) getCacheKey(module, key string) string { - return fmt.Sprintf("%s:%s", module, key) +func (t *TemplateRenderer) getCacheKey(group, id string) string { + return fmt.Sprintf("%s:%s", group, id) }