-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
156 lines (131 loc) · 4.21 KB
/
index.js
File metadata and controls
156 lines (131 loc) · 4.21 KB
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
const express = require("express");
const path = require("path");
const RLHandler = require("./routes/ratelimits.js");
const passport = require("passport");
const Discord = require("passport-discord");
const session = require("express-session");
const bp = require("body-parser");
const LevelSessionStore = require("level-session-store")(session);
const app = express();
const docRouter = express.Router();
docRouter.use(express.static(path.join(__dirname, "docs")));
docRouter.get("*", (_, res) => res.sendFile("index.html"));
app.use("/docs", docRouter);
const fs = require("fs").promises;
const mountRoutes = require("./routes");
const { Pool } = require("pg");
const config = require("./config.json");
console.log(`CLIENT SECRET: ${config.secret}`);
const md = require("markdown-it")({
langPrefix: "lang-"
});
app.db = new Pool({
host: "localhost",
user: "bananaboy21",
password: config.password,
database: "bananapi"
});
const BananAPIClient = require("./bot.js");
const client = new BananAPIClient(app);
client.login();
client.on("ready", () => {
client.console.log("Client logged in.");
});
app.client = client;
app.db.connect();
client.console.log("Connected to PSQL DB.");
app.cache = {};
passport.serializeUser((user, done) => done(null, user));
passport.deserializeUser((user, done) => done(null, user));
passport.use(new Discord.Strategy({
clientID: 496455297959985167,
clientSecret: config.secret,
callbackURL: "https://bananapi.ml/login",
scope: ["identify"]
}, (_, __, profile, done) => {
process.nextTick(() => {
return done(null, profile);
});
}));
app.use(session({
cookie: {
maxAge: 31536000000
},
secret: "secret",
saveUninitialized: true,
resave: true,
store: new LevelSessionStore()
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(bp.json());
app.use(bp.urlencoded({ extended: true }));
app.use("/api", (req, res, next) => {
const auth = req.headers["authorization"]; // Get Header
if(!auth) return res.status(401);
// If it is already an invalid token cached don't re request db
if(app.cache[auth] === false) res.status(401);
// If it cached and valid next() and pass control to other routes
if(app.cache[auth] === true) return next();
// No cache = ask Database
app.db.query("SELECT * FROM tokens WHERE token = $1", [auth]).then((res) => {
if(!res.rows.length) { // If no results in query
app.cache[auth] = false; // Recognize this key is invalid if they re request
return res.status(401);
}
app.cache[auth] = true; // Cache it for later requests
return next(); // Query returned results so we know token is right
});
});
const handler = new RLHandler();
app.use("/api", (req, res, next) => {
handler.handle.bind(handler);
next();
});
mountRoutes(app);
// Homepage
app.get("/", async (req, res) => {
const dbCount = await app.db.query("SELECT COUNT(*) FROM tokens");
const users = dbCount.rows[0].count;
res.render("index.ejs", {
authorized: req.isAuthenticated(),
count: users,
bananapfp: app.client.users.get("277981712989028353").displayAvatarURL({ size: 2048 }),
tntpfp: app.client.users.get("292690616285134850").displayAvatarURL({ size: 2048 }),
icepfp: app.client.users.get("302604426781261824").displayAvatarURL({ size: 2048 })
});
});
app.get("/ping", (req, res) => {
res.send({ data: "Pong!" });
});
app.get("/support", (req, res) => {
res.render("support.ejs");
});
app.get("/examples", async (req, res) => {
const markdown = await fs.readFile("./markdown/examples.md");
res.render("examples.ejs", {
code: md.render(markdown.toString())
});
});
app.get("/libs", async (req, res) => {
const markdown = await fs.readFile("./markdown/libs.md");
res.render("libs.ejs", {
code: md.render(markdown.toString())
});
});
app.get("/login", passport.authenticate("discord", {
failureRedirect: "/"
}), (req, res) => res.redirect("/"));
app.get("/logout", (req, res) => {
req.logout();
res.redirect("/");
});
app.get("/github/:name", (req, res) => {
const { name } = req.params;
return res.redirect(`https://github.com/bananaboy21/${name}`);
});
app.listen(3001, (err) => {
if(err) throw err;
// eslint-disable-next-line no-console
console.log("Listening on port 3001!");
});