backend: 拆分API路由到独立模块,优化日志记录方式,移除重复状态管理
This commit is contained in:
parent
6965c67de2
commit
e8886743f9
41
api.js
Normal file
41
api.js
Normal file
@ -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;
|
||||
47
app.js
47
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 => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user