forked from Aliucord/Aliucord-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
101 lines (93 loc) · 2.76 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
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"strconv"
"strings"
"github.com/Aliucord/Aliucord-backend/bot"
"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),
}
fsHandler := fs.NewRequestHandler()
server := fasthttp.Server{
Logger: &fastHttpLogger{},
Handler: func(ctx *fasthttp.RequestCtx) {
path := string(ctx.Path())
switch path {
case "/":
ctx.Response.Header.Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprint(
ctx,
"<html><head><title>Aliucord</title></head><body>High quality™ temp page.<br><a href=\"/links/github\">[GitHub Org]</a> <a href=\"/links/discord\">[Discord Server]</a></body></html>",
)
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/") {
fsHandler(ctx)
} else {
ctx.SetStatusCode(fasthttp.StatusNotFound)
ctx.WriteString(fasthttp.StatusMessage(fasthttp.StatusNotFound))
}
}
},
}
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 "))
}