2021-12-03 03:11:01 -08:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2021-12-14 17:14:11 -08:00
|
|
|
"os"
|
2024-07-11 21:09:15 -07:00
|
|
|
"path/filepath"
|
2022-11-02 11:50:19 -07:00
|
|
|
"strings"
|
2021-12-03 03:11:01 -08:00
|
|
|
"time"
|
|
|
|
|
2022-11-02 11:50:19 -07:00
|
|
|
"github.com/spf13/viper"
|
2021-12-03 03:11:01 -08:00
|
|
|
)
|
|
|
|
|
2021-12-10 05:49:15 -08:00
|
|
|
const (
|
2021-12-18 07:15:31 -08:00
|
|
|
// 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) {
|
2022-11-02 11:50:19 -07:00
|
|
|
if err := os.Setenv("PAGODA_APP_ENVIRONMENT", string(env)); err != nil {
|
2021-12-18 07:15:31 -08:00
|
|
|
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 {
|
2024-07-11 21:09:15 -07:00
|
|
|
HTTP HTTPConfig
|
|
|
|
App AppConfig
|
|
|
|
Cache CacheConfig
|
|
|
|
Storage StorageConfig
|
|
|
|
Tasks TasksConfig
|
|
|
|
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 {
|
2022-11-02 11:50:19 -07:00
|
|
|
Hostname string
|
|
|
|
Port uint16
|
|
|
|
ReadTimeout time.Duration
|
|
|
|
WriteTimeout time.Duration
|
|
|
|
IdleTimeout time.Duration
|
2021-12-20 09:33:14 -08:00
|
|
|
TLS struct {
|
2022-11-02 11:50:19 -07:00
|
|
|
Enabled bool
|
|
|
|
Certificate string
|
|
|
|
Key string
|
2021-12-20 09:33:14 -08:00
|
|
|
}
|
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-11-02 11:50:19 -07:00
|
|
|
Name string
|
|
|
|
Environment environment
|
|
|
|
EncryptionKey string
|
|
|
|
Timeout time.Duration
|
2021-12-17 16:58:44 -08:00
|
|
|
PasswordToken struct {
|
2022-11-02 11:50:19 -07:00
|
|
|
Expiration time.Duration
|
|
|
|
Length int
|
2021-12-17 16:58:44 -08:00
|
|
|
}
|
2022-11-02 11:50:19 -07:00
|
|
|
EmailVerificationTokenExpiration time.Duration
|
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 {
|
2024-06-22 07:34:26 -07:00
|
|
|
Capacity int
|
|
|
|
Expiration struct {
|
2022-11-02 11:50:19 -07:00
|
|
|
StaticFile time.Duration
|
|
|
|
Page time.Duration
|
2021-12-11 16:32:34 -08:00
|
|
|
}
|
2021-12-05 18:56:57 -08:00
|
|
|
}
|
2021-12-05 17:56:38 -08:00
|
|
|
|
2024-07-11 21:09:15 -07:00
|
|
|
// StorageConfig stores the storage configuration
|
|
|
|
StorageConfig struct {
|
|
|
|
// DatabaseFile is the path used to find the database file.
|
|
|
|
// It can be an absolute path, or a path relative to the config file location.
|
|
|
|
DatabaseFile string
|
|
|
|
|
|
|
|
// MigrationsDir is the path used to find the migration files.
|
|
|
|
// It can be an absolute path, or a path relative to the config file location.
|
|
|
|
MigrationsDir string
|
2024-06-22 07:34:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// TasksConfig stores the tasks configuration
|
|
|
|
TasksConfig struct {
|
|
|
|
PollInterval time.Duration
|
|
|
|
MaxRetries int
|
|
|
|
Goroutines int
|
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 {
|
2022-11-02 11:50:19 -07:00
|
|
|
Hostname string
|
|
|
|
Port uint16
|
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
FromAddress string
|
2021-12-14 08:29:45 -08:00
|
|
|
}
|
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) {
|
2022-11-02 11:50:19 -07:00
|
|
|
var c Config
|
|
|
|
|
|
|
|
// Load the config file
|
|
|
|
viper.SetConfigName("config")
|
|
|
|
viper.SetConfigType("yaml")
|
|
|
|
viper.AddConfigPath(".")
|
|
|
|
viper.AddConfigPath("config")
|
2024-07-11 21:09:15 -07:00
|
|
|
viper.AddConfigPath("../")
|
|
|
|
viper.AddConfigPath("../../")
|
2022-11-02 11:50:19 -07:00
|
|
|
|
|
|
|
// Load env variables
|
|
|
|
viper.SetEnvPrefix("pagoda")
|
|
|
|
viper.AutomaticEnv()
|
|
|
|
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
|
|
|
|
|
|
|
if err := viper.ReadInConfig(); err != nil {
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := viper.Unmarshal(&c); err != nil {
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
|
2024-07-11 21:09:15 -07:00
|
|
|
usedConfigFilePath := viper.GetViper().ConfigFileUsed()
|
|
|
|
configFileDir := filepath.Dir(usedConfigFilePath)
|
|
|
|
if !filepath.IsAbs(c.Storage.DatabaseFile) {
|
|
|
|
c.Storage.DatabaseFile = filepath.Join(configFileDir, c.Storage.DatabaseFile)
|
|
|
|
}
|
|
|
|
if !filepath.IsAbs(c.Storage.MigrationsDir) {
|
|
|
|
c.Storage.MigrationsDir = filepath.Join(configFileDir, c.Storage.MigrationsDir)
|
|
|
|
}
|
|
|
|
|
2022-11-02 11:50:19 -07:00
|
|
|
return c, nil
|
2021-12-03 03:11:01 -08:00
|
|
|
}
|