Skip to content

Commit c177573

Browse files
authored
Merge pull request #43 from diskcloud/feature/42
refactor: #42 Optimize project file structure
2 parents d0f22e5 + 8fb4bf5 commit c177573

File tree

7 files changed

+53
-290
lines changed

7 files changed

+53
-290
lines changed

README-zh.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
</a>
55
</p>
66

7-
<p align="center">
8-
<a href="./README.md">English</a> |
9-
<a href="./README-zh.md">中文</a>
10-
</p>
11-
127
<p align="center">
138
<img alt="GitHub repo size" src="https://img.shields.io/github/repo-size/diskcloud/service">
149
<img alt="GitHub Issues or Pull Requests" src="https://img.shields.io/github/issues-pr/diskcloud/service">
1510
<img alt="GitHub Issues or Pull Requests" src="https://img.shields.io/github/issues/diskcloud/service">
1611
</p>
1712

13+
<p align="center">
14+
<a href="./README.md">English</a> |
15+
<a href="./README-zh.md">中文</a>
16+
</p>
17+
1818
## 支持系统 💻
1919

2020
| Linux | MacOS | Windows |
@@ -79,4 +79,4 @@
7979

8080
## 其他文档 📖
8181

82-
[Contribution Guide](./CONTRIBUTING.md) | [Security Policy](./SECURITY.md) | [Update Log](./CHANGELOG.md) | [Licence](./LICENSE)
82+
[贡献指南](./CONTRIBUTING.md) | [安全策略](./SECURITY.md) | [更新日志](./CHANGELOG.md) | [许可证](./LICENSE)

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
</a>
55
</p>
66

7-
<p align="center">
8-
<a href="./README.md">English</a> |
9-
<a href="./README-zh.md">中文</a>
10-
</p>
11-
127
<p align="center">
138
<img alt="GitHub repo size" src="https://img.shields.io/github/repo-size/diskcloud/service">
149
<img alt="GitHub Issues or Pull Requests" src="https://img.shields.io/github/issues-pr/diskcloud/service">
1510
<img alt="GitHub Issues or Pull Requests" src="https://img.shields.io/github/issues/diskcloud/service">
1611
</p>
1712

13+
<p align="center">
14+
<a href="./README.md">English</a> |
15+
<a href="./README-zh.md">中文</a>
16+
</p>
17+
1818
## Support System 💻
1919

2020
| Liunx | MacOS | Windows |
@@ -79,4 +79,4 @@
7979

8080
## Other Docs 📖
8181

82-
[贡献指南](./CONTRIBUTING.md) | [安全策略](./SECURITY.md) | [更新日志](./CHANGELOG.md) | [许可证](./LICENSE)
82+
[Contribution Guide](./CONTRIBUTING.md) | [Security Policy](./SECURITY.md) | [Update Log](./CHANGELOG.md) | [Licence](./LICENSE)

index.js

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,32 @@
11
// app.js
22
const Koa = require("koa");
3-
const { koaBody } = require("koa-body");
4-
const path = require("path");
5-
const fs = require("fs");
63
const sequelize = require("./utils/dbInstance");
74
const filesRouter = require("./routers/files");
85
const usersRouter = require("./routers/users");
96
const redisClient = require("./redis");
107
const authenticateToken = require("./middleware/authenticateToken");
11-
const cors = require("@koa/cors");
8+
const koaBody = require("./middleware/koaBody");
9+
const cors = require("./middleware/cors");
10+
const createInitDir = require("./utils/createInitDir");
11+
1212
require("./models");
1313
require("./schedules/fileRecover");
1414
require("dotenv").config({ path: ".env.local" });
1515

1616
const app = new Koa();
1717

18-
app.use(
19-
cors({
20-
origin: "http://localhost:5173/", // 允许的来源
21-
allowMethods: ["GET", "POST"], // 允许的方法
22-
})
23-
);
24-
25-
app.use(require("koa-static")(path.join(__dirname, "public")));
26-
27-
const createDirectories = () => {
28-
const dirs = [
29-
path.join(__dirname, "provisional"),
30-
path.join(__dirname, "resource"),
31-
];
32-
dirs.forEach((dir) => {
33-
if (!fs.existsSync(dir)) {
34-
fs.mkdirSync(dir, { recursive: true });
35-
}
36-
});
37-
};
38-
39-
createDirectories();
18+
app.use(cors());
4019

4120
app.use(authenticateToken);
4221

43-
app.use(
44-
koaBody({
45-
multipart: true,
46-
// 解决 DELETE 没法获取ids的问题
47-
parsedMethods: ["POST", "PUT", "PATCH", "DELETE"],
48-
formidable: {
49-
uploadDir: path.join(__dirname, "provisional"), // 临时上传目录
50-
keepExtensions: true, // 保留文件扩展名
51-
},
52-
})
53-
);
22+
app.use(koaBody());
5423

5524
// 挂载文件路由
5625
app.use(usersRouter.routes()).use(usersRouter.allowedMethods());
5726
app.use(filesRouter.routes()).use(filesRouter.allowedMethods());
5827

5928
app.listen(process.env.SERVER_PORT, async () => {
29+
createInitDir();
6030
await redisClient.connect();
6131
await sequelize.sync();
6232
console.log(`Server is running on ${process.env.INTERNAL_NETWORK_DOMAIN}`);

middleware/cors.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const cors = require("@koa/cors");
2+
3+
module.exports = () =>
4+
cors({
5+
origin: "http://localhost:5173/", // 允许的来源
6+
allowMethods: ["GET", "POST"], // 允许的方法
7+
});

middleware/koaBody.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const { koaBody } = require("koa-body");
2+
const path = require("path");
3+
4+
module.exports = () =>
5+
koaBody({
6+
multipart: true,
7+
// 解决 DELETE 没法获取ids的问题
8+
parsedMethods: ["POST", "PUT", "PATCH", "DELETE"],
9+
formidable: {
10+
uploadDir: path.join(__dirname, "..", "provisional"), // 临时上传目录
11+
keepExtensions: true, // 保留文件扩展名
12+
},
13+
});

public/index.html

Lines changed: 0 additions & 241 deletions
This file was deleted.

0 commit comments

Comments
 (0)