-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
138 lines (124 loc) · 3.64 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
package main
import (
"context"
"net/http"
"os"
"os/signal"
"strconv"
"time"
"github.com/traPtitech/knoQ/domain"
"github.com/traPtitech/knoQ/infra/db"
"github.com/traPtitech/knoQ/infra/traq"
"github.com/traPtitech/knoQ/repository"
"github.com/traPtitech/knoQ/utils"
"github.com/traPtitech/knoQ/utils/tz"
"golang.org/x/oauth2"
"github.com/traPtitech/knoQ/router"
"github.com/gorilla/sessions"
"github.com/robfig/cron/v3"
"go.uber.org/zap"
)
var (
version = getenv("KNOQ_VERSION", "unknown")
revision = getenv("KNOQ_REVISION", "unknown")
development = getenv("DEVELOPMENT", "false")
mariadbHost = getenv("MARIADB_HOSTNAME", "db")
mariadbUser = getenv("MARIADB_USERNAME", "root")
mariadbPassword = getenv("MARIADB_PASSWORD", "password")
mariadbDatabase = getenv("MARIADB_DATABASE", "knoQ")
mariadbPort = getenv("MARIADB_PORT", "3306")
tokenKey = getenv("TOKEN_KEY", "random32wordsXXXXXXXXXXXXXXXXXXX")
gormLogLevel = getenv("GORM_LOG_LEVEL", "silent")
clientID = getenv("CLIENT_ID", "client_id")
origin = getenv("ORIGIN", "http://localhost:3000")
sessionKey = getenv("SESSION_KEY", "random32wordsXXXXXXXXXXXXXXXXXXX")
webhookID = getenv("WEBHOOK_ID", "")
webhookSecret = getenv("WEBHOOK_SECRET", "")
activityChannelID = getenv("ACTIVITY_CHANNEL_ID", "")
dailyChannelID = getenv("DAILY_CHANNEL_ID", "")
// TODO: traQにClient Credential Flowが実装されたら定期的に取得するように変更する
// Issue: https://github.com/traPtitech/traQ/issues/2403
traqAccessToken = getenv("TRAQ_ACCESS_TOKEN", "")
)
func main() {
logger, _ := zap.NewDevelopment()
domain.VERSION = version
domain.REVISION = revision
domain.DEVELOPMENT, _ = strconv.ParseBool(development)
gormRepo := db.GormRepository{}
err := gormRepo.Setup(mariadbHost, mariadbUser, mariadbPassword, mariadbDatabase, mariadbPort, tokenKey, gormLogLevel, tz.JST)
if err != nil {
panic(err)
}
traqRepo := traq.TraQRepository{
Config: &oauth2.Config{
ClientID: clientID,
RedirectURL: origin + "/api/callback",
Scopes: []string{"read"},
Endpoint: oauth2.Endpoint{
AuthURL: "https://q.trap.jp/api/v3/oauth2/authorize",
TokenURL: "https://q.trap.jp/api/v3/oauth2/token",
},
},
URL: "https://q.trap.jp/api/v3",
ServerAccessToken: traqAccessToken,
}
repo := &repository.Repository{
GormRepo: gormRepo,
TraQRepo: traqRepo,
}
handler := &router.Handlers{
Repo: repo,
Logger: logger,
SessionKey: []byte(sessionKey),
ClientID: clientID,
SessionOption: sessions.Options{
Path: "/",
MaxAge: 86400 * 30,
HttpOnly: true,
SameSite: http.SameSiteLaxMode,
},
WebhookID: webhookID,
WebhookSecret: webhookSecret,
ActivityChannelID: activityChannelID,
DailyChannelId: dailyChannelID,
Origin: origin,
}
e := handler.SetupRoute()
// webhook
c := cron.New(cron.WithLocation(tz.JST))
_, err = c.AddFunc(
"0 8 * * *",
utils.InitPostEventToTraQ(
&repo.GormRepo,
handler.WebhookSecret,
handler.DailyChannelId,
handler.WebhookID,
handler.Origin,
),
)
if err != nil {
panic(err)
}
c.Start()
// サーバースタート
go func() {
if err := e.Start(":3000"); 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.Fatal(err)
}
}
func getenv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}