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 #4133

Merged
merged 4 commits into from
Jan 28, 2021
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
155 changes: 75 additions & 80 deletions backend/src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,111 +2,106 @@ import dotenv from 'dotenv'
import links from './links.js'
import metadata from './metadata.js'

// Load env file
if (require.resolve) {
// are we in a nodejs environment?
try {
dotenv.config({ path: require.resolve('../../.env') })
} catch (error) {
if (error.code !== 'MODULE_NOT_FOUND') throw error
console.log('WARN: No `.env` file found in `/app` (docker) or `/backend` (no docker)') // eslint-disable-line no-console
if (error.code === 'MODULE_NOT_FOUND') {
console.log('WARN: No `.env` file found in `/app` (docker) or `/backend` (no docker)') // eslint-disable-line no-console
} else {
throw error
}
}
}

// eslint-disable-next-line no-undef
const env = typeof Cypress !== 'undefined' ? Cypress.env() : process.env

const {
MAPBOX_TOKEN,
JWT_SECRET,
PRIVATE_KEY_PASSPHRASE,
SMTP_IGNORE_TLS = true,
SMTP_HOST,
SMTP_PORT,
SMTP_USERNAME,
SMTP_PASSWORD,
SENTRY_DSN_BACKEND,
COMMIT,
AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY,
AWS_ENDPOINT,
AWS_REGION,
AWS_BUCKET,
NEO4J_URI = 'bolt://localhost:7687',
NEO4J_USERNAME = 'neo4j',
NEO4J_PASSWORD = 'neo4j',
CLIENT_URI = 'http://localhost:3000',
GRAPHQL_URI = 'http://localhost:4000',
REDIS_DOMAIN,
REDIS_PORT,
REDIS_PASSWORD,
EMAIL_DEFAULT_SENDER,
} = env
// Use Cypress env or process.env
const env = typeof Cypress !== 'undefined' ? Cypress.env() : process.env // eslint-disable-line no-undef

export const requiredConfigs = {
MAPBOX_TOKEN,
JWT_SECRET,
PRIVATE_KEY_PASSPHRASE,
const environment = {
NODE_ENV: env.NODE_ENV || process.NODE_ENV,
DEBUG: env.NODE_ENV !== 'production' && env.DEBUG,
TEST: env.NODE_ENV === 'test',
PRODUCTION: env.NODE_ENV === 'production',
DISABLED_MIDDLEWARES: (env.NODE_ENV !== 'production' && env.DISABLED_MIDDLEWARES) || false,
}

if (require.resolve) {
// are we in a nodejs environment?
Object.entries(requiredConfigs).map((entry) => {
if (!entry[1]) {
throw new Error(`ERROR: "${entry[0]}" env variable is missing.`)
}
})
const required = {
MAPBOX_TOKEN: env.MAPBOX_TOKEN,
JWT_SECRET: env.JWT_SECRET,
PRIVATE_KEY_PASSPHRASE: env.PRIVATE_KEY_PASSPHRASE,
}

export const smtpConfigs = {
SMTP_HOST,
SMTP_PORT,
SMTP_IGNORE_TLS,
SMTP_USERNAME,
SMTP_PASSWORD,
const server = {
CLIENT_URI: env.CLIENT_URI || 'http://localhost:3000',
GRAPHQL_URI: env.GRAPHQL_URI || 'http://localhost:4000',
}
export const neo4jConfigs = { NEO4J_URI, NEO4J_USERNAME, NEO4J_PASSWORD }
export const serverConfigs = {
CLIENT_URI,
GRAPHQL_URI,
PUBLIC_REGISTRATION: process.env.PUBLIC_REGISTRATION === 'true',

const smtp = {
SMTP_HOST: env.SMTP_HOST,
SMTP_PORT: env.SMTP_PORT,
SMTP_IGNORE_TLS: env.SMTP_IGNORE_TLS || true,
SMTP_USERNAME: env.SMTP_USERNAME,
SMTP_PASSWORD: env.SMTP_PASSWORD,
}

export const developmentConfigs = {
DEBUG: process.env.NODE_ENV !== 'production' && process.env.DEBUG,
DISABLED_MIDDLEWARES:
(process.env.NODE_ENV !== 'production' && process.env.DISABLED_MIDDLEWARES) || '',
const neo4j = {
NEO4J_URI: env.NEO4J_URI || 'bolt://localhost:7687',
NEO4J_USERNAME: env.NEO4J_USERNAME || 'neo4j',
NEO4J_PASSWORD: env.NEO4J_PASSWORD || 'neo4j',
}

export const sentryConfigs = { SENTRY_DSN_BACKEND, COMMIT }
export const redisConfigs = { REDIS_DOMAIN, REDIS_PORT, REDIS_PASSWORD }
const sentry = {
SENTRY_DSN_BACKEND: env.SENTRY_DSN_BACKEND,
COMMIT: env.COMMIT,
}

const S3_CONFIGURED =
AWS_ACCESS_KEY_ID && AWS_SECRET_ACCESS_KEY && AWS_ENDPOINT && AWS_REGION && AWS_BUCKET
const redis = {
REDIS_DOMAIN: env.REDIS_DOMAIN,
REDIS_PORT: env.REDIS_PORT,
REDIS_PASSWORD: env.REDIS_PASSWORD,
}

export const s3Configs = {
AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY,
AWS_ENDPOINT,
AWS_REGION,
AWS_BUCKET,
S3_CONFIGURED,
const s3 = {
AWS_ACCESS_KEY_ID: env.AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY: env.AWS_SECRET_ACCESS_KEY,
AWS_ENDPOINT: env.AWS_ENDPOINT,
AWS_REGION: env.AWS_REGION,
AWS_BUCKET: env.AWS_BUCKET,
S3_CONFIGURED:
env.AWS_ACCESS_KEY_ID &&
env.AWS_SECRET_ACCESS_KEY &&
env.AWS_ENDPOINT &&
env.AWS_REGION &&
env.AWS_BUCKET,
}

export const customConfigs = {
EMAIL_DEFAULT_SENDER,
const options = {
EMAIL_DEFAULT_SENDER: env.EMAIL_DEFAULT_SENDER,
SUPPORT_URL: links.SUPPORT,
APPLICATION_NAME: metadata.APPLICATION_NAME,
ORGANIZATION_URL: links.ORGANIZATION,
PUBLIC_REGISTRATION: env.PUBLIC_REGISTRATION === 'true',
}

// Check if all required configs are present
if (require.resolve) {
// are we in a nodejs environment?
Object.entries(required).map((entry) => {
if (!entry[1]) {
throw new Error(`ERROR: "${entry[0]}" env variable is missing.`)
}
})
}

export default {
...requiredConfigs,
...smtpConfigs,
...neo4jConfigs,
...serverConfigs,
...developmentConfigs,
...sentryConfigs,
...redisConfigs,
...s3Configs,
...customConfigs,
...environment,
...server,
...required,
...smtp,
...neo4j,
...sentry,
...redis,
...s3,
...options,
}
3 changes: 2 additions & 1 deletion backend/src/db/clean.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { cleanDatabase } from '../db/factories'
import CONFIG from '../config'

if (process.env.NODE_ENV === 'production') {
if (CONFIG.PRODUCTION) {
throw new Error(`You cannot clean the database in production environment!`)
}

Expand Down
2 changes: 1 addition & 1 deletion backend/src/middleware/email/emailMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const hasAuthData = CONFIG.SMTP_USERNAME && CONFIG.SMTP_PASSWORD

let sendMail = () => {}
if (!hasEmailConfig) {
if (process.env.NODE_ENV !== 'test') {
if (!CONFIG.TEST) {
// eslint-disable-next-line no-console
console.log('Warning: Email middleware will not try to send mails.')
}
Expand Down
12 changes: 6 additions & 6 deletions backend/src/middleware/sentryMiddleware.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { sentry } from 'graphql-middleware-sentry'
import { sentryConfigs } from '../config'
import CONFIG from '../config'

let sentryMiddleware = (resolve, root, args, context, resolveInfo) =>
resolve(root, args, context, resolveInfo)

if (sentryConfigs.SENTRY_DSN_BACKEND) {
if (CONFIG.SENTRY_DSN_BACKEND) {
sentryMiddleware = sentry({
forwardErrors: true,
config: {
dsn: sentryConfigs.SENTRY_DSN_BACKEND,
release: sentryConfigs.COMMIT,
environment: process.env.NODE_ENV,
dsn: CONFIG.SENTRY_DSN_BACKEND,
release: CONFIG.COMMIT,
environment: CONFIG.NODE_ENV,
},
withScope: (scope, error, context) => {
scope.setUser({
Expand All @@ -23,7 +23,7 @@ if (sentryConfigs.SENTRY_DSN_BACKEND) {
})
} else {
// eslint-disable-next-line no-console
if (process.env.NODE_ENV !== 'test') console.log('Warning: Sentry middleware inactive.')
if (!CONFIG.TEST) console.log('Warning: Sentry middleware inactive.')
}

export default sentryMiddleware
4 changes: 2 additions & 2 deletions backend/src/schema/resolvers/images/images.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import slug from 'slug'
import { existsSync, unlinkSync, createWriteStream } from 'fs'
import { UserInputError } from 'apollo-server'
import { getDriver } from '../../../db/neo4j'
import { s3Configs } from '../../../config'
import CONFIG from '../../../config'

// const widths = [34, 160, 320, 640, 1024]
const { AWS_ENDPOINT: endpoint, AWS_REGION: region, AWS_BUCKET: Bucket, S3_CONFIGURED } = s3Configs
const { AWS_ENDPOINT: endpoint, AWS_REGION: region, AWS_BUCKET: Bucket, S3_CONFIGURED } = CONFIG

export async function deleteImage(resource, relationshipType, opts = {}) {
sanitizeRelationshipType(relationshipType)
Expand Down
1 change: 1 addition & 0 deletions webapp/Dockerfile.maintenance
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ COPY plugins/i18n.js plugins/v-tooltip.js plugins/styleguide.js plugins/
COPY static static
COPY constants constants
COPY nuxt.config.js nuxt.config.js
COPY config/ config/

# this will also ovewrite the existing package.json
COPY maintenance/source ./
Expand Down
46 changes: 46 additions & 0 deletions webapp/config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// ATTENTION: DO NOT PUT ANY SECRETS IN HERE (or the .env)

import dotenv from 'dotenv'
dotenv.config() // we want to synchronize @nuxt-dotenv and nuxt-env

// Load Package Details for some default values
const pkg = require('../package')

const environment = {
NODE_ENV: process.env.NODE_ENV,
DEBUG: process.env.NODE_ENV !== 'production' || false,
PRODUCTION: process.env.NODE_ENV === 'production' || false,
NUXT_BUILD: process.env.NUXT_BUILD || '.nuxt',
STYLEGUIDE_DEV: process.env.STYLEGUIDE_DEV || false,
RELEASE: process.env.release,
}

const server = {
GRAPHQL_URI: process.env.GRAPHQL_URI || 'http://localhost:4000',
BACKEND_TOKEN: process.env.BACKEND_TOKEN || 'NULL',
}

const sentry = {
SENTRY_DSN_WEBAPP: process.env.SENTRY_DSN_WEBAPP,
COMMIT: process.env.COMMIT,
}

const options = {
VERSION: process.env.VERSION || pkg.version,
DESCRIPTION: process.env.DESCRIPTION || pkg.description,
}

const CONFIG = {
...environment,
...server,
...sentry,
...options,
}

// override process.env with the values here since they contain default values
process.env = {
...process.env,
...CONFIG,
}

export default CONFIG
Loading