Skip to content

Commit 41b6731

Browse files
author
amitb0ra
committed
refactor: env-var overrides, error handling, and cleanup for testability
- app.js: env-var path overrides (LEADERBOARD_CONFIG_PATH, LEADERBOARD_DATA_PATH, LEADERBOARD_LOG_PATH, LEADERBOARD_ADMINDATA_PATH, LEADERBOARD_CONFIG_BACKUP_PATH, LEADERBOARD_PORT), LEADERBOARD_SKIP_REFRESH to disable child process, pass res to Util.post calls, fix hardcoded ./admindata.json in login handler, export server - Util.js: post(req, res, callback) — respond 400 on malformed JSON instead of silently hanging, move findContributor from app.js - API.js: remove process.exit() on bad credentials — log error and return instead, support LEADERBOARD_CONFIG_PATH env var - Remove dead .dockerignore (Docker support removed previously) - Remove stale tests/access.sh and tests/api-call-tests.sh (brute-force scripts hitting dead external URLs)
1 parent 2375ec7 commit 41b6731

6 files changed

Lines changed: 46 additions & 68 deletions

File tree

.dockerignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/server/app.js

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ const app = express()
1010
const proxy = require('http-proxy-middleware')
1111
const path = require('path')
1212

13-
const configPath = './config.json'
14-
const admindataPath = './admindata.json'
15-
const dataPath = '../assets/data/data.json'
16-
const logPath = '../assets/data/log.json'
17-
const port = jsonfile.readFileSync(configPath).serverPort
18-
const configBackupPath = '../../configBackup.json'
13+
const configPath = process.env.LEADERBOARD_CONFIG_PATH || './config.json'
14+
const admindataPath = process.env.LEADERBOARD_ADMINDATA_PATH || './admindata.json'
15+
const dataPath = process.env.LEADERBOARD_DATA_PATH || '../assets/data/data.json'
16+
const logPath = process.env.LEADERBOARD_LOG_PATH || '../assets/data/log.json'
17+
const port = process.env.LEADERBOARD_PORT || jsonfile.readFileSync(configPath).serverPort
18+
const configBackupPath = process.env.LEADERBOARD_CONFIG_BACKUP_PATH || '../../configBackup.json'
1919
const proxyOption = {
2020
target: 'http://localhost:' + port + '/',
2121
pathRewrite: { '^/api': '' },
@@ -55,13 +55,15 @@ if (!fs.existsSync(admindataPath)) {
5555
}
5656

5757
// spawn - `node refresh.js`
58-
const refresh = spawn('node', ['refresh.js'], {
59-
shell: true,
60-
stdio: 'inherit',
61-
})
62-
process.on('exit', () => {
63-
refresh.kill() // kill it when exit
64-
})
58+
if (process.env.LEADERBOARD_SKIP_REFRESH !== '1') {
59+
const refresh = spawn('node', ['refresh.js'], {
60+
shell: true,
61+
stdio: 'inherit',
62+
})
63+
process.on('exit', () => {
64+
refresh.kill() // kill it when exit
65+
})
66+
}
6567

6668
const server = http
6769
.createServer((req, res) => {
@@ -104,12 +106,12 @@ const server = http
104106
)
105107
var contributorsList = []
106108

107-
Util.post(req, async (params) => {
109+
Util.post(req, res, async (params) => {
108110
const { token } = params
109111
if (token === adminPassword) {
110112
await Promise.all(
111113
contributors.map(async (contributor) => {
112-
const admindata = jsonfile.readFileSync('./admindata.json')
114+
const admindata = jsonfile.readFileSync(admindataPath)
113115
const existContributor = findContributor(
114116
contributor,
115117
admindata
@@ -176,7 +178,7 @@ const server = http
176178
return
177179
}
178180

179-
Util.post(req, (params) => {
181+
Util.post(req, res, (params) => {
180182
const { token, includedRepositories } = params
181183

182184
if (token !== adminPassword) {
@@ -197,7 +199,7 @@ const server = http
197199
res.end('Permission denied\n')
198200
return
199201
}
200-
Util.post(req, (params) => {
202+
Util.post(req, res, (params) => {
201203
const { token, startDate } = params
202204

203205
if (token !== adminPassword) {
@@ -219,7 +221,7 @@ const server = http
219221
return
220222
}
221223

222-
Util.post(req, (params) => {
224+
Util.post(req, res, (params) => {
223225
const { token, interval } = params
224226

225227
if (token !== adminPassword) {
@@ -241,7 +243,7 @@ const server = http
241243
return
242244
}
243245

244-
Util.post(req, (params) => {
246+
Util.post(req, res, (params) => {
245247
const { token, username } = params
246248

247249
if (token !== adminPassword) {
@@ -272,7 +274,7 @@ const server = http
272274
return
273275
}
274276

275-
Util.post(req, (params) => {
277+
Util.post(req, res, (params) => {
276278
const { token, username } = params
277279

278280
if (token !== adminPassword) {
@@ -410,6 +412,8 @@ const server = http
410412
})
411413
.listen(port)
412414

415+
module.exports = { server }
416+
413417
const io = require('socket.io')(server)
414418
io.on('connection', (socket) => {
415419
const intervalId = setInterval(() => {
@@ -423,14 +427,4 @@ io.on('connection', (socket) => {
423427
})
424428
})
425429

426-
function findContributor(contributorName, admindata) {
427-
let result = null
428-
429-
admindata.forEach((contributor) => {
430-
if (contributor.username === contributorName) {
431-
result = contributor
432-
}
433-
})
434-
435-
return result
436-
}
430+
const findContributor = Util.findContributor

src/server/util/API.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
const axios = require('axios')
2-
const Config = require('../config.json')
32
const chalk = require('chalk')
43

4+
const Config = process.env.LEADERBOARD_CONFIG_PATH
5+
? require(require('path').resolve(process.env.LEADERBOARD_CONFIG_PATH))
6+
: require('../config.json')
7+
58
const BASEURL = 'https://github.com'
69
const APIHOST = 'https://api.github.com'
710

@@ -35,7 +38,6 @@ async function get(url, _authToken) {
3538
'[ERROR] Your GitHub Token is not correct! Please check it in the config.json.'
3639
)
3740
)
38-
process.exit()
3941
break
4042
default:
4143
console.log(chalk.yellow('[WARNING] ' + message))

src/server/util/Util.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function post(req, callback) {
1+
function post(req, res, callback) {
22
if(req.method === 'POST') {
33
let body = ''
44

@@ -10,12 +10,26 @@ function post(req, callback) {
1010
try {
1111
callback(JSON.parse(body))
1212
} catch (ex) {
13-
return
13+
res.writeHead(400, { 'Content-Type': 'application/json' })
14+
res.end(JSON.stringify({ message: 'Invalid JSON body' }))
1415
}
1516
})
1617
}
1718
}
1819

20+
function findContributor(contributorName, admindata) {
21+
let result = null
22+
23+
admindata.forEach((contributor) => {
24+
if (contributor.username === contributorName) {
25+
result = contributor
26+
}
27+
})
28+
29+
return result
30+
}
31+
1932
module.exports = {
20-
post
33+
post,
34+
findContributor
2135
}

tests/access.sh

Lines changed: 0 additions & 18 deletions
This file was deleted.

tests/api-call-tests.sh

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)