Skip to content

Commit

Permalink
Merge pull request #11 from PaloMiku/main
Browse files Browse the repository at this point in the history
fix:启动前同步数据库的尝试
  • Loading branch information
RavelloH authored Oct 31, 2024
2 parents fdc3ce8 + 886d9b2 commit 1cf6822
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 14 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ yarn-error.log*
next-env.d.ts

.env
/.vscode
/.vscode
# prisma
/prisma/migrations
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "prisma generate && next dev",
"build": "prisma generate && next build",
"dev": "node script/check.js && next dev",
"build": "node script/build.js && next build",
"start": "next start",
"lint": "next lint"
},
Expand All @@ -26,6 +26,7 @@
"react-dom": "^18.3.1",
"ref-napi": "^3.0.3",
"remixicon": "^4.3.0",
"rlog-js": "^1.4.0",
"rss": "^1.2.2",
"sharp": "^0.33.4",
"vditor": "^3.10.4",
Expand Down
69 changes: 66 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions script/check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const RLog = require('rlog-js');
const { execSync } = require('child_process');
const { exit } = require('process');
const rlog = new RLog();

// 计时
const start = Date.now();

// Start build
rlog.info('Start build check...');

// 生成prisma client
rlog.info('Generate prisma client...');
try {
const stdout = execSync('npx prisma generate');
rlog.success(`Success: ${stdout.toString()}`);
} catch (error) {
rlog.error(`Error: ${error.message}`);
exit(1);
}
rlog.success('Prisma client generated.');

// 检测数据库是否存在
rlog.info('Checking remote database...');
try {
const stdout = execSync('npx prisma migrate status');
rlog.success(`Success: ${stdout.toString()}`);
} catch (error) {
rlog.error(`Error: ${error.message}`);
rlog.info('Syncing database...');
// 数据库没migration记录,需要创建
// 运行初始化脚本: init.js
try {
const stdout = execSync('node script/init.js');
rlog.success(`Success: ${stdout.toString()}`);
} catch (error) {
rlog.error(`Error: ${error.message}`);
exit(1);
}
}
rlog.success('Database chekced.');

// 同步数据库
rlog.info("Start sync database...");
const commit = new Date().toISOString();
try {
const stdout = execSync(`npx prisma migrate dev --name ${commit}`);
rlog.success(`Success: ${stdout.toString()}`);
} catch (error) {
rlog.error(`Error: ${error.message}`);
exit(1);
}
rlog.success('Database synced.');

const time = ((Date.now() - start) / 1000).toFixed(2);
rlog.success(`Build check completed in ${time}s.`);
15 changes: 15 additions & 0 deletions script/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const { execSync } = require('child_process');
const { exit } = require('process');
const RLog = require('rlog-js');
const rlog = new RLog();

// 初始化:强制同步数据库
try {
const stdout = execSync('npx prisma migrate dev --name init');
rlog.success(`Success: ${stdout.toString()}`);
} catch (error) {
rlog.error(`Error: ${error.message}`);
exit
}

// TODO: 生成一些默认数据,例如首篇hello world,站点设置默认值等
66 changes: 66 additions & 0 deletions src/app/api/site/build/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* POST /api/site/build
* WITH Authorization: Bearer <token>
*/

import prisma from '../../_utils/prisma';
import limitControl from '../../_utils/limitControl';
import token from '../../_utils/token';

export async function POST(request) {
try {
const authHeader = request.headers.get('Authorization');
if (!authHeader) {
return Response.json({ message: '缺少授权头信息' }, { status: 400 });
}

// 验证格式
if (!(await limitControl.check(request))) {
return Response.json({ message: '已触发速率限制' }, { status: 429 });
}
// 检查传入的token
const tokenString = authHeader.split(' ')[1];
let tokenInfo;
try {
tokenInfo = token.verify(tokenString);
} catch (err) {
if (err.name == 'TokenExpiredError') {
return Response.json(
{
message: 'TOKEN已过期,请重新登录',
},
{ status: 410 },
);
} else {
return Response.json(
{
message: 'TOKEN无效',
error: err,
},
{ status: 400 },
);
}
}

// 检查用户权限
if (tokenInfo) {
if (tokenInfo.role == "ADMIN") {
// TODO: 根据config中设置的平台触发相关Hook

return Response.json({ message: '成功(这是一条占位消息)' }, { status: 200 });
}
return Response.json({ message: '权限不足' }, { status: 400 });
}
return Response.json({ message: '请提供TOKEN' }, { status: 400 });
} catch (error) {
console.error(error);
return Response.json(
{
code: 500,
message: '500 Interal server error.',
error: error,
},
{ status: 500 },
);
}
}
Loading

0 comments on commit 1cf6822

Please sign in to comment.