From e8886743f97444a92e59267c818e7946e91a53dd Mon Sep 17 00:00:00 2001 From: starlight_0208 <2682994272@qq.com> Date: Thu, 17 Jul 2025 15:28:50 +0800 Subject: [PATCH] =?UTF-8?q?backend:=20=E6=8B=86=E5=88=86API=E8=B7=AF?= =?UTF-8?q?=E7=94=B1=E5=88=B0=E7=8B=AC=E7=AB=8B=E6=A8=A1=E5=9D=97=EF=BC=8C?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=97=A5=E5=BF=97=E8=AE=B0=E5=BD=95=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=EF=BC=8C=E7=A7=BB=E9=99=A4=E9=87=8D=E5=A4=8D=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api.js | 41 +++++++++++++++++++++++++++++++++++++++++ app.js | 47 +++++++++-------------------------------------- data.js | 2 ++ 3 files changed, 52 insertions(+), 38 deletions(-) create mode 100644 api.js create mode 100644 data.js diff --git a/api.js b/api.js new file mode 100644 index 0000000..c4f76d5 --- /dev/null +++ b/api.js @@ -0,0 +1,41 @@ +import express from 'express'; +import { loadHistory, getDatabaseState } from "./storage.js"; +import { onlineList } from './data.js'; + +const router = express.Router(); + +router.get("/online", (req, res) => { + res.status(200).send({ + online: onlineList.length, + user: onlineList + }) +}) + +router.get("/history", (req, res) => { + if (!getDatabaseState()) { + res.status(500).send({ + status: 500, + msg: "database is not ready", + data: null + }) + } + if (!req.query.limit || isNaN(req.query.limit) || req.query.limit <= 0 || req.query.limit > 100) { + req.query.limit = 10; + } + loadHistory(req.query.limit).then((data) => { + res.status(200).send({ + status: 200, + msg: "success", + data: data + }) + }).catch((err) => { + logger.error(`database error: ${err}`) + res.status(500).send({ + status: 500, + msg: "database error", + data: null + }) + }) +}) + +export default router; \ No newline at end of file diff --git a/app.js b/app.js index dc2a82e..7bd96af 100644 --- a/app.js +++ b/app.js @@ -1,8 +1,10 @@ import express from "express"; import http from "http"; import log4js from "log4js"; +import apiRouter from "./api.js" import { Server } from "socket.io"; -import { loadHistory, saveToDatabase, queryHistory, getDatabaseState } from "./storage.js"; +import { saveToDatabase } from "./storage.js"; +import { onlineList, clientMap } from "./data.js"; const PORT = process.env.PORT || 5691; @@ -19,11 +21,12 @@ const io = new Server(server, { const logger = log4js.getLogger("app"); app.use(express.static("static")) +app.use(apiRouter) logger.level = process.env.LOG_LEVEL || "debug"; // 初始化在线列表 -let onlineList = []; -let clientMap = new Map(); +// let onlineList = []; +// let clientMap = new Map(); class Response { constructor(status, msg) { @@ -32,39 +35,7 @@ class Response { } } -app.get("/online", (req, res) => { - res.status(200).send({ - online: onlineList.length, - user: onlineList - }) -}) -app.get("/history", (req, res) => { - if (!getDatabaseState()) { - res.status(500).send({ - status: 500, - msg: "database is not ready", - data: null - }) - } - if (!req.query.limit || isNaN(req.query.limit) || req.query.limit <= 0 || req.query.limit > 100) { - req.query.limit = 10; - } - loadHistory(req.query.limit).then((data) => { - res.status(200).send({ - status: 200, - msg: "success", - data: data - }) - }).catch((err) => { - logger.error(`database error: ${err}`) - res.status(500).send({ - status: 500, - msg: "database error", - data: null - }) - }) -}) io.on("connection", (socket) => { logger.info(`A client connected, id: ${socket.id}`) @@ -79,7 +50,7 @@ io.on("connection", (socket) => { } try { const { opt, args } = JSON.parse(data); - console.log(opt, args) + logger.trace(opt, args) if (opt == "signin") { if (!args || !args.username) { socket.emit("system", JSON.stringify(new Response(false, "username is empty."))) @@ -107,8 +78,8 @@ io.on("connection", (socket) => { socket.emit("system", JSON.stringify(new Response(false, "data payload is invaild."))) return; } - console.log(clientMap) - console.log(onlineList) + logger.trace(clientMap) + logger.trace(onlineList) }) socket.on("msg", data => { diff --git a/data.js b/data.js new file mode 100644 index 0000000..1348643 --- /dev/null +++ b/data.js @@ -0,0 +1,2 @@ +export var onlineList = []; +export var clientMap = new Map(); \ No newline at end of file