/
application.go
164 lines (153 loc) · 5.41 KB
/
application.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
package application
import (
"fmt"
"github.com/go-redis/redis/v8"
hybs "github.com/hayabusa-cloud/hayabusa"
"github.com/hayabusa-cloud/hybs-server/application/common"
"github.com/hayabusa-cloud/hybs-server/application/middleware/realtime"
"github.com/hayabusa-cloud/hybs-server/application/model/central"
"github.com/hayabusa-cloud/hybs-server/application/model/games"
"github.com/hayabusa-cloud/hybs-server/application/model/platform"
"github.com/jinzhu/configor"
"github.com/patrickmn/go-cache"
"go.mongodb.org/mongo-driver/mongo"
)
// Up init somethings at server startup timing
func Up(eng hybs.Engine) {
loadAppConfig(eng)
ensureMongoIndices(eng)
loadRedisScripts(eng)
realtimeDebugToken(eng)
}
// Down stops service before exit process
func Down(eng hybs.Engine) {
eng.LogWarnf("hayabusa engine stopped")
}
func loadAppConfig(eng hybs.Engine) {
var appConf = eng.AppConfig()
var configLoader = configor.New(&configor.Config{
AutoReload: true,
AutoReloadInterval: common.ConfigReload,
})
var err = configLoader.Load(&common.Config, appConf)
if err != nil {
fmt.Printf("load application config failed:\n%s\n", err)
eng.Stop()
}
}
func ensureMongoIndices(eng hybs.Engine) {
var ensureIndexFn = func(dbID string, collection string, index mongo.IndexModel) (err error) {
err = eng.MongoDo(dbID, func(db *mongo.Database) (err error) {
// try create collection
db.CreateCollection(eng.Context(), collection)
var idxView, _ = db.Collection(collection).Indexes(), &mongo.Cursor{}
// create new index
_, err = idxView.CreateOne(eng.Context(), index)
return
})
return
}
var ensureAppIndexFn = func(collection string, index mongo.IndexModel) (err error) {
for _, app := range common.Config.Apps {
ensureIndexFn(app.DatabaseID, collection, index)
}
return
}
// platform tables
ensureIndexFn("MongoPlatform", "hayabusa_id", platform.HayabusaIDIndex)
// game common tables
ensureAppIndexFn("onetime_token", games.OnetimeTokenIndex)
ensureAppIndexFn("onetime_token", games.OnetimeTokenHayabusaIndex)
ensureAppIndexFn("onetime_token", games.OnetimeTokenExpireIndex)
ensureAppIndexFn("player", games.PlayerIndex)
// central tables
ensureIndexFn("MongoCentral", "sys_realtime_server", central.SysRealtimeServerAppIDIndex)
ensureIndexFn("MongoCentral", "sys_realtime_server", central.SysRealtimeServerOwnerIndex)
ensureIndexFn("MongoCentral", "sys_realtime_server", central.SysRealtimeServerExpireIndex)
}
func loadRedisScripts(eng hybs.Engine) {
const tokenExpireSeconds = 60*60*6 + 60
var onetimeTokenGetSetScript = fmt.Sprintf(`
local exists = redis.call('EXISTS', KEYS[1])
local result = {false, nil, nil, nil, nil}
if exists ~= 1 then
return result
end
local hayabusaID = redis.call('HGET', KEYS[1], 'HayabusaID')
local serverID = redis.call('HGET', KEYS[1], 'ServerID')
local permission = redis.call('HGET', KEYS[1], 'Permission')
local generatedAt = redis.call('HGET', KEYS[1], 'GeneratedAt')
redis.call('DEL', KEYS[1])
redis.call('HSET', KEYS[2], 'HayabusaID', hayabusaID)
redis.call('HSET', KEYS[2], 'ServerID', serverID)
redis.call('HSET', KEYS[2], 'Permission', permission)
redis.call('HSET', KEYS[2], 'GeneratedAt', ARGV[2])
redis.call('EXPIRE', KEYS[2], %d)
result[0] = true
result[1] = hayabusaID
result[2] = serverID
result[3] = permission
result[4] = generatedAt
return result
`, tokenExpireSeconds)
var onetimeTokenGetOnlyScript = fmt.Sprintf(`
local exists = redis.call('EXISTS', KEYS[1])
local result = {false, nil, nil, nil, nil}
if exists ~= 1 then
return result
end
local hayabusaID = redis.call('HGET', KEYS[1], 'HayabusaID')
local serverID = redis.call('HGET', KEYS[1], 'ServerID')
local permission = redis.call('HGET', KEYS[1], 'Permission')
local generatedAt = redis.call('HGET', KEYS[1], 'GeneratedAt')
redis.call('HSET', KEYS[1], 'GeneratedAt', ARGV[1])
redis.call('EXPIRE', KEYS[1], %d)
result[0] = true
result[1] = hayabusaID
result[2] = serverID
result[3] = permission
result[4] = generatedAt
return result
`, tokenExpireSeconds)
var err = eng.RedisDo("Redis", func(r redis.UniversalClient) error {
// onetime token get set script
var cmd = r.ScriptLoad(eng.Context(), onetimeTokenGetSetScript)
if err := cmd.Err(); err != nil {
return fmt.Errorf("redis script load failed:%s", err)
}
// store SHA1 of scripts
eng.SetUserValue("/Redis/Scripts/OnetimeTokenGetSet", cmd.Val())
// onetime token get only script
cmd = r.ScriptLoad(eng.Context(), onetimeTokenGetOnlyScript)
if err := cmd.Err(); err != nil {
return fmt.Errorf("redis script load failed:%s", err)
}
// store SHA1 of scripts
eng.SetUserValue("/Redis/Scripts/OnetimeTokenGetOnly", cmd.Val())
return nil
})
if err != nil {
eng.LogErrorf("redis error:%s", err)
}
}
func realtimeDebugToken(eng hybs.Engine) {
eng.CacheDo("Cache", func(c *cache.Cache) error {
var debugToken = &realtime.AccessToken{
AppID: "jp:playground:hayabusa-cloud:debug",
HayabusaID: []byte("hayabusa-cloud"),
Mux: "default",
Token: []byte("anA6cGxheWdyb3VuZDpoYXlhYnVzYS1jbG91ZDpkZWJ1ZyNoYXlhYnVzYS1jbG91ZCNta1pTWXlJd3VZR3dKdWJD"),
ValidUntil: hybs.Time{},
}
var cacheKey = fmt.Sprintf("/Realtime/Tokens/%s", debugToken.Token)
c.Set(cacheKey, debugToken, -1)
return nil
})
}
func init() {
hybs.RegisterRealtimeHandler("EchoTest", func(ctx hybs.RealtimeCtx) {
ctx.SetHeader(hybs.RealtimeHeader0SCOriginal)
ctx.SetEventCode(0x80f0)
ctx.Response()
})
}