From 777a98acb01b0b76361ea9bedc5e9593857f3efe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B8=A5=E5=AE=B6=E8=BE=89?=
Date: Sun, 7 Jul 2024 15:32:44 +0800
Subject: [PATCH 1/2] refactor: #42 Optimize project file structure
---
index.js | 44 ++------
middleware/cors.js | 7 ++
middleware/koaBody.js | 13 +++
public/index.html | 241 -----------------------------------------
utils/createInitDir.js | 14 +++
5 files changed, 41 insertions(+), 278 deletions(-)
create mode 100644 middleware/cors.js
create mode 100644 middleware/koaBody.js
delete mode 100755 public/index.html
create mode 100644 utils/createInitDir.js
diff --git a/index.js b/index.js
index 90eb979..6bc417d 100755
--- a/index.js
+++ b/index.js
@@ -1,62 +1,32 @@
// app.js
const Koa = require("koa");
-const { koaBody } = require("koa-body");
-const path = require("path");
-const fs = require("fs");
const sequelize = require("./utils/dbInstance");
const filesRouter = require("./routers/files");
const usersRouter = require("./routers/users");
const redisClient = require("./redis");
const authenticateToken = require("./middleware/authenticateToken");
-const cors = require("@koa/cors");
+const koaBody = require("./middleware/koaBody");
+const cors = require("./middleware/cors");
+const createInitDir = require("./utils/createInitDir");
+
require("./models");
require("./schedules/fileRecover");
require("dotenv").config({ path: ".env.local" });
const app = new Koa();
-app.use(
- cors({
- origin: "http://localhost:5173/", // 允许的来源
- allowMethods: ["GET", "POST"], // 允许的方法
- })
-);
-
-app.use(require("koa-static")(path.join(__dirname, "public")));
-
-const createDirectories = () => {
- const dirs = [
- path.join(__dirname, "provisional"),
- path.join(__dirname, "resource"),
- ];
- dirs.forEach((dir) => {
- if (!fs.existsSync(dir)) {
- fs.mkdirSync(dir, { recursive: true });
- }
- });
-};
-
-createDirectories();
+app.use(cors());
app.use(authenticateToken);
-app.use(
- koaBody({
- multipart: true,
- // 解决 DELETE 没法获取ids的问题
- parsedMethods: ["POST", "PUT", "PATCH", "DELETE"],
- formidable: {
- uploadDir: path.join(__dirname, "provisional"), // 临时上传目录
- keepExtensions: true, // 保留文件扩展名
- },
- })
-);
+app.use(koaBody());
// 挂载文件路由
app.use(usersRouter.routes()).use(usersRouter.allowedMethods());
app.use(filesRouter.routes()).use(filesRouter.allowedMethods());
app.listen(process.env.SERVER_PORT, async () => {
+ createInitDir();
await redisClient.connect();
await sequelize.sync();
console.log(`Server is running on ${process.env.INTERNAL_NETWORK_DOMAIN}`);
diff --git a/middleware/cors.js b/middleware/cors.js
new file mode 100644
index 0000000..55348f5
--- /dev/null
+++ b/middleware/cors.js
@@ -0,0 +1,7 @@
+const cors = require("@koa/cors");
+
+module.exports = () =>
+ cors({
+ origin: "http://localhost:5173/", // 允许的来源
+ allowMethods: ["GET", "POST"], // 允许的方法
+ });
diff --git a/middleware/koaBody.js b/middleware/koaBody.js
new file mode 100644
index 0000000..0bd79d9
--- /dev/null
+++ b/middleware/koaBody.js
@@ -0,0 +1,13 @@
+const { koaBody } = require("koa-body");
+const path = require("path");
+
+module.exports = () =>
+ koaBody({
+ multipart: true,
+ // 解决 DELETE 没法获取ids的问题
+ parsedMethods: ["POST", "PUT", "PATCH", "DELETE"],
+ formidable: {
+ uploadDir: path.join(__dirname, "..", "provisional"), // 临时上传目录
+ keepExtensions: true, // 保留文件扩展名
+ },
+ });
diff --git a/public/index.html b/public/index.html
deleted file mode 100755
index e22a6c8..0000000
--- a/public/index.html
+++ /dev/null
@@ -1,241 +0,0 @@
-
-
-
-
-
-
- 批量上传文件和图床
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/utils/createInitDir.js b/utils/createInitDir.js
new file mode 100644
index 0000000..88e1c34
--- /dev/null
+++ b/utils/createInitDir.js
@@ -0,0 +1,14 @@
+const path = require("path");
+const fs = require("fs");
+
+module.exports = () => {
+ const dirs = [
+ path.join(__dirname, "..", "provisional"),
+ path.join(__dirname, "..", "resource"),
+ ];
+ dirs.forEach((dir) => {
+ if (!fs.existsSync(dir)) {
+ fs.mkdirSync(dir, { recursive: true });
+ }
+ });
+};
From 8fb4bf5ae215401bf576634d9e36c6cc4b67628d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B8=A5=E5=AE=B6=E8=BE=89?=
Date: Sun, 7 Jul 2024 15:35:16 +0800
Subject: [PATCH 2/2] refactor: #42 Optimize project file structure
---
README-zh.md | 12 ++++++------
README.md | 12 ++++++------
2 files changed, 12 insertions(+), 12 deletions(-)
diff --git a/README-zh.md b/README-zh.md
index e1ac472..b3f2581 100644
--- a/README-zh.md
+++ b/README-zh.md
@@ -4,17 +4,17 @@
-
- English |
- 中文
-
-
+
+ English |
+ 中文
+
+
## 支持系统 💻
| Linux | MacOS | Windows |
@@ -79,4 +79,4 @@
## 其他文档 📖
-[Contribution Guide](./CONTRIBUTING.md) | [Security Policy](./SECURITY.md) | [Update Log](./CHANGELOG.md) | [Licence](./LICENSE)
+[贡献指南](./CONTRIBUTING.md) | [安全策略](./SECURITY.md) | [更新日志](./CHANGELOG.md) | [许可证](./LICENSE)
diff --git a/README.md b/README.md
index 88bd202..05df782 100755
--- a/README.md
+++ b/README.md
@@ -4,17 +4,17 @@
-
- English |
- 中文
-
-
+
+ English |
+ 中文
+
+
## Support System 💻
| Liunx | MacOS | Windows |
@@ -79,4 +79,4 @@
## Other Docs 📖
-[贡献指南](./CONTRIBUTING.md) | [安全策略](./SECURITY.md) | [更新日志](./CHANGELOG.md) | [许可证](./LICENSE)
+[Contribution Guide](./CONTRIBUTING.md) | [Security Policy](./SECURITY.md) | [Update Log](./CHANGELOG.md) | [Licence](./LICENSE)