-
Notifications
You must be signed in to change notification settings - Fork 236
/
autoload.go
110 lines (94 loc) · 2.49 KB
/
autoload.go
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
package config
import (
"flag"
"fmt"
"github.com/spf13/viper"
"strconv"
)
var (
BotToken string
Socks5 string
TelegraphToken string
EnableTelegraph bool
Mysql MysqlConfig
EnableMysql bool
UpdateInterval int = 10
ErrorThreshold uint = 100
)
type MysqlConfig struct {
Host string
Port int
User string
Password string
DB string
}
func init() {
telegramTokenCli := flag.String("k", "", "Telegram Bot Token")
telegraphTokenCli := flag.String("tk", "", "Telegraph API Token")
socks5Cli := flag.String("s", "", "Socks5 Proxy")
intervalCli := flag.Int("i", 0, "Update Interval")
flag.Parse()
projectName := "flowerss-bot"
viper.SetConfigName("config") // name of config file (without extension)
viper.AddConfigPath(".")
viper.AddConfigPath(fmt.Sprintf("$HOME/.%s", projectName)) // call multiple times to add many search paths
viper.AddConfigPath(fmt.Sprintf("/data/docker/config/%s", projectName)) // path to look for the config file in
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("Fatal error config file: %s", err))
}
if *telegramTokenCli == "" {
BotToken = viper.GetString("bot_token")
} else {
BotToken = *telegramTokenCli
}
if *socks5Cli == "" {
Socks5 = viper.GetString("socks5")
} else {
Socks5 = *socks5Cli
}
if *telegraphTokenCli == "" {
if viper.IsSet("telegraph_token") {
EnableTelegraph = true
TelegraphToken = viper.GetString("telegraph_token")
} else {
EnableTelegraph = false
}
} else {
EnableTelegraph = true
TelegraphToken = *telegramTokenCli
}
if *intervalCli == 0 {
if viper.IsSet("update_interval") {
UpdateInterval = viper.GetInt("update_interval")
} else {
UpdateInterval = 10
}
} else {
UpdateInterval = *intervalCli
}
if viper.IsSet("mysql.host") {
EnableMysql = true
Mysql = MysqlConfig{
Host: viper.GetString("mysql.host"),
Port: viper.GetInt("mysql.port"),
User: viper.GetString("mysql.user"),
Password: viper.GetString("mysql.password"),
DB: viper.GetString("mysql.database"),
}
} else {
EnableMysql = false
}
}
func getInt(s string) int {
num, _ := strconv.Atoi(s)
return num
}
func (m *MysqlConfig) GetMysqlConnectingString() string {
usr := m.User
pwd := m.Password
host := m.Host
port := m.Port
db := m.DB
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=true", usr, pwd, host, port, db)
}