Skip to content

Commit d457892

Browse files
authored
Merge pull request #40 from oss-slu/centralize_mongo_env
Centralize mongo env
2 parents 8ce3ad5 + a06e7d1 commit d457892

24 files changed

Lines changed: 160 additions & 108 deletions

app.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
import express from 'express'
44
import path from 'path'
55
import cookieParser from 'cookie-parser'
6-
import dotenv from 'dotenv'
7-
dotenv.config()
6+
import config from './config/index.js'
87
import logger from 'morgan'
98
import cors from 'cors'
109
import indexRouter from './routes/index.js'
@@ -71,7 +70,7 @@ app.use(express.static(path.join(__dirname, 'public')))
7170
* This is without middleware
7271
*/
7372
app.all('*_', (req, res, next) => {
74-
if(process.env.DOWN === "true"){
73+
if(config.DOWN === "true"){
7574
res.status(503).json({"message":"RERUM v1 is down for updates or maintenance at this time. We apologize for the inconvenience. Try again later."})
7675
}
7776
else{

auth/index.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { auth } from 'express-oauth2-jwt-bearer'
2-
import dotenv from 'dotenv'
3-
dotenv.config()
2+
import config from '../config/index.js'
43

54
const _tokenError = function (err, req, res, next) {
65
if(!err.code || err.code !== "invalid_token"){
@@ -55,10 +54,10 @@ const generateNewAccessToken = async (req, res, next) => {
5554
console.log("RERUM v1 is generating a proxy access token.")
5655
const form = {
5756
grant_type: 'refresh_token',
58-
client_id: process.env.CLIENT_ID,
59-
client_secret: process.env.CLIENT_SECRET,
57+
client_id: config.CLIENT_ID,
58+
client_secret: config.CLIENT_SECRET,
6059
refresh_token: req.body.refresh_token,
61-
redirect_uri:process.env.RERUM_PREFIX
60+
redirect_uri: config.RERUM_PREFIX
6261
}
6362
try{
6463
// Successful responses from auth 0 look like {"refresh_token":"BLAHBLAH", "access_token":"BLAHBLAH"}
@@ -101,10 +100,10 @@ const generateNewRefreshToken = async (req, res, next) => {
101100
console.log("RERUM v1 is generating a new refresh token.")
102101
const form = {
103102
grant_type: 'authorization_code',
104-
client_id: process.env.CLIENT_ID,
105-
client_secret: process.env.CLIENT_SECRET,
103+
client_id: config.CLIENT_ID,
104+
client_secret: config.CLIENT_SECRET,
106105
code: req.body.authorization_code,
107-
redirect_uri:process.env.RERUM_PREFIX
106+
redirect_uri: config.RERUM_PREFIX
108107
}
109108
try {
110109
// Successful responses from auth 0 look like {"refresh_token":"BLAHBLAH", "access_token":"BLAHBLAH"}
@@ -160,7 +159,7 @@ const verifyAccess = (secret) => {
160159
* @returns Boolean match between encoded Generator Agent and obj generator
161160
*/
162161
const isGenerator = (obj, userObj) => {
163-
return userObj[process.env.RERUM_AGENT_CLAIM] === obj.__rerum.generatedBy
162+
return userObj[config.RERUM_AGENT_CLAIM] === obj.__rerum.generatedBy
164163
}
165164

166165
/**
@@ -170,11 +169,11 @@ const isGenerator = (obj, userObj) => {
170169
* @returns Boolean for matching ID.
171170
*/
172171
const isBot = (userObj) => {
173-
return process.env.BOT_AGENT === userObj[process.env.RERUM_AGENT_CLAIM]
172+
return config.BOT_AGENT === userObj[config.RERUM_AGENT_CLAIM]
174173
}
175174

176175
function READONLY(req, res, next) {
177-
if(process.env.READONLY=="true"){
176+
if(config.READONLY=="true"){
178177
res.status(503).json({"message":"RERUM v1 is read only at this time. We apologize for the inconvenience. Try again later."})
179178
return
180179
}

bin/rerum_v1.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ import app from '../app.js'
88
import debug from 'debug'
99
debug('rerum_server_nodejs:server')
1010
import http from "http"
11-
import dotenv from "dotenv"
12-
dotenv.config()
11+
import config from '../config/index.js'
1312

1413
/**
1514
* Get port from environment and store in Express.
1615
*/
1716

18-
const port = process.env.PORT ?? 3001
17+
const port = config.PORT ?? 3001
1918
app.set('port', port)
2019

2120
/**

config/index.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Centralized environment configuration for the RERUM API.
3+
* Loads variables from .env via dotenv and provides typed defaults.
4+
* All modules should import config from this file instead of
5+
* reading process.env directly.
6+
*
7+
* @module config
8+
* @author joeljoby02
9+
*/
10+
import dotenv from 'dotenv'
11+
dotenv.config()
12+
13+
const config = {
14+
MONGO_CONNECTION_STRING: process.env.MONGO_CONNECTION_STRING ?? 'mongodb://localhost:27017',
15+
MONGODBNAME: process.env.MONGODBNAME ?? 'rerum',
16+
MONGODBCOLLECTION: process.env.MONGODBCOLLECTION ?? 'objects',
17+
DOWN: process.env.DOWN ?? 'false',
18+
READONLY: process.env.READONLY ?? 'false',
19+
CLIENT_ID: process.env.CLIENT_ID ?? process.env.CLIENTID ?? '',
20+
CLIENT_SECRET: process.env.CLIENT_SECRET ?? process.env.RERUMSECRET ?? '',
21+
RERUM_PREFIX: process.env.RERUM_PREFIX ?? 'http://localhost:3001/v1/',
22+
RERUM_ID_PREFIX: process.env.RERUM_ID_PREFIX ?? 'http://localhost:3001/v1/id/',
23+
RERUM_AGENT_CLAIM: process.env.RERUM_AGENT_CLAIM ?? 'http://localhost:3001/agent',
24+
RERUM_CONTEXT: process.env.RERUM_CONTEXT ?? 'http://localhost:3001/v1/context.json',
25+
RERUM_API_VERSION: process.env.RERUM_API_VERSION ?? '1.0.0',
26+
BOT_AGENT: process.env.BOT_AGENT ?? '',
27+
AUDIENCE: process.env.AUDIENCE ?? '',
28+
ISSUER_BASE_URL: process.env.ISSUER_BASE_URL ?? '',
29+
BOT_TOKEN: process.env.BOT_TOKEN ?? '',
30+
PORT: parseInt(process.env.PORT ?? process.env.PORT_NUMBER ?? 3001, 10)
31+
}
32+
33+
export default config

controllers/bulk.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
* @author Claude Sonnet 4, cubap, thehabes
77
*/
88

9-
import { newID, isValidID, db } from '../database/index.js'
9+
import { newID, isValidID, db } from '../database/client.js'
1010
import utils from '../utils.js'
11+
import config from '../config/index.js'
1112
import { _contextid, ObjectID, createExpressError, getAgentClaim, parseDocumentID, idNegotiation } from './utils.js'
1213

1314
/**
@@ -75,13 +76,13 @@ const bulkCreate = async function (req, res, next) {
7576
// id is also protected in this case, so it can't be set.
7677
if(_contextid(d["@context"])) delete d.id
7778
d._id = id
78-
d['@id'] = `${process.env.RERUM_ID_PREFIX}${id}`
79+
d['@id'] = `${config.RERUM_ID_PREFIX}${id}`
7980
bulkOps.push({ insertOne : { "document" : d }})
8081
}
8182
try {
8283
let dbResponse = await db.bulkWrite(bulkOps, {'ordered':false})
8384
res.set("Content-Type", "application/json; charset=utf-8")
84-
res.set("Link",dbResponse.result.insertedIds.map(r => `${process.env.RERUM_ID_PREFIX}${r._id}`)) // https://www.rfc-editor.org/rfc/rfc5988
85+
res.set("Link",dbResponse.result.insertedIds.map(r => `${config.RERUM_ID_PREFIX}${r._id}`)) // https://www.rfc-editor.org/rfc/rfc5988
8586
res.status(201)
8687
const estimatedResults = bulkOps.map(f=>{
8788
let doc = f.insertOne.document
@@ -148,7 +149,7 @@ const bulkUpdate = async function (req, res, next) {
148149
// Update the same thing twice? can vs should.
149150
// if(encountered.includes(idReceived)) continue
150151
encountered.push(idReceived)
151-
if(!idReceived.includes(process.env.RERUM_ID_PREFIX)) continue
152+
if(!idReceived.includes(config.RERUM_ID_PREFIX)) continue
152153
let id = parseDocumentID(idReceived)
153154
let originalObject
154155
try {
@@ -168,7 +169,7 @@ const bulkUpdate = async function (req, res, next) {
168169
// id is also protected in this case, so it can't be set.
169170
if(_contextid(objectReceived["@context"])) delete objectReceived.id
170171
delete objectReceived["@context"]
171-
let newObject = Object.assign(context, { "@id": process.env.RERUM_ID_PREFIX + id }, objectReceived, rerumProp, { "_id": id })
172+
let newObject = Object.assign(context, { "@id": config.RERUM_ID_PREFIX + id }, objectReceived, rerumProp, { "_id": id })
172173
bulkOps.push({ insertOne : { "document" : newObject }})
173174
if(originalObject.__rerum.history.next.indexOf(newObject["@id"]) === -1){
174175
originalObject.__rerum.history.next.push(newObject["@id"])
@@ -185,7 +186,7 @@ const bulkUpdate = async function (req, res, next) {
185186
try {
186187
let dbResponse = await db.bulkWrite(bulkOps, {'ordered':false})
187188
res.set("Content-Type", "application/json; charset=utf-8")
188-
res.set("Link", dbResponse.result.insertedIds.map(r => `${process.env.RERUM_ID_PREFIX}${r._id}`)) // https://www.rfc-editor.org/rfc/rfc5988
189+
res.set("Link", dbResponse.result.insertedIds.map(r => `${config.RERUM_ID_PREFIX}${r._id}`)) // https://www.rfc-editor.org/rfc/rfc5988
189190
res.status(200)
190191
const estimatedResults = bulkOps.filter(f=>f.insertOne).map(f=>{
191192
let doc = f.insertOne.document

controllers/crud.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
* Basic CRUD operations for RERUM v1
55
* @author Claude Sonnet 4, cubap, thehabes
66
*/
7-
import { newID, isValidID, db } from '../database/index.js'
7+
import { newID, isValidID, db } from '../database/client.js'
88
import utils from '../utils.js'
9+
import config from '../config/index.js'
910
import { _contextid, idNegotiation, generateSlugId, ObjectID, createExpressError, getAgentClaim, parseDocumentID } from './utils.js'
1011

1112
/**
@@ -42,7 +43,7 @@ const create = async function (req, res, next) {
4243
if(_contextid(provided["@context"])) delete provided.id
4344
delete provided["@context"]
4445

45-
let newObject = Object.assign(context, { "@id": process.env.RERUM_ID_PREFIX + id }, provided, rerumProp, { "_id": id })
46+
let newObject = Object.assign(context, { "@id": config.RERUM_ID_PREFIX + id }, provided, rerumProp, { "_id": id })
4647
console.log("CREATE")
4748
try {
4849
let result = await db.insertOne(newObject)

controllers/delete.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
* Delete operations for RERUM v1
55
* @author Claude Sonnet 4, cubap, thehabes
66
*/
7-
import { newID, isValidID, db } from '../database/index.js'
7+
import { newID, isValidID, db } from '../database/client.js'
88
import utils from '../utils.js'
9+
import config from '../config/index.js'
910
import { createExpressError, getAgentClaim, parseDocumentID, getAllVersions, getAllDescendants } from './utils.js'
1011

1112
/**
@@ -159,7 +160,7 @@ async function healHistoryTree(obj) {
159160
throw Error("Could not update all descendants with their new prime value")
160161
}
161162
}
162-
if (previous_id.indexOf(process.env.RERUM_PREFIX) > -1) {
163+
if (previous_id.indexOf(config.RERUM_PREFIX) > -1) {
163164
let previousIdForQuery = parseDocumentID(previous_id)
164165
const objToUpdate2 = await db.findOne({"$or":[{"_id": previousIdForQuery}, {"__rerum.slug": previousIdForQuery}]})
165166
if (null !== objToUpdate2) {

controllers/gog.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @author Claude Sonnet 4, cubap, thehabes
77
*/
88

9-
import { newID, isValidID, db } from '../database/index.js'
9+
import { newID, isValidID, db } from '../database/client.js'
1010
import utils from '../utils.js'
1111
import { _contextid, ObjectID, createExpressError, getAgentClaim, parseDocumentID, idNegotiation } from './utils.js'
1212

controllers/history.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @author Claude Sonnet 4, cubap, thehabes
77
*/
88

9-
import { newID, isValidID, db } from '../database/index.js'
9+
import { newID, isValidID, db } from '../database/client.js'
1010
import utils from '../utils.js'
1111
import { _contextid, ObjectID, createExpressError, getAgentClaim, parseDocumentID, idNegotiation, getAllVersions, getAllAncestors, getAllDescendants } from './utils.js'
1212

controllers/overwrite.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @author Claude Sonnet 4, cubap, thehabes
77
*/
88

9-
import { newID, isValidID, db } from '../database/index.js'
9+
import { newID, isValidID, db } from '../database/client.js'
1010
import utils from '../utils.js'
1111
import { _contextid, ObjectID, createExpressError, getAgentClaim, parseDocumentID, idNegotiation } from './utils.js'
1212

0 commit comments

Comments
 (0)