Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor config #81

Merged
merged 3 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env → .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
Expand Down
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand 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` 会自动生成.
Expand Down
4 changes: 2 additions & 2 deletions config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 // 之所以两个只为了兼容旧版命名;请优先采用后者

Expand All @@ -65,7 +65,7 @@ config.mail = {
}

config.port = Number.parseInt(
process.env.PORT,
process.env.PORT || 3000,
)

module.exports = config
8 changes: 5 additions & 3 deletions config/website.js
Original file line number Diff line number Diff line change
@@ -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 || 'false').toLocaleLowerCase() === 'true',
semi_restful: (process.env.semiRestful || 'false').toLocaleLowerCase() === 'true',
}

/**
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
50 changes: 37 additions & 13 deletions manage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 !

Expand Down Expand Up @@ -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
Expand All @@ -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()
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 .",
Expand Down Expand Up @@ -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",
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

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

Loading