saasitone/config/config.go

166 lines
3.8 KiB
Go
Raw Permalink Normal View History

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
)
const (
// 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
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
)
// 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 {
panic(err)
}
}
2021-12-11 16:32:34 -08:00
type (
// 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
PasswordToken struct {
2022-11-02 11:50:19 -07:00
Expiration time.Duration
Length int
}
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
// CacheConfig stores the cache configuration
2021-12-11 16:32:34 -08:00
CacheConfig struct {
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
}
// TasksConfig stores the tasks configuration
TasksConfig struct {
Goroutines int
ReleaseAfter time.Duration
CleanupInterval time.Duration
2021-12-11 16:32:34 -08:00
}
2021-12-14 08:29:45 -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
)
// 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()
2024-07-11 21:09:15 -07:00
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
}