-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
168 lines (148 loc) · 4.31 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
package main
import (
"ciderbot/types"
"fmt"
"log"
"net/http"
"os"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
const (
authorizedUserKey = "AUTHORIZED_USER_EMAIL"
certFilePath = "./config/certs/localhost.pem"
certKeyFilePath = "./config/certs/localhost-key.pem"
appPort = ":8080"
)
var (
sessionName string
dbName string
clientID string
clientSecret string
redirectURL string
sessionSecret string
db *gorm.DB
googleOAuthConf *oauth2.Config
slackOAuthConf *oauth2.Config
slackClientID string
slackClientSecret string
slackSigningSecret string
slackVerificationToken string
slackRedirectURI string
appEnv string
encryptionKey string
applelinkAuthAud string
applelinkAuthIssuer string
applelinkAuthSecret string
applelinkCredentials *types.ApplelinkCredentials
applelinkHost string
)
func initEnv() {
e := godotenv.Load()
if e != nil {
log.Fatalf("Error loading .env file: %s", e)
}
appEnv = os.Getenv("ENV")
sessionName = os.Getenv("APP_NAME")
dbName = os.Getenv("DB_NAME")
clientID = os.Getenv("GOOGLE_CLIENT_ID")
clientSecret = os.Getenv("GOOGLE_CLIENT_SECRET")
redirectURL = os.Getenv("GOOGLE_REDIRECT_URL")
sessionSecret = os.Getenv("SECRET_SESSION_KEY")
slackClientID = os.Getenv("SLACK_CLIENT_ID")
slackClientSecret = os.Getenv("SLACK_CLIENT_SECRET")
slackSigningSecret = os.Getenv("SLACK_SIGNING_SECRET")
slackVerificationToken = os.Getenv("SLACK_VERIFICATION_TOKEN")
slackRedirectURI = os.Getenv("SLACK_REDIRECT_URL")
encryptionKey = os.Getenv("ENCRYPTION_KEY")
applelinkAuthAud = os.Getenv("APPLELINK_AUTH_AUD")
applelinkAuthIssuer = os.Getenv("APPLELINK_AUTH_ISSUER")
applelinkAuthSecret = os.Getenv("APPLELINK_AUTH_SECRET")
applelinkHost = os.Getenv("APPLELINK_HOST")
}
// TODO: do we need to close the DB "conn"?
func initDB(name string) *gorm.DB {
var err error
db, err = gorm.Open(sqlite.Open(name), &gorm.Config{})
if err != nil {
panic(err)
}
db.AutoMigrate(&types.User{})
db.AutoMigrate(&types.Metrics{})
return db
}
func initGoogleOAuthConf() {
googleOAuthConf = &oauth2.Config{
ClientID: clientID,
ClientSecret: clientSecret,
RedirectURL: redirectURL,
Scopes: []string{
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/userinfo.profile",
},
Endpoint: google.Endpoint,
}
}
func initSlackOAuthConf() {
slackOAuthConf = &oauth2.Config{
ClientID: slackClientID,
ClientSecret: slackClientSecret,
Endpoint: oauth2.Endpoint{
AuthURL: "https://slack.com/oauth/v2/authorize",
TokenURL: "https://slack.com/api/oauth.v2.access",
},
RedirectURL: slackRedirectURI,
Scopes: []string{"chat:write", "chat:write.customize", "commands"},
}
}
func initApplelinkCreds() {
applelinkCredentials = &types.ApplelinkCredentials{
Aud: applelinkAuthAud,
Issuer: applelinkAuthIssuer,
Secret: applelinkAuthSecret,
}
}
func handlePing() gin.HandlerFunc {
return func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "pong"})
}
}
func initServer(db *gorm.DB) {
r := gin.Default()
store := cookie.NewStore([]byte(sessionSecret))
r.Use(sessions.Sessions(sessionName, store))
r.GET("/", handleHome())
r.GET("/login", handleLogin())
r.GET("/logout", handleLogout())
r.GET("/auth/google/callback", handleGoogleCallback(db))
r.GET("/auth/slack/start", handleSlackAuth())
r.GET("/auth/slack/callback", getUserFromSessionMiddleware(), handleSlackAuthCallback())
r.POST("/auth/apple", getUserFromSessionMiddleware(), handleAppStoreCreds())
r.POST("/user/delete", getUserFromSessionMiddleware(), handleDeleteUser())
r.GET("/ping", handlePing())
r.POST("/slack/listen", handleSlackCommands())
r.Static("/assets", "./assets")
r.LoadHTMLGlob("views/*")
var err error
if appEnv == "production" {
err = r.Run(appPort)
} else {
err = r.RunTLS(appPort, certFilePath, certKeyFilePath)
}
if err != nil {
fmt.Println(err)
}
}
func main() {
initEnv()
initSlackOAuthConf()
initGoogleOAuthConf()
initApplelinkCreds()
initServer(initDB(dbName))
}