Skip to content

Commit

Permalink
Merge pull request #43 from diskcloud/feature/42
Browse files Browse the repository at this point in the history
refactor: #42 Optimize project file structure
  • Loading branch information
CrazyMrYan authored Jul 7, 2024
2 parents d0f22e5 + 8fb4bf5 commit c177573
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 290 deletions.
12 changes: 6 additions & 6 deletions README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
</a>
</p>

<p align="center">
<a href="./README.md">English</a> |
<a href="./README-zh.md">中文</a>
</p>

<p align="center">
<img alt="GitHub repo size" src="https://img.shields.io/github/repo-size/diskcloud/service">
<img alt="GitHub Issues or Pull Requests" src="https://img.shields.io/github/issues-pr/diskcloud/service">
<img alt="GitHub Issues or Pull Requests" src="https://img.shields.io/github/issues/diskcloud/service">
</p>

<p align="center">
<a href="./README.md">English</a> |
<a href="./README-zh.md">中文</a>
</p>

## 支持系统 💻

| Linux | MacOS | Windows |
Expand Down Expand Up @@ -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)
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
</a>
</p>

<p align="center">
<a href="./README.md">English</a> |
<a href="./README-zh.md">中文</a>
</p>

<p align="center">
<img alt="GitHub repo size" src="https://img.shields.io/github/repo-size/diskcloud/service">
<img alt="GitHub Issues or Pull Requests" src="https://img.shields.io/github/issues-pr/diskcloud/service">
<img alt="GitHub Issues or Pull Requests" src="https://img.shields.io/github/issues/diskcloud/service">
</p>

<p align="center">
<a href="./README.md">English</a> |
<a href="./README-zh.md">中文</a>
</p>

## Support System 💻

| Liunx | MacOS | Windows |
Expand Down Expand Up @@ -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)
44 changes: 7 additions & 37 deletions index.js
Original file line number Diff line number Diff line change
@@ -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}`);
Expand Down
7 changes: 7 additions & 0 deletions middleware/cors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const cors = require("@koa/cors");

module.exports = () =>
cors({
origin: "http://localhost:5173/", // 允许的来源
allowMethods: ["GET", "POST"], // 允许的方法
});
13 changes: 13 additions & 0 deletions middleware/koaBody.js
Original file line number Diff line number Diff line change
@@ -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, // 保留文件扩展名
},
});
241 changes: 0 additions & 241 deletions public/index.html

This file was deleted.

Loading

0 comments on commit c177573

Please sign in to comment.