Skip to content

Commit ad836de

Browse files
authored
Merge pull request #583 from WatWowMap/config-api-route
Add a config api route
2 parents 2d6d6db + 4d7f895 commit ad836de

File tree

6 files changed

+72
-18
lines changed

6 files changed

+72
-18
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ server/src/routes/api/v1/*
3333
!server/src/routes/api/v1/users.js
3434
!server/src/routes/api/v1/sessions.js
3535
!server/src/routes/api/v1/available.js
36+
!server/src/routes/api/v1/config.js
3637

3738
# custom model
3839
server/src/models/Custom.js

server/src/configs/custom-environment-variables.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@
2727
"api": {
2828
"sessionSecret": "API_SESSION_SECRET",
2929
"reactMapSecret": "API_REACT_MAP_SECRET",
30+
"showSchemasInConfigApi": {
31+
"__name": "API_SHOW_SCHEMAS_IN_CONFIG_API",
32+
"__format": "boolean"
33+
},
34+
"showStrategiesInConfigApi": {
35+
"__name": "API_SHOW_STRATEGIES_IN_CONFIG_API",
36+
"__format": "boolean"
37+
},
3038
"maxSessions": {
3139
"__name": "API_MAX_SESSIONS",
3240
"__format": "number"

server/src/configs/default.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
"api": {
1313
"sessionSecret": "98ki^e72~!@#(85o3kXLI*#c9wu5l!Z",
1414
"reactMapSecret": "",
15+
"showSchemasInConfigApi": false,
16+
"showStrategiesInConfigApi": false,
1517
"maxSessions": 5,
1618
"sessionCheckIntervalMs": 900000,
1719
"cookieAgeDays": 7,

server/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ app.use(express.static(path.join(__dirname, config.devOptions.clientPath)))
140140

141141
app.use(
142142
session({
143-
name: 'discord',
143+
name: 'reactmap',
144144
key: 'session',
145145
secret: config.api.sessionSecret,
146146
store: sessionStore,

server/src/routes/api/v1/config.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* eslint-disable no-console */
2+
const path = require('path')
3+
const router = require('express').Router()
4+
const config = require('../../../services/config')
5+
6+
router.get('/', (req, res) => {
7+
try {
8+
if (
9+
config.api.reactMapSecret &&
10+
req.headers['react-map-secret'] === config.api.reactMapSecret
11+
) {
12+
res.status(200).json({
13+
api: {
14+
...config.api,
15+
reactMapSecret: undefined,
16+
},
17+
...config,
18+
database: {
19+
...config.database,
20+
schemas: config.api.showSchemasInConfigApi
21+
? config.database.schemas
22+
: [],
23+
},
24+
authentication: {
25+
...config.authentication,
26+
strategies: config.api.showStrategiesInConfigApi
27+
? config.authentication.strategies
28+
: [],
29+
},
30+
})
31+
} else {
32+
throw new Error('Incorrect or missing API secret')
33+
}
34+
console.log(`[API] api/v1/${path.parse(__filename).name}`)
35+
} catch (e) {
36+
console.error(`[API Error] api/v1/${path.parse(__filename).name}`, e)
37+
res.status(500).json({ status: 'error', reason: e.message })
38+
}
39+
})
40+
41+
module.exports = router

server/src/services/sessionStore.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,25 @@ const { Session } = require('../models/index')
1414

1515
const dbSelection = schemas.find(({ useFor }) => useFor?.includes('session'))
1616

17-
const sessionStore = new MySQLStore(
18-
{
19-
clearExpired: true,
20-
checkExpirationInterval: sessionCheckIntervalMs,
21-
createDatabaseTable: true,
22-
schema: {
23-
tableName: sessionTableName,
24-
},
25-
},
26-
mysql2.createPool({
27-
host: dbSelection.host,
28-
port: dbSelection.port,
29-
user: dbSelection.username,
30-
password: dbSelection.password,
31-
database: dbSelection.database,
32-
}),
33-
)
17+
const sessionStore = dbSelection
18+
? new MySQLStore(
19+
{
20+
clearExpired: true,
21+
checkExpirationInterval: sessionCheckIntervalMs,
22+
createDatabaseTable: true,
23+
schema: {
24+
tableName: sessionTableName,
25+
},
26+
},
27+
mysql2.createPool({
28+
host: dbSelection.host,
29+
port: dbSelection.port,
30+
user: dbSelection.username,
31+
password: dbSelection.password,
32+
database: dbSelection.database,
33+
}),
34+
)
35+
: null
3436

3537
const isValidSession = async (userId) => {
3638
try {

0 commit comments

Comments
 (0)