From c13dd9addbf89f716e4ef5cfdf1d673139ffcb68 Mon Sep 17 00:00:00 2001 From: Mo Tarbin Date: Sun, 30 Jun 2024 21:41:41 -0400 Subject: Move to Donetick Org, first commit --- config/config.go | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ config/local.yaml | 28 ++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 config/config.go create mode 100644 config/local.yaml (limited to 'config') diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..b1f73a1 --- /dev/null +++ b/config/config.go @@ -0,0 +1,126 @@ +package config + +import ( + "fmt" + "os" + "time" + + "github.com/spf13/viper" +) + +type Config struct { + Name string `mapstructure:"name" yaml:"name"` + Telegram TelegramConfig `mapstructure:"telegram" yaml:"telegram"` + Database DatabaseConfig `mapstructure:"database" yaml:"database"` + Jwt JwtConfig `mapstructure:"jwt" yaml:"jwt"` + Server ServerConfig `mapstructure:"server" yaml:"server"` + SchedulerJobs SchedulerConfig `mapstructure:"scheduler_jobs" yaml:"scheduler_jobs"` + EmailConfig EmailConfig `mapstructure:"email" yaml:"email"` + StripeConfig StripeConfig `mapstructure:"stripe" yaml:"stripe"` + IsDoneTickDotCom bool `mapstructure:"is_done_tick_dot_com" yaml:"is_done_tick_dot_com"` +} + +type TelegramConfig struct { + Token string `mapstructure:"token" yaml:"token"` +} + +type DatabaseConfig struct { + Type string `mapstructure:"type" yaml:"type"` + Host string `mapstructure:"host" yaml:"host"` + Port int `mapstructure:"port" yaml:"port"` + User string `mapstructure:"user" yaml:"user"` + Password string `mapstructure:"password" yaml:"password"` + Name string `mapstructure:"name" yaml:"name"` + Migration bool `mapstructure:"migration" yaml:"migration"` + LogLevel int `mapstructure:"logger" yaml:"logger"` +} + +type JwtConfig struct { + Secret string `mapstructure:"secret" yaml:"secret"` + SessionTime time.Duration `mapstructure:"session_time" yaml:"session_time"` + MaxRefresh time.Duration `mapstructure:"max_refresh" yaml:"max_refresh"` +} + +type ServerConfig struct { + Port int `mapstructure:"port" yaml:"port"` + RatePeriod time.Duration `mapstructure:"rate_period" yaml:"rate_period"` + RateLimit int `mapstructure:"rate_limit" yaml:"rate_limit"` + ReadTimeout time.Duration `mapstructure:"read_timeout" yaml:"read_timeout"` + WriteTimeout time.Duration `mapstructure:"write_timeout" yaml:"write_timeout"` +} + +type SchedulerConfig struct { + DueJob time.Duration `mapstructure:"due_job" yaml:"due_job"` + OverdueJob time.Duration `mapstructure:"overdue_job" yaml:"overdue_job"` + PreDueJob time.Duration `mapstructure:"pre_due_job" yaml:"pre_due_job"` +} + +type StripeConfig struct { + APIKey string `mapstructure:"api_key"` + WhitelistedIPs []string `mapstructure:"whitelisted_ips"` + Prices []StripePrices `mapstructure:"prices"` + SuccessURL string `mapstructure:"success_url"` + CancelURL string `mapstructure:"cancel_url"` +} + +type StripePrices struct { + PriceID string `mapstructure:"id"` + Name string `mapstructure:"name"` +} + +type EmailConfig struct { + Email string `mapstructure:"email"` + Key string `mapstructure:"key"` + Host string `mapstructure:"host"` + Port int `mapstructure:"port"` + AppHost string `mapstructure:"appHost"` +} + +func NewConfig() *Config { + return &Config{ + Telegram: TelegramConfig{ + Token: "", + }, + Database: DatabaseConfig{ + Type: "sqlite", + Migration: true, + }, + Jwt: JwtConfig{ + Secret: "secret", + SessionTime: 7 * 24 * time.Hour, + MaxRefresh: 7 * 24 * time.Hour, + }, + } +} + +func LoadConfig() *Config { + // read evniroment variable FORWARD_ENV and see if it's equal local then load local (config/local.yaml) using viper: + if os.Getenv("CA_ENV") == "local" { + viper.SetConfigName("local") + } else if os.Getenv("CA_ENV") == "prod" { + viper.SetConfigName("prod") + } else { + viper.SetConfigName("local") + } + // get logger and log the current environment: + fmt.Printf("--ConfigLoad config for environment: %s ", os.Getenv("CA_ENV")) + + viper.AddConfigPath("./config") + viper.SetConfigType("yaml") + err := viper.ReadInConfig() + // print a useful error: + if err != nil { + panic(err) + } + + var config Config + err = viper.Unmarshal(&config) + if err != nil { + panic(err) + } + fmt.Printf("--ConfigLoad name : %s ", config.Name) + + return &config + + // return LocalConfig() +} diff --git a/config/local.yaml b/config/local.yaml new file mode 100644 index 0000000..945a122 --- /dev/null +++ b/config/local.yaml @@ -0,0 +1,28 @@ +name: "local" +is_done_tick_dot_com: true +telegram: + token: "" +database: + type: "sqlite" + migration: true +jwt: + secret: "secret" + session_time: 168h + max_refresh: 168h +server: + port: 8000 + read_timeout: 2s + write_timeout: 1s + rate_period: 60s + rate_limit: 200 + +scheduler_jobs: + due_job: 30m + overdue_job: 3h + pre_due_job: 3h +email: + host: + port: + key: + email: + appHost: -- cgit