From 930e1e20a10d5e656dcb112f501477b3caf781a2 Mon Sep 17 00:00:00 2001 From: lazzzis Date: Thu, 3 Oct 2024 21:21:10 +0800 Subject: [PATCH 1/3] refactor: refactor config files --- .env => .env.sample | 4 ++ .gitignore | 5 +- README.md | 27 +++++----- config/index.js | 2 +- config/website.js | 8 +-- .../657a2950-e753-11e7-b5d5-acbc32d0fe0d.in | 0 .../657a2950-e753-11e7-b5d5-acbc32d0fe0d.out | 0 data/{1000 => example}/README.md | 0 .../aaa8a3d0-e753-11e7-83cc-9f6f17c0d58c.in | 0 .../aaa8a3d0-e753-11e7-83cc-9f6f17c0d58c.out | 0 data/{1000 => example}/ac/2.cpp | 0 data/{1000 => example}/meta.json | 0 manage.js | 50 ++++++++++++++----- package.json | 1 + pnpm-lock.yaml | 9 ++++ 15 files changed, 74 insertions(+), 32 deletions(-) rename .env => .env.sample (69%) rename data/{1000 => example}/657a2950-e753-11e7-b5d5-acbc32d0fe0d.in (100%) rename data/{1000 => example}/657a2950-e753-11e7-b5d5-acbc32d0fe0d.out (100%) rename data/{1000 => example}/README.md (100%) rename data/{1000 => example}/aaa8a3d0-e753-11e7-83cc-9f6f17c0d58c.in (100%) rename data/{1000 => example}/aaa8a3d0-e753-11e7-83cc-9f6f17c0d58c.out (100%) rename data/{1000 => example}/ac/2.cpp (100%) rename data/{1000 => example}/meta.json (100%) diff --git a/.env b/.env.sample similarity index 69% rename from .env rename to .env.sample index 374a53f..43ffda4 100644 --- a/.env +++ b/.env.sample @@ -4,4 +4,8 @@ secretKey='Putong Putong Putong' dbURL='mongodb://127.0.0.1:27017/oj' redisURL='redis://127.0.0.1:6379' +title='Putong OJ' +discussOnProblem=true +semiRestful=false + PORT=3000 diff --git a/.gitignore b/.gitignore index 63138be..57f7286 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,11 @@ docker-compose.debug.yml +.env +data/* +!data/example # customary .tern-project requests/ -data/* -!data/1000 migrations/db migrations/backup !migrations/db/.gitkeep diff --git a/README.md b/README.md index cc1efed..9745fa6 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,13 @@ Currently Not available git clone https://github.com/acm309/PutongOJ.git PutongOJ ``` -2. compose up +2. 配置环境变量 + +```bash +mv .env.sample .env +``` + +3. compose up ```bash docker-compose up @@ -80,27 +86,22 @@ npm i -g pnpm pnpm i ``` -5. 尝试连接数据库 - -设置环境变量 +5. 配置环境变量 ```bash -export redisURL='your redis url' -export dbURL='your mongodb url' +mv .env.sample .env ``` -更新 `config/index.js` - -```js -const prod = { - port: 3000 // the port the application will listen on -} +```bash +redisURL='your redis url' +dbURL='your mongodb url' +port=3000 #the port the application will listen on ``` 6. 下载静态文件和启动判题端 ```bash -node manager.js +node manager.js setup ``` `pm2.config.json` 会自动生成. diff --git a/config/index.js b/config/index.js index 3ecc9fb..12b35ce 100644 --- a/config/index.js +++ b/config/index.js @@ -65,7 +65,7 @@ config.mail = { } config.port = Number.parseInt( - process.env.PORT, + process.env.PORT || 3000, ) module.exports = config diff --git a/config/website.js b/config/website.js index 660c306..7f933d0 100644 --- a/config/website.js +++ b/config/website.js @@ -1,7 +1,9 @@ +const process = require('node:process') + module.exports = { - title: 'Putong OJ', - discussOnProblem: true, - semi_restful: false, + title: process.env.title || 'Putong OJ', + discussOnProblem: process.env.discussOnProblem == null ? false : process.env.discussOnProblem.toLocaleLowerCase() === 'true', + semi_restful: process.env.semiRestful == null ? false : process.env.semiRestful.toLocaleLowerCase() === 'true', } /** diff --git a/data/1000/657a2950-e753-11e7-b5d5-acbc32d0fe0d.in b/data/example/657a2950-e753-11e7-b5d5-acbc32d0fe0d.in similarity index 100% rename from data/1000/657a2950-e753-11e7-b5d5-acbc32d0fe0d.in rename to data/example/657a2950-e753-11e7-b5d5-acbc32d0fe0d.in diff --git a/data/1000/657a2950-e753-11e7-b5d5-acbc32d0fe0d.out b/data/example/657a2950-e753-11e7-b5d5-acbc32d0fe0d.out similarity index 100% rename from data/1000/657a2950-e753-11e7-b5d5-acbc32d0fe0d.out rename to data/example/657a2950-e753-11e7-b5d5-acbc32d0fe0d.out diff --git a/data/1000/README.md b/data/example/README.md similarity index 100% rename from data/1000/README.md rename to data/example/README.md diff --git a/data/1000/aaa8a3d0-e753-11e7-83cc-9f6f17c0d58c.in b/data/example/aaa8a3d0-e753-11e7-83cc-9f6f17c0d58c.in similarity index 100% rename from data/1000/aaa8a3d0-e753-11e7-83cc-9f6f17c0d58c.in rename to data/example/aaa8a3d0-e753-11e7-83cc-9f6f17c0d58c.in diff --git a/data/1000/aaa8a3d0-e753-11e7-83cc-9f6f17c0d58c.out b/data/example/aaa8a3d0-e753-11e7-83cc-9f6f17c0d58c.out similarity index 100% rename from data/1000/aaa8a3d0-e753-11e7-83cc-9f6f17c0d58c.out rename to data/example/aaa8a3d0-e753-11e7-83cc-9f6f17c0d58c.out diff --git a/data/1000/ac/2.cpp b/data/example/ac/2.cpp similarity index 100% rename from data/1000/ac/2.cpp rename to data/example/ac/2.cpp diff --git a/data/1000/meta.json b/data/example/meta.json similarity index 100% rename from data/1000/meta.json rename to data/example/meta.json diff --git a/manage.js b/manage.js index 8fceed7..375069d 100644 --- a/manage.js +++ b/manage.js @@ -7,8 +7,11 @@ const range = require('lodash.range') // DO not upgrade execa to v6.0.0 as it is esm module const { command: execaCommand } = require('execa') const fetch = require('node-fetch') +const { Command } = require('commander') const config = require('./config') +const program = new Command() + // download and initialize the judgers // start ! @@ -101,7 +104,7 @@ async function judgeSetup () { return fse.outputJSON('pm2.config.json', pm2config, { spaces: 2, EOL: '\n' }) } -async function staticFilesSetUp () { +async function staticFilesSetup () { const res = await fetch('https://api.github.com/repos/acm309/PutongOJ-FE/releases') const json = await res.json() const url = json[0].assets[0].browser_download_url @@ -112,19 +115,40 @@ async function staticFilesSetUp () { }) } -function main () { - return Promise.all([ - judgeSetup(), - staticFilesSetUp(), - ]) +async function exampleProblemSetUp () { + fse.move('data/example', 'data/1000') } -main() - .then(() => { - console.log('ok') - process.exit(0) +program.command('setup') + .action(() => { + console.log('setup...') + Promise.all([ + exampleProblemSetUp(), + judgeSetup(), + staticFilesSetup(), + ]) + .then(() => { + console.log('ok') + process.exit(0) + }) + .catch((err) => { + console.error(err) + process.exit(-1) + }) }) - .catch((err) => { - console.error(err) - process.exit(-1) + +program.command('update-fe') + .action(() => { + console.log('updating fe...') + staticFilesSetup() + .then(() => { + console.log('ok') + process.exit(0) + }) + .catch((err) => { + console.error(err) + process.exit(-1) + }) }) + +program.parse() diff --git a/package.json b/package.json index 5bbd3c4..dfa2a11 100644 --- a/package.json +++ b/package.json @@ -67,6 +67,7 @@ "devDependencies": { "ava": "^4.3.3", "codecov": "^3.8.3", + "commander": "^12.1.0", "eslint-config-standard": "^11.0.0", "eslint-plugin-import": "^2.30.0", "eslint-plugin-node": "^6.0.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 778cc51..c3caf14 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -105,6 +105,9 @@ importers: codecov: specifier: ^3.8.3 version: 3.8.3 + commander: + specifier: ^12.1.0 + version: 12.1.0 eslint-config-standard: specifier: ^11.0.0 version: 11.0.0(eslint-plugin-import@2.30.0(@typescript-eslint/parser@8.7.0(eslint@8.57.1)(typescript@4.9.5))(eslint@8.57.1))(eslint-plugin-node@6.0.1(eslint@8.57.1))(eslint-plugin-promise@3.8.0)(eslint-plugin-standard@3.1.0(eslint@8.57.1))(eslint@8.57.1) @@ -1316,6 +1319,10 @@ packages: resolution: {integrity: sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==} engines: {node: '>= 0.8'} + commander@12.1.0: + resolution: {integrity: sha512-Vw8qHK3bZM9y/P10u3Vib8o/DdkvA2OtPtZvD871QKjy74Wj1WSKFILMPRPSdUSx5RFK1arlJzEtA4PkFgnbuA==} + engines: {node: '>=18'} + commander@2.15.1: resolution: {integrity: sha512-VlfT9F3V0v+jr4yxPc5gg9s62/fIVWsd2Bk2iD435um1NlGMYdVCq+MjcXnhYq2icNOizHr1kK+5TI6H0Hy0ag==} @@ -6379,6 +6386,8 @@ snapshots: dependencies: delayed-stream: 1.0.0 + commander@12.1.0: {} + commander@2.15.1: {} comment-parser@1.4.1: {} From f3ea847d2f919190f8881253389d9365ce55f9fb Mon Sep 17 00:00:00 2001 From: lazzzis Date: Thu, 3 Oct 2024 21:27:01 +0800 Subject: [PATCH 2/3] fix: fix typo --- manage.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/manage.js b/manage.js index 375069d..4803695 100644 --- a/manage.js +++ b/manage.js @@ -115,7 +115,7 @@ async function staticFilesSetup () { }) } -async function exampleProblemSetUp () { +async function exampleProblemSetup () { fse.move('data/example', 'data/1000') } @@ -123,7 +123,7 @@ program.command('setup') .action(() => { console.log('setup...') Promise.all([ - exampleProblemSetUp(), + exampleProblemSetup(), judgeSetup(), staticFilesSetup(), ]) From 813b10faa0e0ed7f724d360feb03009ef97bd554 Mon Sep 17 00:00:00 2001 From: luoingly Date: Mon, 24 Feb 2025 15:41:47 +0800 Subject: [PATCH 3/3] ci: fix --- config/index.js | 2 +- config/website.js | 4 ++-- package.json | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/config/index.js b/config/index.js index 12b35ce..f52796f 100644 --- a/config/index.js +++ b/config/index.js @@ -48,7 +48,7 @@ config.deploy = { adminInitPwd: 'kplkplkpl', } -config.secretKey = process.env.secretKey +config.secretKey = process.env.secretKey || '' config.dbURL = process.env.DBURL || process.env.dbURL // 之所以两个只为了兼容旧版命名;请优先采用后者 diff --git a/config/website.js b/config/website.js index 7f933d0..cd77481 100644 --- a/config/website.js +++ b/config/website.js @@ -2,8 +2,8 @@ const process = require('node:process') module.exports = { title: process.env.title || 'Putong OJ', - discussOnProblem: process.env.discussOnProblem == null ? false : process.env.discussOnProblem.toLocaleLowerCase() === 'true', - semi_restful: process.env.semiRestful == null ? false : process.env.semiRestful.toLocaleLowerCase() === 'true', + discussOnProblem: (process.env.discussOnProblem || 'false').toLocaleLowerCase() === 'true', + semi_restful: (process.env.semiRestful || 'false').toLocaleLowerCase() === 'true', } /** diff --git a/package.json b/package.json index dfa2a11..4447087 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "deploy:init": "cross-env NODE_ENV=production node manage.js", "deploy:start": "npx pm2 start pm2.config.json", "pretest": "cross-env NODE_ENV=test node test/pretest.js", - "test": "cross-env NODE_ENV=test node test/pretest.js && nyc ava && node test/posttest.js", + "test": "cross-env-shell NODE_ENV=test \"node test/pretest.js && nyc ava && node test/posttest.js\"", "report": "nyc report --reporter=html", "posttest": "cross-env NODE_ENV=test node test/posttest.js", "lint": "eslint .",