66 lines
1.6 KiB
Go
66 lines
1.6 KiB
Go
package configs
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
type Config struct {
|
|
PublicHost string
|
|
Port string
|
|
DBUser string
|
|
DBPassword string
|
|
DBAddress string
|
|
DBName string
|
|
JWTSecret string
|
|
JWTExpirationInSeconds int64
|
|
GoogleClientID string
|
|
GoogleClientSecret string
|
|
SessionKey string
|
|
}
|
|
|
|
var Envs = initConfig()
|
|
|
|
func initConfig() Config {
|
|
godotenv.Load()
|
|
|
|
return Config{
|
|
PublicHost: getEnv("PUBLIC_HOST", "http://localhost"),
|
|
Port: getEnv("PORT", "8080"),
|
|
DBUser: getEnv("DB_USER", "root"),
|
|
DBPassword: getEnv("DB_PASSWORD", "mypassword"),
|
|
DBAddress: fmt.Sprintf("%s:%s", getEnv("DB_HOST", "127.0.0.1"), getEnv("DB_PORT", "3306")),
|
|
DBName: getEnv("DB_NAME", "ecom"),
|
|
JWTSecret: getEnv("JWT_SECRET", "not-so-secret-now-is-it?"),
|
|
JWTExpirationInSeconds: getEnvAsInt("JWT_EXPIRATION_IN_SECONDS", 3600*24*7),
|
|
GoogleClientID: getEnv("GOOGLE_CLIENT_ID", "notfound"),
|
|
GoogleClientSecret: getEnv("GOOGLE_CLIENT_SECRET", "nosecret"),
|
|
SessionKey: getEnv("SESSION_KEY", "sessionkey"),
|
|
}
|
|
}
|
|
|
|
// Gets the env by key or fallbacks
|
|
func getEnv(key, fallback string) string {
|
|
if value, ok := os.LookupEnv(key); ok {
|
|
return value
|
|
}
|
|
|
|
return fallback
|
|
}
|
|
|
|
func getEnvAsInt(key string, fallback int64) int64 {
|
|
if value, ok := os.LookupEnv(key); ok {
|
|
i, err := strconv.ParseInt(value, 10, 64)
|
|
if err != nil {
|
|
return fallback
|
|
}
|
|
|
|
return i
|
|
}
|
|
|
|
return fallback
|
|
}
|