Added mock mail client.
This commit is contained in:
parent
869c507737
commit
d0caa8119e
3 changed files with 48 additions and 0 deletions
|
|
@ -31,6 +31,7 @@ type (
|
||||||
App AppConfig
|
App AppConfig
|
||||||
Cache CacheConfig
|
Cache CacheConfig
|
||||||
Database DatabaseConfig
|
Database DatabaseConfig
|
||||||
|
Mail MailConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTTPConfig stores HTTP configuration
|
// HTTPConfig stores HTTP configuration
|
||||||
|
|
@ -68,6 +69,14 @@ type (
|
||||||
Database string `env:"DB_NAME,default=app"`
|
Database string `env:"DB_NAME,default=app"`
|
||||||
TestDatabase string `env:"DB_NAME_TEST,default=app_test"`
|
TestDatabase string `env:"DB_NAME_TEST,default=app_test"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MailConfig struct {
|
||||||
|
Hostname string `env:"MAIL_HOSTNAME,default=localhost"`
|
||||||
|
Port uint16 `env:"MAIL_PORT,default=25"`
|
||||||
|
User string `env:"MAIL_USER,default=admin"`
|
||||||
|
Password string `env:"MAIL_PASSWORD,default=admin"`
|
||||||
|
FromAddress string `env:"MAIL_FROM_ADDRESS,default=admin@localhost"`
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetConfig loads and returns application configuration
|
// GetConfig loads and returns application configuration
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,8 @@ import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"goweb/mail"
|
||||||
|
|
||||||
"entgo.io/ent/dialect"
|
"entgo.io/ent/dialect"
|
||||||
entsql "entgo.io/ent/dialect/sql"
|
entsql "entgo.io/ent/dialect/sql"
|
||||||
"github.com/eko/gocache/v2/cache"
|
"github.com/eko/gocache/v2/cache"
|
||||||
|
|
@ -24,6 +26,7 @@ type Container struct {
|
||||||
Cache *cache.Cache
|
Cache *cache.Cache
|
||||||
Database *sql.DB
|
Database *sql.DB
|
||||||
ORM *ent.Client
|
ORM *ent.Client
|
||||||
|
Mail *mail.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewContainer() *Container {
|
func NewContainer() *Container {
|
||||||
|
|
@ -33,6 +36,7 @@ func NewContainer() *Container {
|
||||||
c.initCache()
|
c.initCache()
|
||||||
c.initDatabase()
|
c.initDatabase()
|
||||||
c.initORM()
|
c.initORM()
|
||||||
|
c.initMail()
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -113,3 +117,11 @@ func (c *Container) initORM() {
|
||||||
panic(fmt.Sprintf("failed to create database schema: %v", err))
|
panic(fmt.Sprintf("failed to create database schema: %v", err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Container) initMail() {
|
||||||
|
var err error
|
||||||
|
c.Mail, err = mail.NewClient(c.Config)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprintf("failed to create mail client: %v", err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
27
mail/mail.go
Normal file
27
mail/mail.go
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
package mail
|
||||||
|
|
||||||
|
import (
|
||||||
|
"goweb/config"
|
||||||
|
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Client struct {
|
||||||
|
config *config.Config
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewClient(cfg *config.Config) (*Client, error) {
|
||||||
|
return &Client{
|
||||||
|
config: cfg,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) Send(ctx echo.Context, to, body string) error {
|
||||||
|
if c.config.App.Environment != config.EnvProduction {
|
||||||
|
// IE, skip sending email..
|
||||||
|
}
|
||||||
|
ctx.Logger().Debugf("Mock email sent. To: %s Body: %s", to, body)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Send with template?
|
||||||
Loading…
Add table
Add a link
Reference in a new issue