Skip to content

Instantly share code, notes, and snippets.

@rockchalkwushock
Created May 18, 2024 12:38
Show Gist options
  • Save rockchalkwushock/140c90c7e7dbc90ed60f546c4e707e63 to your computer and use it in GitHub Desktop.
Save rockchalkwushock/140c90c7e7dbc90ed60f546c4e707e63 to your computer and use it in GitHub Desktop.
package config
import (
"log"
"os"
"github.com/spf13/viper"
)
type (
Config struct {
AppEnv string `mapstructure:"APP_ENV"`
Domain string `mapstructure:"DOMAIN"`
Host string `mapstructure:"HOST"`
LibSqlUrl string `mapstructure:"LIBSQL_URL"`
MaxAge int `mapstructure:"MAX_AGE"`
Port string `mapstructure:"PORT"`
RequestLimit int `mapstructure:"REQUEST_LIMIT"`
}
)
func New() *Config {
isOnFly := os.Getenv("FLY_APP_NAME")
if len(isOnFly) > 0 {
viper.AutomaticEnv()
} else {
viper.SetConfigType("env")
viper.SetConfigFile("./.env.development")
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err != nil {
log.Fatalf("failed to read config: %v\n", err)
}
}
return &Config{
AppEnv: viper.GetString("APP_ENV"),
Domain: viper.GetString("DOMAIN"),
Host: viper.GetString("HOST"),
LibSqlUrl: viper.GetString("LIBSQL_URL"),
MaxAge: viper.GetInt("MAX_AGE"),
Port: viper.GetString("PORT"),
RequestLimit: viper.GetInt("REQUEST_LIMIT"),
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment