-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from PaloMiku/main
fix:启动前同步数据库的尝试
- Loading branch information
Showing
7 changed files
with
228 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,4 +36,6 @@ yarn-error.log* | |
next-env.d.ts | ||
|
||
.env | ||
/.vscode | ||
/.vscode | ||
# prisma | ||
/prisma/migrations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,站点设置默认值等 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }, | ||
); | ||
} | ||
} |
Oops, something went wrong.