-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
91 lines (82 loc) · 2.37 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
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/usecase/production"
"github.com/traPtitech/knoQ/utils"
"golang.org/x/oauth2"
"github.com/traPtitech/knoQ/router"
"github.com/carlescere/scheduler"
"github.com/gorilla/sessions"
"go.uber.org/zap"
)
func main() {
logger, _ := zap.NewDevelopment()
domain.VERSION = os.Getenv("KNOQ_VERSION")
domain.REVISION = os.Getenv("KNOQ_REVISION")
domain.DEVELOPMENT, _ = strconv.ParseBool(os.Getenv("DEVELOPMENT"))
gormRepo := db.GormRepository{}
err := gormRepo.Setup(os.Getenv("MARIADB_HOSTNAME"), os.Getenv("MARIADB_USERNAME"),
os.Getenv("MARIADB_PASSWORD"), os.Getenv("MARIADB_DATABASE"), os.Getenv("TOKEN_KEY"))
if err != nil {
panic(err)
}
traqRepo := traq.TraQRepository{
Config: &oauth2.Config{
ClientID: os.Getenv("CLIENT_ID"),
RedirectURL: os.Getenv("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",
}
repo := &production.Repository{
GormRepo: gormRepo,
TraQRepo: traqRepo,
}
handler := &router.Handlers{
Repo: repo,
Logger: logger,
SessionKey: []byte(os.Getenv("SESSION_KEY")),
ClientID: os.Getenv("CLIENT_ID"),
SessionOption: sessions.Options{
Path: "/",
MaxAge: 86400 * 30,
HttpOnly: true,
SameSite: http.SameSiteLaxMode,
},
WebhookID: os.Getenv("WEBHOOK_ID"),
WebhookSecret: os.Getenv("WEBHOOK_SECRET"),
ActivityChannelID: os.Getenv("CHANNEL_ID"),
Origin: os.Getenv("ORIGIN"),
}
e := handler.SetupRoute()
// webhook
job := utils.InitPostEventToTraQ(&repo.GormRepo, handler.WebhookSecret,
handler.ActivityChannelID, handler.WebhookID, handler.Origin)
scheduler.Every().Day().At("08:00").Run(job)
// サーバースタート
go func() {
if err := e.Start(":3000"); err != nil {
e.Logger.Info("shutting down the server")
}
}()
quit := make(chan os.Signal)
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)
}
}