-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
130 lines (113 loc) · 3.5 KB
/
config.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"fmt"
"log"
"strings"
"time"
"github.com/gofrs/uuid"
"github.com/spf13/viper"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"git.trap.jp/toki/bot_converter/migrate"
"git.trap.jp/toki/bot_converter/router"
"git.trap.jp/toki/bot_converter/service/bot"
)
// Config describes server config.
type Config struct {
// Port number to listen on.
Port int `mapstructure:"port" yaml:"port"`
// Origin is the origin URL of the bot.
Origin string `mapstructure:"origin" yaml:"origin"`
// Traq describes traq bot settings.
Traq struct {
// Origin is traQ origin. e.g. "wss://q.trap.jp"
Origin string `mapstructure:"origin" yaml:"origin"`
// AccessToken is access token for accessing traq API.
AccessToken string `mapstructure:"accessToken" yaml:"accessToken"`
// UserID is the user UUID of the bot.
UserID string `mapstructure:"userID" yaml:"userID"`
// Prefix is the bot command prefix.
Prefix string `mapstructure:"prefix" yaml:"prefix"`
} `mapstructure:"traq" yaml:"traq"`
// MariaDB describes db settings.
MariaDB struct {
// Port is MariaDB port.
Port int `mapstructure:"port" yaml:"port"`
// Hostname is MariaDB host.
Hostname string `mapstructure:"hostname" yaml:"hostname"`
// Username is MariaDB user.
Username string `mapstructure:"username" yaml:"username"`
// Password is password for the above user.
Password string `mapstructure:"password" yaml:"password"`
// Database is database name.
Database string `mapstructure:"database" yaml:"database"`
} `mapstructure:"mariadb" yaml:"mariadb"`
}
var c Config
func init() {
viper.SetDefault("port", 3000)
viper.SetDefault("origin", "")
viper.SetDefault("traq.origin", "wss://q.trap.jp")
viper.SetDefault("traq.accessToken", "")
viper.SetDefault("traq.userID", uuid.Nil)
viper.SetDefault("traq.prefix", "/")
viper.SetDefault("mariadb.port", 3306)
viper.SetDefault("mariadb.hostname", "localhost")
viper.SetDefault("mariadb.username", "root")
viper.SetDefault("mariadb.password", "password")
viper.SetDefault("mariadb.database", "poll")
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
if err := viper.Unmarshal(&c); err != nil {
log.Fatalf("an error occurred while unmarshalling config: %s", err)
}
}
// initDB initializes DB connection and executes migration.
func initDB() (*gorm.DB, error) {
db, err := gorm.Open(mysql.New(mysql.Config{
DSN: fmt.Sprintf(
"%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&collation=utf8mb4_general_ci&parseTime=true&loc=Local",
c.MariaDB.Username,
c.MariaDB.Password,
c.MariaDB.Hostname,
c.MariaDB.Port,
c.MariaDB.Database,
),
DefaultStringSize: 256,
DontSupportRenameIndex: true,
DontSupportRenameColumn: true,
}))
if err != nil {
return nil, err
}
db.Logger.LogMode(logger.Silent)
// Prevent bad idle connection closed by server side
sqlDB, err := db.DB()
if err != nil {
return nil, err
}
sqlDB.SetConnMaxIdleTime(10 * time.Second)
if err := migrate.Migrate(db); err != nil {
return nil, err
}
return db, nil
}
// provideRouterConfig provides router.Config.
func provideRouterConfig() router.Config {
return router.Config{
AccessToken: c.Traq.AccessToken,
}
}
// provideBotConfig provides service.Config.
func provideBotConfig() bot.Config {
return bot.Config{
TraqOrigin: c.Traq.Origin,
AccessToken: c.Traq.AccessToken,
BotID: uuid.Must(uuid.FromString(c.Traq.UserID)),
Prefix: c.Traq.Prefix,
Origin: c.Origin,
}
}