Skip to content

Commit

Permalink
Revert "removed bedrock/config in favor of node --env-file (#251)"
Browse files Browse the repository at this point in the history
This reverts commit 6462f5b.
  • Loading branch information
andrewplummer committed Dec 10, 2024
1 parent 6462f5b commit a110966
Show file tree
Hide file tree
Showing 25 changed files with 100 additions and 58 deletions.
2 changes: 1 addition & 1 deletion services/api/.env
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ SERVER_PORT=2300

# Main DB Config
MONGO_URI=mongodb://localhost/bedrock_dev
MONGO_DEBUG=
MONGO_DEBUG=false

# Default admin account for dashboard login
ADMIN_NAME=Marlon Brando
Expand Down
5 changes: 2 additions & 3 deletions services/api/.env.production
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
# This file is for local use only. Do NOT put secrets in here.
MONGO_URI=mongodb://localhost/bedrock_production
UPLOADS_GCS_BUCKET=bedrock-production-uploads
export MONGO_URI=mongodb://localhost/bedrock_production
export UPLOADS_GCS_BUCKET=bedrock-production-uploads
5 changes: 2 additions & 3 deletions services/api/.env.staging
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
# This file is for local use only. Do NOT put secrets in here.
MONGO_URI=mongodb://localhost/bedrock_staging
UPLOADS_GCS_BUCKET=bedrock-staging-uploads
export MONGO_URI=mongodb://localhost/bedrock_staging
export UPLOADS_GCS_BUCKET=bedrock-staging-uploads
3 changes: 2 additions & 1 deletion services/api/__mocks__/twilio.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const crypto = require('crypto');
const config = require('@bedrockio/config');
const twilio = jest.requireActual('twilio');

const { AUTH_TOKEN } = process.env;
const AUTH_TOKEN = config.get('TWILIO_AUTH_TOKEN');

let sentMessages;
let createdRooms;
Expand Down
15 changes: 8 additions & 7 deletions services/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@
"node": ">=20"
},
"scripts": {
"start": "node --env-file=.env --watch src/index.js",
"start": "node --watch src/index.js",
"debug": "MONGO_DEBUG=true yarn start",
"staging": "node --env-file=.env --env-file=.env.staging --watch src/index.js",
"production": "node --env-file=.env --env-file=.env.production --watch src/index.js",
"start:production": "node --env-file=.env src/index",
"staging": "source .env.staging && node --watch src/index.js",
"production": "source .env.production && node --watch src/index.js",
"start:production": "node src/index",
"lint": "eslint",
"test": "node --env-file=.env ./node_modules/.bin/jest",
"test:watch": "node --env-file=.env ./node_modules/.bin/jest --watch -i",
"test": "jest",
"test:watch": "jest --watch -i",
"fixtures:load": "./scripts/fixtures/load",
"fixtures:reload": "./scripts/fixtures/reload",
"fixtures:export": "./scripts/fixtures/export",
"docs:generate": "./scripts/docs/generate"
},
"dependencies": {
"@bedrockio/fixtures": "^1.3.0",
"@bedrockio/config": "^2.2.3",
"@bedrockio/fixtures": "^1.2.5",
"@bedrockio/logger": "^1.0.8",
"@bedrockio/model": "^0.7.3",
"@bedrockio/yada": "^1.2.3",
Expand Down
3 changes: 2 additions & 1 deletion services/api/scripts/fixtures/reload
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node

const config = require('@bedrockio/config');
const logger = require('@bedrockio/logger');
const { initialize } = require('../../src/utils/database');
const { loadFixtures } = require('../../src/utils/fixtures');
Expand All @@ -8,7 +9,7 @@ const readline = require('readline');
// Ensure models are loaded.
require('../../src/models');

const { ENV_NAME } = process.env;
const ENV_NAME = config.get('ENV_NAME');

const rl = readline.createInterface({
input: process.stdin,
Expand Down
9 changes: 5 additions & 4 deletions services/api/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ const serializeMiddleware = require('./utils/middleware/serialize');
const organizationMiddleware = require('./utils/middleware/organization');
const { applicationMiddleware } = require('./utils/middleware/application');
const { loadDefinition } = require('./utils/openapi');
const logger = require('@bedrockio/logger');
const Sentry = require('@sentry/node');
const routes = require('./routes');
const config = require('@bedrockio/config');
const logger = require('@bedrockio/logger');

const { ENV_NAME, SENTRY_DSN } = process.env;
const ENV_NAME = config.get('ENV_NAME');

const app = new Koa();

Expand Down Expand Up @@ -73,9 +74,9 @@ app.on('error', (err, ctx) => {
}
});

if (SENTRY_DSN) {
if (config.has('SENTRY_DSN')) {
Sentry.init({
dsn: SENTRY_DSN,
dsn: config.get('SENTRY_DSN'),
environment: ENV_NAME,
});
}
Expand Down
15 changes: 11 additions & 4 deletions services/api/src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
const logger = require('@bedrockio/logger');
const config = require('@bedrockio/config');

const { initialize } = require('./utils/database');
const { loadFixtures } = require('./utils/fixtures');
const app = require('./app');

const { ENV_NAME, SERVER_PORT, SERVER_HOST, APP_NAME, ADMIN_EMAIL, ADMIN_PASSWORD } = process.env;
const ENV_NAME = config.get('ENV_NAME');
const PORT = config.get('SERVER_PORT', 'number');
const HOST = config.get('SERVER_HOST');

if (process.env.NODE_ENV === 'production') {
logger.setupGoogleCloud({
Expand All @@ -21,11 +24,15 @@ module.exports = (async () => {
if (ENV_NAME === 'development') {
await loadFixtures();
}
app.listen(SERVER_PORT, SERVER_HOST, () => {
logger.info(`Started on port //${SERVER_HOST}:${SERVER_PORT}`);
app.listen(PORT, HOST, () => {
logger.info(`Started on port //${HOST}:${PORT}`);
if (ENV_NAME === 'development') {
logger.info('-----------------------------------------------------------------');
logger.info(`${APP_NAME} Admin Login ${ADMIN_EMAIL}:${ADMIN_PASSWORD} (dev env only)`);
logger.info(
`${config.get('APP_NAME')} Admin Login ${config.get('ADMIN_EMAIL')}:${config.get(
'ADMIN_PASSWORD'
)} (dev env only)`
);
logger.info('-----------------------------------------------------------------');
}
});
Expand Down
3 changes: 2 additions & 1 deletion services/api/src/routes/auth/apple/utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const verifyAppleToken = require('verify-apple-id-token').default;
const config = require('@bedrockio/config');

const { clearAuthenticators } = require('../../../utils/auth/authenticators');

const { APPLE_SERVICE_ID } = process.env;
const APPLE_SERVICE_ID = config.get('APPLE_SERVICE_ID');

async function verifyToken(token) {
const payload = await verifyAppleToken({
Expand Down
3 changes: 2 additions & 1 deletion services/api/src/routes/auth/google/utils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const { OAuth2Client } = require('google-auth-library');
const config = require('@bedrockio/config');
const { clearAuthenticators } = require('../../../utils/auth/authenticators');

const client = new OAuth2Client();

const { GOOGLE_CLIENT_ID } = process.env;
const GOOGLE_CLIENT_ID = config.get('GOOGLE_CLIENT_ID');

async function verifyToken(token) {
const ticket = await client.verifyIdToken({
Expand Down
7 changes: 5 additions & 2 deletions services/api/src/routes/auth/passkey/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const SimpleWebAuthn = require('@simplewebauthn/server');
const config = require('@bedrockio/config');

const { clearAuthenticators, getRequiredAuthenticator } = require('../../../utils/auth/authenticators');

const { APP_NAME, APP_URL } = process.env;
const APP_NAME = config.get('APP_NAME');
const APP_URL = config.get('APP_URL');

// Human-readable app name.
const rpName = APP_NAME;
Expand All @@ -11,7 +14,7 @@ const rpName = APP_NAME;
const rpID = new URL(APP_URL).hostname;

// The URL at which registrations and authentications should occur
const origin = APP_URL;
const origin = config.get('APP_URL');

async function generateRegistrationOptions(user) {
// Only allow a single passkey at a time.
Expand Down
3 changes: 2 additions & 1 deletion services/api/src/routes/auth/totp/utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const speakeasy = require('speakeasy');
const config = require('@bedrockio/config');

const { clearAuthenticators, getRequiredAuthenticator } = require('../../../utils/auth/authenticators');

const { APP_NAME } = process.env;
const APP_NAME = config.get('APP_NAME');

function generateTotp() {
const secret = createSecret();
Expand Down
3 changes: 2 additions & 1 deletion services/api/src/utils/auth/tokens.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const jwt = require('jsonwebtoken');
const config = require('@bedrockio/config');
const { nanoid } = require('nanoid');

const { JWT_SECRET } = process.env;
const JWT_SECRET = config.get('JWT_SECRET');

// All expires are expressed in seconds (jwt spec)
const expiresIn = {
Expand Down
3 changes: 2 additions & 1 deletion services/api/src/utils/csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ const { PassThrough } = require('stream');

const csv = require('fast-csv');
const yd = require('@bedrockio/yada');
const config = require('@bedrockio/config');
const mongoose = require('mongoose');
const { get, startCase } = require('lodash');

const { serializeObject } = require('./serialize');

const formatter = Intl.NumberFormat('us');

const { API_URL } = process.env;
const API_URL = config.get('API_URL');

const DEFAULT_OPTIONS = {
readableHeaders: true,
Expand Down
7 changes: 3 additions & 4 deletions services/api/src/utils/database.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const mongoose = require('mongoose');
const config = require('@bedrockio/config');
const logger = require('@bedrockio/logger');

const { MONGO_URI, MONGO_DEBUG } = process.env;

mongoose.Promise = Promise;

// https://mongoosejs.com/docs/migrating_to_6.html#no-more-deprecation-warning-options
Expand All @@ -26,9 +25,9 @@ exports.flags = flags;

exports.initialize = async function initialize() {
mongoose.set('strictQuery', false);
await mongoose.connect(MONGO_URI, flags);
await mongoose.connect(config.get('MONGO_URI'), flags);

if (MONGO_DEBUG) {
if (config.get('MONGO_DEBUG', 'boolean')) {
mongoose.set('debug', true);
}

Expand Down
8 changes: 7 additions & 1 deletion services/api/src/utils/messaging/mailer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ const htmlToText = require('html-to-text');

const { loadTemplate, interpolate, escapeHtml } = require('./utils');

const config = require('@bedrockio/config');
const logger = require('@bedrockio/logger');

const { ENV_NAME, APP_NAME, POSTMARK_API_KEY, POSTMARK_FROM, POSTMARK_DEV_EMAIL, POSTMARK_WEBHOOK_KEY } = process.env;
const ENV_NAME = config.get('ENV_NAME');
const APP_NAME = config.get('APP_NAME');
const POSTMARK_FROM = config.get('POSTMARK_FROM');
const POSTMARK_API_KEY = config.get('POSTMARK_API_KEY');
const POSTMARK_DEV_EMAIL = config.get('POSTMARK_DEV_EMAIL');
const POSTMARK_WEBHOOK_KEY = config.get('POSTMARK_WEBHOOK_KEY');

const TEMPLATE_DIR = path.join(__dirname, '../../emails');

Expand Down
10 changes: 9 additions & 1 deletion services/api/src/utils/messaging/sms.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
const path = require('path');
const twilio = require('twilio');
const config = require('@bedrockio/config');
const logger = require('@bedrockio/logger');

const { interpolate, loadTemplate } = require('./utils');

const { API_URL, ENV_NAME, AUTH_TOKEN, ACCOUNT_SID, TEST_NUMBER, FROM_NUMBER, WEBHOOK_URL } = process.env;
const API_URL = config.get('API_URL');
const ENV_NAME = config.get('ENV_NAME');

const AUTH_TOKEN = config.get('TWILIO_AUTH_TOKEN');
const ACCOUNT_SID = config.get('TWILIO_ACCOUNT_SID');
const TEST_NUMBER = config.get('TWILIO_TEST_NUMBER');
const FROM_NUMBER = config.get('TWILIO_FROM_NUMBER');
const WEBHOOK_URL = config.get('TWILIO_WEBHOOK_URL');

const TEMPLATE_DIR = path.join(__dirname, '../../sms');

Expand Down
3 changes: 2 additions & 1 deletion services/api/src/utils/messaging/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ const fs = require('fs/promises');
const path = require('path');
const Mustache = require('mustache');
const frontmatter = require('front-matter');
const config = require('@bedrockio/config');
const { memoize } = require('lodash');

// Environment vars

const ENV = process.env;
const ENV = config.getAll();

// Mustache utils

Expand Down
12 changes: 6 additions & 6 deletions services/api/src/utils/middleware/__tests__/tokens.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const jwt = require('jsonwebtoken');
const config = require('@bedrockio/config');

const { JWT_SECRET } = process.env;
const { validateToken } = require('../tokens');

const { context } = require('../../testing');
Expand Down Expand Up @@ -53,14 +53,14 @@ describe('validateToken', () => {

it('should fail if expired', async () => {
const middleware = validateToken();
const token = jwt.sign({ kid: 'user' }, JWT_SECRET, { expiresIn: 0 });
const token = jwt.sign({ kid: 'user' }, config.get('JWT_SECRET'), { expiresIn: 0 });
const ctx = context({ headers: { authorization: `Bearer ${token}` } });
await expect(middleware(ctx)).rejects.toHaveProperty('message', 'jwt expired');
});

it('should work with valid secret and not expired', async () => {
const middleware = validateToken();
const token = jwt.sign({ kid: 'user', attribute: 'value' }, JWT_SECRET);
const token = jwt.sign({ kid: 'user', attribute: 'value' }, config.get('JWT_SECRET'));
const ctx = context({ headers: { authorization: `Bearer ${token}` } });
await middleware(ctx, () => {
expect(ctx.state.jwt.attribute).toBe('value');
Expand All @@ -69,7 +69,7 @@ describe('validateToken', () => {

it('should only validate the token once when called multiple times', async () => {
const middleware = validateToken();
const token = jwt.sign({ kid: 'user', attribute: 'value' }, JWT_SECRET);
const token = jwt.sign({ kid: 'user', attribute: 'value' }, config.get('JWT_SECRET'));
const ctx = context({ headers: { authorization: `Bearer ${token}` } });

let tmp;
Expand All @@ -92,7 +92,7 @@ describe('validateToken', () => {
describe('optional validation', () => {
it('should validateToken when token exists', async () => {
const middleware = validateToken({ optional: true });
const token = jwt.sign({ kid: 'user', attribute: 'value' }, JWT_SECRET);
const token = jwt.sign({ kid: 'user', attribute: 'value' }, config.get('JWT_SECRET'));
const ctx = context({ headers: { authorization: `Bearer ${token}` } });
await middleware(ctx, () => {
expect(ctx.state.jwt.attribute).toBe('value');
Expand All @@ -111,7 +111,7 @@ describe('validateToken', () => {
const optional = validateToken({ optional: true });
const required = validateToken();

const token = jwt.sign({ kid: 'user', attribute: 'value' }, JWT_SECRET);
const token = jwt.sign({ kid: 'user', attribute: 'value' }, config.get('JWT_SECRET'));
const ctx = context({ headers: { authorization: `Bearer ${token}` } });

await optional(ctx, () => {});
Expand Down
3 changes: 2 additions & 1 deletion services/api/src/utils/middleware/authenticate.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const { validateToken } = require('./tokens');
const { User } = require('../../models');
const compose = require('koa-compose');
const config = require('@bedrockio/config');

const { ENV_NAME } = process.env;
const ENV_NAME = config.get('ENV_NAME');

function authenticate(options = {}) {
const { optional } = options;
Expand Down
4 changes: 2 additions & 2 deletions services/api/src/utils/middleware/error-handler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const config = require('@bedrockio/config');
const { isSchemaError } = require('@bedrockio/yada');

const { ENV_NAME } = process.env;
const ENV_NAME = config.get('ENV_NAME');

async function errorHandler(ctx, next) {
try {
Expand Down
3 changes: 2 additions & 1 deletion services/api/src/utils/middleware/organization.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { Organization } = require('../../models');
const config = require('@bedrockio/config');

const { DEFAULT_ORGANIZATION_NAME } = process.env;
const DEFAULT_ORGANIZATION_NAME = config.get('DEFAULT_ORGANIZATION_NAME');

async function organization(ctx, next) {
const identifier = ctx.request.get('organization') || '';
Expand Down
Loading

0 comments on commit a110966

Please sign in to comment.