This repository has been archived by the owner on Jan 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·174 lines (139 loc) · 3.98 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
package main
import (
"fmt"
"net/url"
"time"
"github.com/fatih/color"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
"github.com/kjk/betterguid"
"github.com/toms1441/resistance-server/internal/client"
"github.com/toms1441/resistance-server/internal/config"
"github.com/toms1441/resistance-server/internal/discord"
"github.com/toms1441/resistance-server/internal/lobby"
"github.com/toms1441/resistance-server/internal/logger"
"github.com/toms1441/resistance-server/internal/repo/plain"
"github.com/toms1441/resistance-server/internal/routes"
"golang.org/x/oauth2"
_ "github.com/toms1441/resistance-server/internal/game"
)
func main() {
lc := logger.DefaultConfig
lc.PAttr = color.New(color.Italic, color.FgHiGreen)
lc.Prefix = "main"
main := logger.NewLogger(lc)
c, err := config.NewConfig()
if err != nil {
main.Fatal("config.NewConfig: %v", err)
}
gin.SetMode(gin.ReleaseMode)
r := gin.New()
r.Use(gin.Recovery())
store := cookie.NewStore([]byte(c.SessionSecret))
sesh := sessions.Sessions("sesh", store)
r.Use(sesh)
getuser := func(c *gin.Context) (discord.User, error) {
return discord.User{}, nil
}
// auth-related routes
{
group := "/auth/discord"
dr := routes.NewDiscordRoutes(oauth2.Config{
ClientID: c.Discord.ClientID,
ClientSecret: c.Discord.ClientSecret,
RedirectURL: fmt.Sprintf("%s%s/login", c.Domain, group),
Scopes: []string{"email", "identify"},
}, time.Hour*12)
// cache for 12 hours
// refresh the token whenever it's invalid
r.Use(dr.RefreshMiddleware())
// kinda globalize getuser to use in client
getuser = dr.GetUser
routergroup := r.Group(group)
routergroup.GET("/redirect", dr.Redirect())
routergroup.GET("/login", dr.Login())
routergroup.GET("/logout", dr.Logout())
routergroup.GET("/verify", func(c *gin.Context) {
user, _ := dr.GetUser(c)
c.JSON(200, user)
})
routergroup.POST("/refresh", func(c *gin.Context) {
dr.RefreshUser(c)
})
}
// because we're testing the new terminal client
discriminator := 1000
getuser = func(c *gin.Context) (discord.User, error) {
discriminator++
return discord.User{
ID: betterguid.New(),
Username: "test user",
Discriminator: fmt.Sprintf("%04d", discriminator),
}, nil
}
// lobby service init
// make the lobby service kinda global
// so we can use it in websocket route
var lserv lobby.Service
{
lc = logger.DefaultConfig
lc.PAttr = color.New(color.FgRed, color.Italic)
lc.Prefix = "lobby"
lc.Debug = true
llog := logger.NewLogger(lc)
lrepo := plain.NewLobbyRepository()
var err error
lserv, err = lobby.NewService(lrepo, c.Lobby)
if err != nil {
main.Danger("an error occurred with creating the lobby service: %v", err)
}
lserv.SetLogger(llog)
}
// client service init
var cserv client.Service
{
lc = logger.DefaultConfig
lc.PAttr = color.New(color.FgHiBlue, color.Italic)
lc.Prefix = "client"
lc.Debug = true
clog := logger.NewLogger(lc)
crepo := plain.NewClientRepository()
var err error
cserv, err = client.NewService(crepo)
if err != nil {
main.Danger("an error occurred with creating the client service: %v", err)
}
cserv.SetLogger(clog)
}
// websocket route init
{
lc := logger.DefaultConfig
lc.PAttr = color.New(color.FgCyan, color.Italic)
lc.Prefix = "socket"
lc.Debug = true
r.GET("/ws", routes.NewWebsocketRoute(
routes.WebsocketConfig{
LobbyService: lserv,
ClientService: cserv,
Log: logger.NewLogger(lc),
GetUser: getuser,
},
))
}
var port = "8080"
uri, err := url.Parse(c.Domain)
if err == nil {
port = uri.Port()
}
main.Info("Running web server on %s. Press Ctrl-C to quit.", port)
r.Run(":" + port)
}
/* send post request to refresh user information
r.GET("/", func(c *gin.Context) {
c.Data(200, "text/html; charset=utf-8", []byte(`
<form method="post" action="/auth/discord/refresh">
<input type="submit" value="alright">
</form>`))
})
*/