finish up profiles, begin custom songs
This commit is contained in:
55
index.js
55
index.js
@ -1,4 +1,3 @@
|
||||
const io = require('@pm2/io')
|
||||
import { log } from "./log.js";
|
||||
import bodyParser from "body-parser";
|
||||
import express from "express";
|
||||
@ -10,6 +9,7 @@ import mime from "mime-types";
|
||||
import compression from "compression";
|
||||
import { accs, infdb, polytrack } from "./database.js";
|
||||
import { } from "./accounts/friend.js";
|
||||
import { search, download } from "./accounts/music.js";
|
||||
import { banUser, removeAccount, verifyCookie, getUserFromCookie, createAccount, resetPassword, loginAccount, addBadge } from "./accounts/manage.js";
|
||||
import { } from "./accounts/misc.js";
|
||||
import { getRawData, generateAccountPage, editProfile, saveData, getUsers, isAdmin, retrieveData } from "./accounts/profile.js";
|
||||
@ -26,29 +26,13 @@ app.use(cookieParser());
|
||||
app.use(express.json({ limit: "10mb" }));
|
||||
app.use(express.urlencoded({ extended: false }));
|
||||
app.use(express.text());
|
||||
let requests = 0;
|
||||
const requestsPerSec = io.meter({
|
||||
name: 'req/sec',
|
||||
id: 'app/requests/sec'
|
||||
});
|
||||
const requestsPerMin = io.meter({
|
||||
name: 'req/min',
|
||||
id: 'app/requests/min'
|
||||
});
|
||||
app.use("/", (req, res, next) => {
|
||||
requestsPerSec.mark();
|
||||
requestsPerMin.mark();
|
||||
next();
|
||||
});
|
||||
const sockets = io.metric({
|
||||
name: 'Open Websockets',
|
||||
id: 'app/requests/sockets',
|
||||
});
|
||||
// setInterval(()=>{
|
||||
|
||||
// }, 1000)
|
||||
import WebSocket, { WebSocketServer } from "ws";
|
||||
import { request } from "node:http";
|
||||
const wss = new WebSocketServer({ noServer: true });
|
||||
let openSockets = 0;
|
||||
wss.on("connection", function connection(ws, req, res) {
|
||||
@ -77,32 +61,11 @@ wss.on("connection", function connection(ws, req, res) {
|
||||
if (ws.id) {
|
||||
const updateAccount = accs.query(`UPDATE accounts SET last_login = $login WHERE username = $user`)
|
||||
updateAccount.get({ $login: new Date().toUTCString(), $user: ws.id });
|
||||
if (message.substring(4)) {
|
||||
const existingAccount = accs.query(`SELECT * FROM accounts WHERE username LIKE $1`)
|
||||
let userData = existingAccount.get({ $1: ws.id });
|
||||
if (userData == null) {
|
||||
return { success: false, reason: "The account doesn't exist." };
|
||||
}
|
||||
let games;
|
||||
if (userData.playedgames) {
|
||||
games = JSON.parse(userData.playedgames);
|
||||
} else {
|
||||
games = {};
|
||||
}
|
||||
if (games[message.substring(4)]) {
|
||||
games[message.substring(4)] += 30;
|
||||
} else {
|
||||
games[message.substring(4)] = 30;
|
||||
}
|
||||
const updateAccount = accs.query(`UPDATE accounts SET playedgames = $playedgames WHERE username = $user`)
|
||||
updateAccount.get({ $playedgames: JSON.stringify(games), $user: ws.id });
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
ws.on("close", () => {openSockets--;
|
||||
sockets.set(openSockets);});
|
||||
ws.on("close", () => {openSockets--;});
|
||||
});
|
||||
app.post(
|
||||
"/api/event",
|
||||
@ -120,11 +83,6 @@ app.post(
|
||||
})
|
||||
}
|
||||
);
|
||||
// app.use("*.json", async (req, res, next) => {
|
||||
// optimize json
|
||||
// console.log("got data");
|
||||
// next()
|
||||
// });
|
||||
app.post("/register", async (req, res) => {
|
||||
let status = await createAccount(req.body.username, req.body.password, req.body["h-captcha-response"]);
|
||||
if (status["success"]) {
|
||||
@ -477,6 +435,15 @@ app.post("/api/admin/ban", async (req, res) => {
|
||||
let status = await banUser(req.body.name, req.body.reason, req.cookies.token);
|
||||
res.status(200).send(status);
|
||||
});
|
||||
|
||||
|
||||
app.use("/api/music/search", async (req, res, next) => {
|
||||
res.status(200).send(await search(req.query.q))
|
||||
});
|
||||
app.use("/api/music/download", async (req, res, next) => {
|
||||
await download(req.query.url, res);
|
||||
});
|
||||
|
||||
const server = app.listen(port, () => {
|
||||
console.log(log.success("Express is online."));
|
||||
console.log("- " + log.info("http://localhost:" + port));
|
||||
|
Reference in New Issue
Block a user