-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.ts
108 lines (95 loc) · 3.97 KB
/
server.ts
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
import "dotenv/config"
import bodyParser from "body-parser"
import express from "express"
import path from "path"
import ytNotification from "youtube-notification"
import config from "./config.json"
import {YoutubeOnline} from "./routes/YoutubeOnline"
import {Logger} from "./structures/Logger"
import {SQLQuery} from "./structures/SQLQuery"
let yt: any
if (!yt) {
yt = new ytNotification({
hubCallback: `${config.kisaragiAPI}/yt`,
middleware: true
})
}
export default class Server {
public checkState = async (state: string) => {
const states = await SQLQuery.redisGet("state")
if (states.includes(state)) {
return true
} else {
return false
}
}
public run = () => {
const app = express()
const port = process.env.PORT || 4000
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))
app.use("/yt", yt.listener())
YoutubeOnline.youtubeRoutes(app)
app.get("/reddit", async (req, res) => {
res.setHeader("Content-Type", "text/html;charset=utf-8")
if (req.query.code) {
try {
if (!await this.checkState(req.query.state as string)) return res.status(400).sendFile(path.join(__dirname, "../assets/html/expired.html"))
await SQLQuery.redditOuath(req.query.code as string)
res.status(200).sendFile(path.join(__dirname, "../assets/html/authorized.html"))
} catch {
res.status(400).sendFile(path.join(__dirname, "../assets/html/rejected.html"))
}
} else {
res.status(200).sendFile(path.join(__dirname, "../assets/html/index.html"))
}
})
app.get("/twitter", async (req, res) => {
res.setHeader("Content-Type", "text/html;charset=utf-8")
if (req.query.oauth_token) {
try {
await SQLQuery.twitterOauth(req.query.oauth_token as string, req.query.oauth_verifier as string)
res.status(200).sendFile(path.join(__dirname, "../assets/html/authorized.html"))
} catch {
res.status(400).sendFile(path.join(__dirname, "../assets/html/rejected.html"))
}
} else {
res.status(200).sendFile(path.join(__dirname, "../assets/html/index.html"))
}
})
app.get("/discord", async (req, res) => {
res.setHeader("Content-Type", "text/html;charset=utf-8")
if (req.query.code) {
if (!await this.checkState(req.query.state as string)) return res.status(400).sendFile(path.join(__dirname, "../assets/html/expired.html"))
await SQLQuery.initOauth2(req.query.code as string)
res.status(200).sendFile(path.join(__dirname, "../assets/html/authorized.html"))
} else {
res.status(200).sendFile(path.join(__dirname, "../assets/html/index.html"))
}
})
app.get("/commands", async (req, res) => {
res.status(200)
if (req.query.category) {
const commands = await SQLQuery.allCommands(req.query.category as string)
res.json(commands)
} else {
const commands = await SQLQuery.allCommands()
res.json(commands)
}
})
app.get("/", (req, res) => {
res.setHeader("Content-Type", "text/html;charset=utf-8")
res.status(200).sendFile(path.join(__dirname, "../assets/html/index.html"))
})
app.get("*", (req, res) => {
res.setHeader("Content-Type", "text/html;charset=utf-8")
res.status(404).sendFile(path.join(__dirname, "../assets/html/404.html"))
})
app.listen(port)
Logger.log(`Started the web server!`)
}
}
export {yt}
/*When runnning server only build*/
const server = new Server()
server.run()