-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmain.go
265 lines (240 loc) · 6.91 KB
/
main.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package main
import (
"context"
"fmt"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"github.com/labstack/echo"
"github.com/leandro-lugaresi/hub"
"github.com/satori/go.uuid"
"github.com/spf13/viper"
"github.com/traPtitech/traQ/model"
"github.com/traPtitech/traQ/oauth2"
"github.com/traPtitech/traQ/oauth2/impl"
"github.com/traPtitech/traQ/rbac"
"github.com/traPtitech/traQ/rbac/role"
"github.com/traPtitech/traQ/repository"
repoimpl "github.com/traPtitech/traQ/repository/impl"
"github.com/traPtitech/traQ/router"
"github.com/traPtitech/traQ/sessions"
"github.com/traPtitech/traQ/utils/storage"
"io/ioutil"
"log"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"strings"
"time"
)
func main() {
// set default config values
viper.SetDefault("origin", "http://localhost:3000")
viper.SetDefault("port", 3000)
viper.SetDefault("pprof", false)
viper.SetDefault("generateThumbnailOnStartUp", false)
viper.SetDefault("externalAuthentication.enabled", false)
viper.SetDefault("mariadb.host", "127.0.0.1")
viper.SetDefault("mariadb.port", 3306)
viper.SetDefault("mariadb.username", "root")
viper.SetDefault("mariadb.password", "password")
viper.SetDefault("mariadb.database", "traq")
viper.SetDefault("mariadb.connection.maxOpen", 0)
viper.SetDefault("mariadb.connection.maxIdle", 2)
viper.SetDefault("mariadb.connection.lifetime", 0)
viper.SetDefault("storage.type", "local")
viper.SetDefault("storage.local.dir", "./storage")
// read config
viper.AddConfigPath(".")
viper.SetConfigName("config")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.SetEnvPrefix("TRAQ")
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
log.Fatal(err)
}
}
// enable pprof http handler
if viper.GetBool("pprof") {
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
}
// Message Hub
hub := hub.New()
// Database
engine, err := getDatabase()
if err != nil {
panic(err)
}
defer engine.Close()
// FileStorage
fs, err := getFileStorage()
if err != nil {
panic(err)
}
// Repository
repo, err := repoimpl.NewRepositoryImpl(engine, fs, hub)
if err != nil {
panic(err)
}
if init, err := repo.Sync(); err != nil {
panic(err)
} else if init { // 初期化
if dir := viper.GetString("initDataDir"); len(dir) > 0 {
if err := initData(repo, dir); err != nil {
panic(err)
}
}
}
if viper.GetBool("generateThumbnailOnStartUp") {
var files []uuid.UUID
if err := engine.Model(&model.File{}).Where("has_thumbnail = false").Pluck("id", &files).Error; err != nil {
panic(err)
}
for _, v := range files {
_, _ = repo.RegenerateThumbnail(v)
}
}
// SessionStore
sessionStore, err := sessions.NewGORMStore(engine)
if err != nil {
panic(err)
}
sessions.SetStore(sessionStore)
// Init Role-Based Access Controller
rbacStore, err := rbac.NewDefaultStore(engine)
if err != nil {
panic(err)
}
r, err := rbac.New(rbacStore)
if err != nil {
panic(err)
}
role.SetRole(r)
// oauth2 handler
oauth2Store, err := impl.NewDefaultStore(engine)
if err != nil {
panic(err)
}
oauth := &oauth2.Handler{
Store: oauth2Store,
AccessTokenExp: 60 * 60 * 24 * 365, //1年
AuthorizationCodeExp: 60 * 5, //5分
IsRefreshEnabled: false,
UserAuthenticator: func(id, pw string) (uuid.UUID, error) {
user, err := repo.GetUserByName(id)
if err != nil {
switch err {
case repository.ErrNotFound:
return uuid.Nil, oauth2.ErrUserIDOrPasswordWrong
default:
return uuid.Nil, err
}
}
err = model.AuthenticateUser(user, pw)
switch err {
case model.ErrUserWrongIDOrPassword, model.ErrUserBotTryLogin:
err = oauth2.ErrUserIDOrPasswordWrong
}
return user.ID, err
},
UserInfoGetter: func(uid uuid.UUID) (oauth2.UserInfo, error) {
u, err := repo.GetUser(uid)
if err == repository.ErrNotFound {
return nil, oauth2.ErrUserIDOrPasswordWrong
}
return u, err
},
Issuer: viper.GetString("origin"),
}
if viper.IsSet("key.rs256Public") && viper.IsSet("key.rs256Private") {
err := oauth.LoadKeys(loadKeys(viper.GetString("key.rs256Private"), viper.GetString("key.rs256Public")))
if err != nil {
panic(err)
}
}
// Firebase
if f := viper.GetString("firebase.serviceAccount.file"); len(f) > 0 {
if _, err := NewFCMManager(repo, hub, f, viper.GetString("origin")); err != nil {
panic(err)
}
}
// Routing
h := router.NewHandlers(oauth, r, repo, hub, viper.GetString("imagemagick.path"))
e := echo.New()
router.SetupRouting(e, h)
router.LoadWebhookTemplate("static/webhook/*.tmpl")
go func() {
if err := e.Start(fmt.Sprintf(":%d", viper.GetInt("port"))); err != nil {
e.Logger.Info("shutting down the server")
}
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := e.Shutdown(ctx); err != nil {
e.Logger.Error(err)
}
sessions.PurgeCache()
}
func getDatabase() (*gorm.DB, error) {
engine, err := gorm.Open("mysql", fmt.Sprintf(
"%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&collation=utf8mb4_general_ci&parseTime=true",
viper.GetString("mariadb.username"),
viper.GetString("mariadb.password"),
viper.GetString("mariadb.host"),
viper.GetInt("mariadb.port"),
viper.GetString("mariadb.database"),
))
if err != nil {
return nil, err
}
engine.DB().SetMaxOpenConns(viper.GetInt("mariadb.connection.maxOpen"))
engine.DB().SetMaxIdleConns(viper.GetInt("mariadb.connection.maxIdle"))
engine.DB().SetConnMaxLifetime(time.Duration(viper.GetInt("mariadb.connection.lifetime")) * time.Second)
return engine, nil
}
func getFileStorage() (storage.FileStorage, error) {
switch viper.GetString("storage.type") {
case "swift":
return storage.NewSwiftFileStorage(
viper.GetString("storage.swift.container"),
viper.GetString("storage.swift.username"),
viper.GetString("storage.swift.apiKey"),
viper.GetString("storage.swift.tenantName"),
viper.GetString("storage.swift.tenantId"),
viper.GetString("storage.swift.authUrl"),
viper.GetString("storage.swift.tempUrlKey"),
)
case "composite":
return storage.NewCompositeFileStorage(
viper.GetString("storage.local.dir"),
viper.GetString("storage.swift.container"),
viper.GetString("storage.swift.username"),
viper.GetString("storage.swift.apiKey"),
viper.GetString("storage.swift.tenantName"),
viper.GetString("storage.swift.tenantId"),
viper.GetString("storage.swift.authUrl"),
viper.GetString("storage.swift.tempUrlKey"),
)
case "memory":
return storage.NewInMemoryFileStorage(), nil
default:
return storage.NewLocalFileStorage(viper.GetString("storage.local.dir")), nil
}
}
func loadKeys(private, public string) ([]byte, []byte) {
prk, err := ioutil.ReadFile(private)
if err != nil {
panic(err)
}
puk, err := ioutil.ReadFile(public)
if err != nil {
panic(err)
}
return prk, puk
}