-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.mjs
130 lines (99 loc) · 4.69 KB
/
app.mjs
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
import express from "express";
import { Analytic } from "./modules/db.mjs";
import isValidName from "./modules/isValidName.mjs";
import {PORT} from "./modules/consts.mjs"
const port = PORT;
const startTime = new Date();
console.log(`Server started at ${startTime}`);
import { scanInterval } from "./intervals.mjs";
// Setups
const app = express();
const asyncMiddleware = function (fn) {
return function (req, res) {
Promise.resolve(fn(req, res))
.catch((e) => {
console.log(`Error on ${req.originalUrl}: ${e.toString()}`);
res.json({ msg: "error, please send a screenshot or copy paste of this page to Hampton Moore, @herohamp on Scratch, @herohamp_ on Twitter, Email is me (at) hampton (dot) pw, or make an issue on the Github repo https://github.com/hamptonmoore/FluffyScratch/issues.", error: e.toString(), url: req.originalUrl })
});
};
};
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*"); // update to match the domain you will make the request from
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
next();
if (req.originalUrl != "/metrics") {
Analytic.increment("value", { where: { name: "totalRequests" } });
}
});
app.get("/", (req, res) =>
res.send("If you do not know what this is you should not be here <3")
);
app.get("/testError", asyncMiddleware(async function route(req, res) { throw "Error testing"; }))
app.get("/debug", (req, res) => {
res.json({
DEPLOYED: process.env.DEPLOYED,
startTime,
});
});
app.get("/commit", (req, res) => {
res.sendFile('commit.txt', { root: "." });
});
/*
/auth routes
*/
import authGetKeysv1Username from "./routes/auth/getKeys/v1/[username].mjs";
app.use("/auth/getKeys/v1/:username", asyncMiddleware(authGetKeysv1Username));
import authGetKeysv2 from "./routes/auth/getKeys/v2.mjs";
app.use("/auth/getKeys/v2", asyncMiddleware(authGetKeysv2));
import authVerifyV1 from "./routes/auth/verify/v1.mjs";
app.use("/auth/verify/v1/:username/:publicCode/:privateCode/:redirectLocation", asyncMiddleware(authVerifyV1));
import authVerifyV2 from "./routes/auth/verify/v2.mjs";
app.use("/auth/verify/v2/:privateCode", asyncMiddleware(authVerifyV2));
import authTest from "./routes/auth/test.mjs";
app.use("/auth/test", asyncMiddleware(authTest));
import authTest2 from "./routes/auth/test2.mjs";
app.use("/auth/test2", asyncMiddleware(authTest2));
import noRef from "./routes/auth/noRef.mjs";
app.use("/auth/noRef", asyncMiddleware(noRef));
import Metrics from "./routes/metrics.mjs";
app.use("/metrics", Metrics);
/*
/user routes
*/
app.use("/user/:username", function (req, res, next) {
if (isValidName(req.params.username)) {
next();
} else {
res.json({ error: "Username is not a valid scratch username /^[\w-]{3,20}$/" });
}
});
import userUsernameTest from "./routes/user/[username]/test.mjs";
app.use("/user/:username/test", asyncMiddleware(userUsernameTest));
// Profile picture APIs
import userUsernameProfilePicture from "./routes/user/[username]/profile/picture.mjs";
app.use("/user/:username/profile/picture/", asyncMiddleware(userUsernameProfilePicture));
app.use("/profilepicture/v1/:username", asyncMiddleware(userUsernameProfilePicture)); // Temporary for backwards compatibility
// Notifications API
import userUsernameNotifications from "./routes/user/[username]/notifications/index.mjs";
app.use("/user/:username/notifications", asyncMiddleware(userUsernameNotifications));
app.use("/notifications/v2/:username", asyncMiddleware(userUsernameNotifications)); // Temporary for backwards compatibility
import userUsernameNotificationsAlt from "./routes/user/[username]/notifications/alt.mjs";
app.use("/user/:username/notifications/alt", asyncMiddleware(userUsernameNotificationsAlt));
app.use("/notifications/v2/:username/alt", asyncMiddleware(userUsernameNotificationsAlt)); // Temporary for backwards compatibility
// Scrape user profile
import userUsernameProfileCommentsScrape from "./routes/user/[username]/profile/comments/scrape/index.mjs";
app.use("/user/:username/profile/comments/scrape", asyncMiddleware(userUsernameProfileCommentsScrape));
import userUsernameProfileCommentsStats from "./routes/user/[username]/profile/comments/stats.mjs";
app.use("/user/:username/profile/comments/stats", asyncMiddleware(userUsernameProfileCommentsStats));
/*
/comments routes
*/
import commentsFindByParentID from "./routes/comments/findBy/parentID/index.mjs";
app.use("/comments/findBy/parentID/:parentID", commentsFindByParentID);
app.listen(port, () =>
console.log(`Example app listening at http://localhost:${port}`)
);
// Go brrrr