-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
112 lines (104 loc) · 3.16 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
package main
import (
"encoding/json"
"log"
"os"
"strconv"
"strings"
"github.com/Aliucord/Aliucord-backend/bot"
"github.com/Aliucord/Aliucord-backend/bot/commands"
"github.com/Aliucord/Aliucord-backend/bot/modules"
"github.com/Aliucord/Aliucord-backend/common"
"github.com/Aliucord/Aliucord-backend/database"
"github.com/Aliucord/Aliucord-backend/updateTracker"
"github.com/valyala/fasthttp"
)
type fastHttpLogger struct{}
func (*fastHttpLogger) Printf(string, ...interface{}) {}
func main() {
f, err := os.Open("config.json")
if err != nil {
log.Fatal(err)
}
var config *common.Config
err = json.NewDecoder(f).Decode(&config)
f.Close()
if err != nil {
log.Fatal(err)
}
database.InitDB(config.Database)
if config.Bot.Enabled {
modules.UpdateScamTitles()
bot.StartBot(config)
defer bot.StopBot()
}
updateTracker.StartUpdateTracker(config)
log.Println("Starting http server at port", config.Port)
fs := &fasthttp.FS{
Root: config.ApkCacheDir,
PathRewrite: fasthttp.NewPathSlashesStripper(2),
}
apkFsHandler := fs.NewRequestHandler()
staticFs := &fasthttp.FS{Root: "static", IndexNames: []string{"index.html"}}
staticFsHandler := staticFs.NewRequestHandler()
server := fasthttp.Server{
Logger: &fastHttpLogger{},
Handler: func(ctx *fasthttp.RequestCtx) {
path := string(ctx.Path())
switch path {
case "/links/github":
ctx.Redirect("https://github.com/Aliucord", fasthttp.StatusMovedPermanently)
case "/links/discord":
ctx.Redirect("https://discord.gg/EsNDvBaHVU", fasthttp.StatusMovedPermanently)
case "/download/discord":
v := string(ctx.QueryArgs().Peek("v"))
if v == "" {
missingParams(ctx, []string{"v - discord version code"})
return
}
version, err := strconv.Atoi(v)
if err != nil {
missingParams(ctx, []string{"v - discord version code"})
return
}
url, err := updateTracker.GetDownloadURL(version, string(ctx.QueryArgs().Peek("split")), false)
if err != nil {
ctx.SetStatusCode(fasthttp.StatusNotFound)
ctx.WriteString("apk not found: " + err.Error())
return
}
ctx.Redirect(url, fasthttp.StatusFound)
default:
if strings.HasPrefix(path, "/download/direct/") {
apkFsHandler(ctx)
ctx.Response.Header.Set("Cache-Control", "max-age=31536000") // 1 year
} else if strings.HasPrefix(path, "/badges/") { // temporary badges v1 api
if strings.HasPrefix(path, "/badges/users/") {
id := strings.TrimPrefix(path, "/badges/users/")
if data, ok := commands.Badges.Users[id]; ok {
json.NewEncoder(ctx).Encode(&data)
return
}
} else if strings.HasPrefix(path, "/badges/guilds/") {
id := strings.TrimPrefix(path, "/badges/guilds/")
if data, ok := commands.Badges.Guilds[id]; ok {
json.NewEncoder(ctx).Encode(data)
return
}
}
ctx.NotFound()
} else {
staticFsHandler(ctx)
}
}
},
}
err = server.ListenAndServe(":" + config.Port)
if err != nil {
log.Fatal(err)
}
}
func missingParams(ctx *fasthttp.RequestCtx, params []string) {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
ctx.WriteString("missing required query params:\n " + strings.Join(params, "\n "))
}