2021-12-03 03:11:01 -08:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2021-12-14 17:14:11 -08:00
|
|
|
"os"
|
2021-12-03 03:11:01 -08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/joeshaw/envdecode"
|
|
|
|
)
|
|
|
|
|
2021-12-10 05:49:15 -08:00
|
|
|
const (
|
2021-12-18 07:15:31 -08:00
|
|
|
// TemplateDir stores the name of the directory that contains templates
|
|
|
|
TemplateDir = "templates"
|
|
|
|
|
|
|
|
// TemplateExt stores the extension used for the template files
|
|
|
|
TemplateExt = ".gohtml"
|
|
|
|
|
|
|
|
// StaticDir stores the name of the directory that will serve static files
|
|
|
|
StaticDir = "static"
|
|
|
|
|
|
|
|
// StaticPrefix stores the URL prefix used when serving static files
|
2021-12-10 05:49:15 -08:00
|
|
|
StaticPrefix = "files"
|
|
|
|
)
|
|
|
|
|
2022-02-10 05:56:07 -08:00
|
|
|
type environment string
|
2021-12-03 03:11:01 -08:00
|
|
|
|
|
|
|
const (
|
2022-02-10 05:56:07 -08:00
|
|
|
// EnvLocal represents the local environment
|
|
|
|
EnvLocal environment = "local"
|
|
|
|
|
|
|
|
// EnvTest represents the test environment
|
|
|
|
EnvTest environment = "test"
|
|
|
|
|
|
|
|
// EnvDevelop represents the development environment
|
|
|
|
EnvDevelop environment = "dev"
|
|
|
|
|
|
|
|
// EnvStaging represents the staging environment
|
|
|
|
EnvStaging environment = "staging"
|
|
|
|
|
|
|
|
// EnvQA represents the qa environment
|
|
|
|
EnvQA environment = "qa"
|
|
|
|
|
|
|
|
// EnvProduction represents the production environment
|
|
|
|
EnvProduction environment = "prod"
|
2021-12-03 03:11:01 -08:00
|
|
|
)
|
|
|
|
|
2021-12-18 07:15:31 -08:00
|
|
|
// SwitchEnvironment sets the environment variable used to dictate which environment the application is
|
|
|
|
// currently running in.
|
|
|
|
// This must be called prior to loading the configuration in order for it to take effect.
|
2022-02-10 05:56:07 -08:00
|
|
|
func SwitchEnvironment(env environment) {
|
2021-12-18 07:15:31 -08:00
|
|
|
if err := os.Setenv("APP_ENVIRONMENT", string(env)); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-11 16:32:34 -08:00
|
|
|
type (
|
2021-12-18 07:15:31 -08:00
|
|
|
// Config stores complete configuration
|
2021-12-11 16:32:34 -08:00
|
|
|
Config struct {
|
|
|
|
HTTP HTTPConfig
|
|
|
|
App AppConfig
|
|
|
|
Cache CacheConfig
|
|
|
|
Database DatabaseConfig
|
2021-12-14 08:29:45 -08:00
|
|
|
Mail MailConfig
|
2021-12-11 16:32:34 -08:00
|
|
|
}
|
2021-12-03 03:11:01 -08:00
|
|
|
|
2021-12-11 16:32:34 -08:00
|
|
|
// HTTPConfig stores HTTP configuration
|
|
|
|
HTTPConfig struct {
|
|
|
|
Hostname string `env:"HTTP_HOSTNAME"`
|
|
|
|
Port uint16 `env:"HTTP_PORT,default=8000"`
|
|
|
|
ReadTimeout time.Duration `env:"HTTP_READ_TIMEOUT,default=5s"`
|
|
|
|
WriteTimeout time.Duration `env:"HTTP_WRITE_TIMEOUT,default=10s"`
|
|
|
|
IdleTimeout time.Duration `env:"HTTP_IDLE_TIMEOUT,default=2m"`
|
2021-12-20 09:33:14 -08:00
|
|
|
TLS struct {
|
|
|
|
Enabled bool `env:"HTTP_TLS_ENABLED,default=false"`
|
|
|
|
Certificate string `env:"HTTP_TLS_CERTIFICATE"`
|
|
|
|
Key string `env:"HTTP_TLS_KEY"`
|
|
|
|
}
|
2021-12-11 16:32:34 -08:00
|
|
|
}
|
2021-12-03 03:11:01 -08:00
|
|
|
|
2021-12-11 16:32:34 -08:00
|
|
|
// AppConfig stores application configuration
|
|
|
|
AppConfig struct {
|
2022-01-08 12:32:18 -08:00
|
|
|
Name string `env:"APP_NAME,default=Pagoda"`
|
2022-02-10 05:56:07 -08:00
|
|
|
Environment environment `env:"APP_ENVIRONMENT,default=local"`
|
2022-01-08 12:32:18 -08:00
|
|
|
// THIS MUST BE OVERRIDDEN ON ANY LIVE ENVIRONMENTS
|
2021-12-17 16:58:44 -08:00
|
|
|
EncryptionKey string `env:"APP_ENCRYPTION_KEY,default=?E(G+KbPeShVmYq3t6w9z$C&F)J@McQf"`
|
|
|
|
Timeout time.Duration `env:"APP_TIMEOUT,default=20s"`
|
|
|
|
PasswordToken struct {
|
|
|
|
Expiration time.Duration `env:"APP_PASSWORD_TOKEN_EXPIRATION,default=60m"`
|
|
|
|
Length int `env:"APP_PASSWORD_TOKEN_LENGTH,default=64"`
|
|
|
|
}
|
2022-01-08 12:32:18 -08:00
|
|
|
EmailVerificationTokenExpiration time.Duration `env:"APP_EMAIL_VERIFICATION_TOKEN_EXPIRATION,default=12h"`
|
2021-12-11 16:32:34 -08:00
|
|
|
}
|
2021-12-03 03:11:01 -08:00
|
|
|
|
2021-12-18 07:15:31 -08:00
|
|
|
// CacheConfig stores the cache configuration
|
2021-12-11 16:32:34 -08:00
|
|
|
CacheConfig struct {
|
2022-02-06 07:07:25 -08:00
|
|
|
Hostname string `env:"CACHE_HOSTNAME,default=localhost"`
|
|
|
|
Port uint16 `env:"CACHE_PORT,default=6379"`
|
|
|
|
Password string `env:"CACHE_PASSWORD"`
|
|
|
|
Database int `env:"CACHE_DB,default=0"`
|
|
|
|
TestDatabase int `env:"CACHE_DB_TEST,default=1"`
|
|
|
|
Expiration struct {
|
2021-12-11 16:32:34 -08:00
|
|
|
StaticFile time.Duration `env:"CACHE_EXPIRATION_STATIC_FILE,default=4380h"`
|
|
|
|
Page time.Duration `env:"CACHE_EXPIRATION_PAGE,default=24h"`
|
|
|
|
}
|
2021-12-05 18:56:57 -08:00
|
|
|
}
|
2021-12-05 17:56:38 -08:00
|
|
|
|
2021-12-18 07:15:31 -08:00
|
|
|
// DatabaseConfig stores the database configuration
|
2021-12-11 16:32:34 -08:00
|
|
|
DatabaseConfig struct {
|
2021-12-13 09:51:00 -08:00
|
|
|
Hostname string `env:"DB_HOSTNAME,default=localhost"`
|
|
|
|
Port uint16 `env:"DB_PORT,default=5432"`
|
|
|
|
User string `env:"DB_USER,default=admin"`
|
|
|
|
Password string `env:"DB_PASSWORD,default=admin"`
|
|
|
|
Database string `env:"DB_NAME,default=app"`
|
|
|
|
TestDatabase string `env:"DB_NAME_TEST,default=app_test"`
|
2021-12-11 16:32:34 -08:00
|
|
|
}
|
2021-12-14 08:29:45 -08:00
|
|
|
|
2021-12-18 07:15:31 -08:00
|
|
|
// MailConfig stores the mail configuration
|
2021-12-14 08:29:45 -08:00
|
|
|
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"`
|
|
|
|
}
|
2021-12-11 16:32:34 -08:00
|
|
|
)
|
2021-12-10 05:49:15 -08:00
|
|
|
|
2021-12-18 07:15:31 -08:00
|
|
|
// GetConfig loads and returns configuration
|
2021-12-03 03:11:01 -08:00
|
|
|
func GetConfig() (Config, error) {
|
|
|
|
var cfg Config
|
|
|
|
err := envdecode.StrictDecode(&cfg)
|
|
|
|
return cfg, err
|
|
|
|
}
|