mirror of
https://github.com/IT4Change/Ocelot-Social.git
synced 2026-04-06 01:25:31 +00:00
refactor backend config
This commit is contained in:
parent
5ef781c49d
commit
13c8b85c78
@ -2,111 +2,106 @@ import dotenv from 'dotenv'
|
|||||||
import links from './links.js'
|
import links from './links.js'
|
||||||
import metadata from './metadata.js'
|
import metadata from './metadata.js'
|
||||||
|
|
||||||
|
// Load env file
|
||||||
if (require.resolve) {
|
if (require.resolve) {
|
||||||
// are we in a nodejs environment?
|
|
||||||
try {
|
try {
|
||||||
dotenv.config({ path: require.resolve('../../.env') })
|
dotenv.config({ path: require.resolve('../../.env') })
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error.code !== 'MODULE_NOT_FOUND') throw error
|
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
|
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
|
// Use Cypress env or process.env
|
||||||
const env = typeof Cypress !== 'undefined' ? Cypress.env() : process.env
|
const env = typeof Cypress !== 'undefined' ? Cypress.env() : process.env // eslint-disable-line no-undef
|
||||||
|
|
||||||
const {
|
const environment = {
|
||||||
MAPBOX_TOKEN,
|
NODE_ENV: env.NODE_ENV || process.NODE_ENV,
|
||||||
JWT_SECRET,
|
DEBUG: env.NODE_ENV !== 'production' && env.DEBUG,
|
||||||
PRIVATE_KEY_PASSPHRASE,
|
TEST: env.NODE_ENV === 'test',
|
||||||
SMTP_IGNORE_TLS = true,
|
PRODUCTION: env.NODE_ENV === 'production',
|
||||||
SMTP_HOST,
|
DISABLED_MIDDLEWARES: (env.NODE_ENV !== 'production' && env.DISABLED_MIDDLEWARES) || false,
|
||||||
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
|
|
||||||
|
|
||||||
export const requiredConfigs = {
|
|
||||||
MAPBOX_TOKEN,
|
|
||||||
JWT_SECRET,
|
|
||||||
PRIVATE_KEY_PASSPHRASE,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const required = {
|
||||||
|
MAPBOX_TOKEN: env.MAPBOX_TOKEN,
|
||||||
|
JWT_SECRET: env.JWT_SECRET,
|
||||||
|
PRIVATE_KEY_PASSPHRASE: env.PRIVATE_KEY_PASSPHRASE,
|
||||||
|
}
|
||||||
|
|
||||||
|
const server = {
|
||||||
|
CLIENT_URI: env.CLIENT_URI || 'http://localhost:3000',
|
||||||
|
GRAPHQL_URI: env.GRAPHQL_URI || 'http://localhost:4000',
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
|
||||||
|
const neo4j = {
|
||||||
|
NEO4J_URI: env.NEO4J_URI || 'bolt://localhost:7687',
|
||||||
|
NEO4J_USERNAME: env.NEO4J_USERNAME || 'neo4j',
|
||||||
|
NEO4J_PASSWORD: env.NEO4J_PASSWORD || 'neo4j',
|
||||||
|
}
|
||||||
|
|
||||||
|
const sentry = {
|
||||||
|
SENTRY_DSN_BACKEND: env.SENTRY_DSN_BACKEND,
|
||||||
|
COMMIT: env.COMMIT,
|
||||||
|
}
|
||||||
|
|
||||||
|
const redis = {
|
||||||
|
REDIS_DOMAIN: env.REDIS_DOMAIN,
|
||||||
|
REDIS_PORT: env.REDIS_PORT,
|
||||||
|
REDIS_PASSWORD: env.REDIS_PASSWORD,
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
if (require.resolve) {
|
||||||
// are we in a nodejs environment?
|
// are we in a nodejs environment?
|
||||||
Object.entries(requiredConfigs).map((entry) => {
|
Object.entries(required).map((entry) => {
|
||||||
if (!entry[1]) {
|
if (!entry[1]) {
|
||||||
throw new Error(`ERROR: "${entry[0]}" env variable is missing.`)
|
throw new Error(`ERROR: "${entry[0]}" env variable is missing.`)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export const smtpConfigs = {
|
|
||||||
SMTP_HOST,
|
|
||||||
SMTP_PORT,
|
|
||||||
SMTP_IGNORE_TLS,
|
|
||||||
SMTP_USERNAME,
|
|
||||||
SMTP_PASSWORD,
|
|
||||||
}
|
|
||||||
export const neo4jConfigs = { NEO4J_URI, NEO4J_USERNAME, NEO4J_PASSWORD }
|
|
||||||
export const serverConfigs = {
|
|
||||||
CLIENT_URI,
|
|
||||||
GRAPHQL_URI,
|
|
||||||
PUBLIC_REGISTRATION: process.env.PUBLIC_REGISTRATION === 'true',
|
|
||||||
}
|
|
||||||
|
|
||||||
export const developmentConfigs = {
|
|
||||||
DEBUG: process.env.NODE_ENV !== 'production' && process.env.DEBUG,
|
|
||||||
DISABLED_MIDDLEWARES:
|
|
||||||
(process.env.NODE_ENV !== 'production' && process.env.DISABLED_MIDDLEWARES) || '',
|
|
||||||
}
|
|
||||||
|
|
||||||
export const sentryConfigs = { SENTRY_DSN_BACKEND, COMMIT }
|
|
||||||
export const redisConfigs = { REDIS_DOMAIN, REDIS_PORT, REDIS_PASSWORD }
|
|
||||||
|
|
||||||
const S3_CONFIGURED =
|
|
||||||
AWS_ACCESS_KEY_ID && AWS_SECRET_ACCESS_KEY && AWS_ENDPOINT && AWS_REGION && AWS_BUCKET
|
|
||||||
|
|
||||||
export const s3Configs = {
|
|
||||||
AWS_ACCESS_KEY_ID,
|
|
||||||
AWS_SECRET_ACCESS_KEY,
|
|
||||||
AWS_ENDPOINT,
|
|
||||||
AWS_REGION,
|
|
||||||
AWS_BUCKET,
|
|
||||||
S3_CONFIGURED,
|
|
||||||
}
|
|
||||||
|
|
||||||
export const customConfigs = {
|
|
||||||
EMAIL_DEFAULT_SENDER,
|
|
||||||
SUPPORT_URL: links.SUPPORT,
|
|
||||||
APPLICATION_NAME: metadata.APPLICATION_NAME,
|
|
||||||
ORGANIZATION_URL: links.ORGANIZATION,
|
|
||||||
}
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
...requiredConfigs,
|
...environment,
|
||||||
...smtpConfigs,
|
...server,
|
||||||
...neo4jConfigs,
|
...required,
|
||||||
...serverConfigs,
|
...smtp,
|
||||||
...developmentConfigs,
|
...neo4j,
|
||||||
...sentryConfigs,
|
...sentry,
|
||||||
...redisConfigs,
|
...redis,
|
||||||
...s3Configs,
|
...s3,
|
||||||
...customConfigs,
|
...options,
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import { cleanDatabase } from '../db/factories'
|
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!`)
|
throw new Error(`You cannot clean the database in production environment!`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -13,7 +13,7 @@ const hasAuthData = CONFIG.SMTP_USERNAME && CONFIG.SMTP_PASSWORD
|
|||||||
|
|
||||||
let sendMail = () => {}
|
let sendMail = () => {}
|
||||||
if (!hasEmailConfig) {
|
if (!hasEmailConfig) {
|
||||||
if (process.env.NODE_ENV !== 'test') {
|
if (!CONFIG.TEST) {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.log('Warning: Email middleware will not try to send mails.')
|
console.log('Warning: Email middleware will not try to send mails.')
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,16 +1,16 @@
|
|||||||
import { sentry } from 'graphql-middleware-sentry'
|
import { sentry } from 'graphql-middleware-sentry'
|
||||||
import { sentryConfigs } from '../config'
|
import CONFIG from '../config'
|
||||||
|
|
||||||
let sentryMiddleware = (resolve, root, args, context, resolveInfo) =>
|
let sentryMiddleware = (resolve, root, args, context, resolveInfo) =>
|
||||||
resolve(root, args, context, resolveInfo)
|
resolve(root, args, context, resolveInfo)
|
||||||
|
|
||||||
if (sentryConfigs.SENTRY_DSN_BACKEND) {
|
if (CONFIG.SENTRY_DSN_BACKEND) {
|
||||||
sentryMiddleware = sentry({
|
sentryMiddleware = sentry({
|
||||||
forwardErrors: true,
|
forwardErrors: true,
|
||||||
config: {
|
config: {
|
||||||
dsn: sentryConfigs.SENTRY_DSN_BACKEND,
|
dsn: CONFIG.SENTRY_DSN_BACKEND,
|
||||||
release: sentryConfigs.COMMIT,
|
release: CONFIG.COMMIT,
|
||||||
environment: process.env.NODE_ENV,
|
environment: CONFIG.NODE_ENV,
|
||||||
},
|
},
|
||||||
withScope: (scope, error, context) => {
|
withScope: (scope, error, context) => {
|
||||||
scope.setUser({
|
scope.setUser({
|
||||||
@ -23,7 +23,7 @@ if (sentryConfigs.SENTRY_DSN_BACKEND) {
|
|||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
// eslint-disable-next-line no-console
|
// 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
|
export default sentryMiddleware
|
||||||
|
|||||||
@ -5,10 +5,10 @@ import slug from 'slug'
|
|||||||
import { existsSync, unlinkSync, createWriteStream } from 'fs'
|
import { existsSync, unlinkSync, createWriteStream } from 'fs'
|
||||||
import { UserInputError } from 'apollo-server'
|
import { UserInputError } from 'apollo-server'
|
||||||
import { getDriver } from '../../../db/neo4j'
|
import { getDriver } from '../../../db/neo4j'
|
||||||
import { s3Configs } from '../../../config'
|
import CONFIG from '../../../config'
|
||||||
|
|
||||||
// const widths = [34, 160, 320, 640, 1024]
|
// 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 = {}) {
|
export async function deleteImage(resource, relationshipType, opts = {}) {
|
||||||
sanitizeRelationshipType(relationshipType)
|
sanitizeRelationshipType(relationshipType)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user