aboutsummaryrefslogtreecommitdiffstats
path: root/config/config.go
blob: 52ed1dbf9069564cfaf51df8c35f270452122d39 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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"`
	CorsAllowOrigins []string      `mapstructure:"cors_allow_origins" yaml:"cors_allow_origins"`
	ServeFrontend    bool          `mapstructure:"serve_frontend" yaml:"serve_frontend"`
}

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 configEnvironmentOverrides(Config *Config) {
	if os.Getenv("DONETICK_TELEGRAM_TOKEN") != "" {
		Config.Telegram.Token = os.Getenv("DONETICK_TELEGRAM_TOKEN")
	}
}
func LoadConfig() *Config {
	// set the config name based on the environment:

	if os.Getenv("DT_ENV") == "local" {
		viper.SetConfigName("local")
	} else if os.Getenv("DT_ENV") == "prod" {
		viper.SetConfigName("prod")
	} else if os.Getenv("DT_ENV") == "selfhosted" {
		viper.SetConfigName("selfhosted")
	} else {
		viper.SetConfigName("local")
	}
	// get logger and log the current environment:
	fmt.Printf("--ConfigLoad config for environment: %s ", os.Getenv("DT_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)
	configEnvironmentOverrides(&config)
	return &config

	// return LocalConfig()
}