refactor: environment
This commit is contained in:
parent
aab51da281
commit
968f7fc94b
|
@ -15,7 +15,6 @@ module.exports = {
|
||||||
},
|
},
|
||||||
use: [
|
use: [
|
||||||
initial({
|
initial({
|
||||||
environmentDefines: ['FRONTEND_ORIGIN'],
|
|
||||||
scwpPaths: [
|
scwpPaths: [
|
||||||
/^@avocado/,
|
/^@avocado/,
|
||||||
],
|
],
|
||||||
|
|
|
@ -13,7 +13,6 @@ module.exports = {
|
||||||
},
|
},
|
||||||
use: [
|
use: [
|
||||||
initial({
|
initial({
|
||||||
environmentDefines: ['MYSQL_ORIGIN', 'REDIS_ORIGIN'],
|
|
||||||
scwpPaths: [
|
scwpPaths: [
|
||||||
/^@avocado/,
|
/^@avocado/,
|
||||||
],
|
],
|
||||||
|
@ -34,6 +33,11 @@ module.exports = {
|
||||||
AVOCADO_SERVER: true,
|
AVOCADO_SERVER: true,
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
if ('production' !== process.env.NODE_ENV) {
|
||||||
|
neutrino.config
|
||||||
|
.entry('index')
|
||||||
|
.prepend('dotenv/config');
|
||||||
|
}
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
|
@ -4,7 +4,7 @@ services:
|
||||||
reddichat_redis:
|
reddichat_redis:
|
||||||
image: redis:6
|
image: redis:6
|
||||||
ports:
|
ports:
|
||||||
- 31346:6379
|
- ${REDIS_PORT}:6379
|
||||||
|
|
||||||
reddichat_phpredisadmin:
|
reddichat_phpredisadmin:
|
||||||
image: erikdubbelboer/phpredisadmin:latest
|
image: erikdubbelboer/phpredisadmin:latest
|
||||||
|
@ -17,10 +17,10 @@ services:
|
||||||
reddichat_mysql:
|
reddichat_mysql:
|
||||||
image: mysql:8
|
image: mysql:8
|
||||||
ports:
|
ports:
|
||||||
- 31347:3306
|
- ${MYSQL_PORT}:3306
|
||||||
environment:
|
environment:
|
||||||
- MYSQL_DATABASE=reddichat
|
- MYSQL_DATABASE=${MYSQL_DATABASE}
|
||||||
- MYSQL_ROOT_PASSWORD=UNSAFE_DEV_PASSWORD
|
- MYSQL_ROOT_PASSWORD=${MYSQL_PASSWORD}
|
||||||
command:
|
command:
|
||||||
- '--default-authentication-plugin=mysql_native_password'
|
- '--default-authentication-plugin=mysql_native_password'
|
||||||
|
|
||||||
|
|
|
@ -5,11 +5,12 @@ const airbnb = require('@neutrinojs/airbnb');
|
||||||
const airbnbBase = require('@neutrinojs/airbnb-base');
|
const airbnbBase = require('@neutrinojs/airbnb-base');
|
||||||
const mocha = require('@neutrinojs/mocha');
|
const mocha = require('@neutrinojs/mocha');
|
||||||
const scwp = require('scwp/neutrino');
|
const scwp = require('scwp/neutrino');
|
||||||
const {DefinePlugin} = require('webpack');
|
const {EnvironmentPlugin} = require('webpack');
|
||||||
const nodeExternals = require('webpack-node-externals');
|
const nodeExternals = require('webpack-node-externals');
|
||||||
|
|
||||||
const side = require('./side');
|
const side = require('./side');
|
||||||
|
|
||||||
|
// eslint-disable-next-line import/no-dynamic-require
|
||||||
const pkg = require(`${__dirname}/package.json`);
|
const pkg = require(`${__dirname}/package.json`);
|
||||||
|
|
||||||
const gatherPackagePaths = (root, packageMatchers) => {
|
const gatherPackagePaths = (root, packageMatchers) => {
|
||||||
|
@ -17,7 +18,7 @@ const gatherPackagePaths = (root, packageMatchers) => {
|
||||||
r.concat([
|
r.concat([
|
||||||
(pkg.dependencies || {}),
|
(pkg.dependencies || {}),
|
||||||
(pkg.devDependencies || {}),
|
(pkg.devDependencies || {}),
|
||||||
].reduce((r, deps) => {
|
].reduce((r2, deps) => {
|
||||||
const packageNames = Object.keys(deps);
|
const packageNames = Object.keys(deps);
|
||||||
const packages = [];
|
const packages = [];
|
||||||
for (let i = 0; i < packageNames.length; i++) {
|
for (let i = 0; i < packageNames.length; i++) {
|
||||||
|
@ -26,14 +27,14 @@ const gatherPackagePaths = (root, packageMatchers) => {
|
||||||
packages.push(path.relative(root, path.dirname(require.resolve(packageName))));
|
packages.push(path.relative(root, path.dirname(require.resolve(packageName))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return r.concat(packages);
|
return r2.concat(packages);
|
||||||
}, []))
|
}, []))
|
||||||
), []);
|
), []);
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.initial = (options) => (neutrino) => {
|
exports.initial = (options) => (neutrino) => {
|
||||||
const {
|
const {
|
||||||
environmentDefines = [],
|
environmentDefines = {},
|
||||||
rawScwpPaths = [],
|
rawScwpPaths = [],
|
||||||
scwpPaths = [],
|
scwpPaths = [],
|
||||||
} = options;
|
} = options;
|
||||||
|
@ -66,10 +67,8 @@ exports.initial = (options) => (neutrino) => {
|
||||||
),
|
),
|
||||||
})(neutrino);
|
})(neutrino);
|
||||||
neutrino.config
|
neutrino.config
|
||||||
.plugin('environment-define')
|
.plugin('environment')
|
||||||
.use(DefinePlugin, [
|
.use(EnvironmentPlugin, [environmentDefines]);
|
||||||
environmentDefines.reduce((r, k) => ({...r, [k]: JSON.stringify(process.env[k])}), {})
|
|
||||||
]);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.afterPlatform = (options) => (neutrino) => {
|
exports.afterPlatform = (options) => (neutrino) => {
|
||||||
|
@ -131,4 +130,4 @@ exports.afterPlatform = (options) => (neutrino) => {
|
||||||
whitelist: externalMatcher,
|
whitelist: externalMatcher,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
"contempo": "1.x",
|
"contempo": "1.x",
|
||||||
"debug": "^4.1.1",
|
"debug": "^4.1.1",
|
||||||
"deepmerge": "^4.2.2",
|
"deepmerge": "^4.2.2",
|
||||||
|
"dotenv": "8.2.0",
|
||||||
"express": "^4.17.1",
|
"express": "^4.17.1",
|
||||||
"express-session": "^1.17.1",
|
"express-session": "^1.17.1",
|
||||||
"express-socket.io-session": "^1.3.5",
|
"express-socket.io-session": "^1.3.5",
|
||||||
|
|
|
@ -1,13 +0,0 @@
|
||||||
/* eslint-disable no-undef */
|
|
||||||
|
|
||||||
const withDefault = (variable, value) => ('undefined' !== typeof variable ? variable : value);
|
|
||||||
|
|
||||||
export const mysqlOrigin = withDefault(
|
|
||||||
MYSQL_ORIGIN,
|
|
||||||
'mysql://root:UNSAFE_DEV_PASSWORD@localhost:31347/reddichat',
|
|
||||||
);
|
|
||||||
|
|
||||||
export const redisOrigin = withDefault(
|
|
||||||
REDIS_ORIGIN,
|
|
||||||
'localhost:31346',
|
|
||||||
);
|
|
|
@ -1,15 +1,28 @@
|
||||||
import {registerHooks} from 'scwp';
|
import {registerHooks} from 'scwp';
|
||||||
import Sequelize from 'sequelize';
|
import Sequelize from 'sequelize';
|
||||||
|
|
||||||
import {mysqlOrigin} from '~/common/environment';
|
|
||||||
|
|
||||||
import {allModels} from './models/registrar';
|
import {allModels} from './models/registrar';
|
||||||
|
|
||||||
|
const {
|
||||||
|
MYSQL_USER,
|
||||||
|
MYSQL_PASSWORD,
|
||||||
|
MYSQL_HOST,
|
||||||
|
MYSQL_PORT,
|
||||||
|
MYSQL_DATABASE,
|
||||||
|
} = process.env;
|
||||||
|
|
||||||
let map;
|
let map;
|
||||||
|
|
||||||
export async function createDatabaseConnection() {
|
export async function createDatabaseConnection() {
|
||||||
const Models = Object.values(allModels());
|
const Models = Object.values(allModels());
|
||||||
const sequelize = new Sequelize(mysqlOrigin);
|
const sequelize = new Sequelize({
|
||||||
|
database: MYSQL_DATABASE,
|
||||||
|
dialect: 'mysql',
|
||||||
|
username: MYSQL_USER,
|
||||||
|
host: MYSQL_HOST,
|
||||||
|
password: MYSQL_PASSWORD,
|
||||||
|
port: MYSQL_PORT,
|
||||||
|
});
|
||||||
Models.filter((Model) => Model.attributes).forEach((Model) => {
|
Models.filter((Model) => Model.attributes).forEach((Model) => {
|
||||||
Model.init(Model.attributes, {
|
Model.init(Model.attributes, {
|
||||||
sequelize,
|
sequelize,
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
import redis from 'redis';
|
import redis from 'redis';
|
||||||
import session from 'express-session';
|
import session from 'express-session';
|
||||||
|
|
||||||
import {redisOrigin} from '~/common/environment';
|
const {
|
||||||
|
REDIS_HOST,
|
||||||
|
REDIS_PORT,
|
||||||
|
} = process.env;
|
||||||
|
|
||||||
const [host = 'localhost', port = 31346] = redisOrigin.split(':');
|
const redisClient = redis.createClient(REDIS_PORT, REDIS_HOST);
|
||||||
const redisClient = redis.createClient(port, host);
|
|
||||||
|
|
||||||
// eslint-disable-next-line import/newline-after-import
|
// eslint-disable-next-line import/newline-after-import
|
||||||
const RedisStore = require('connect-redis')(session);
|
const RedisStore = require('connect-redis')(session);
|
||||||
|
|
|
@ -4,15 +4,17 @@ import redisAdapter from 'socket.io-redis';
|
||||||
import {SocketServer} from '@avocado/net/server/socket';
|
import {SocketServer} from '@avocado/net/server/socket';
|
||||||
import socketSession from 'express-socket.io-session';
|
import socketSession from 'express-socket.io-session';
|
||||||
|
|
||||||
import {redisOrigin} from '~/common/environment';
|
|
||||||
|
|
||||||
import passport from './passport';
|
import passport from './passport';
|
||||||
import session from './session';
|
import session from './session';
|
||||||
|
|
||||||
|
const {
|
||||||
|
REDIS_HOST,
|
||||||
|
REDIS_PORT,
|
||||||
|
} = process.env;
|
||||||
|
|
||||||
export function createSocketServer(httpServer) {
|
export function createSocketServer(httpServer) {
|
||||||
const [host = 'localhost', port = 31346] = redisOrigin.split(':');
|
|
||||||
const socketServer = new SocketServer(httpServer, {
|
const socketServer = new SocketServer(httpServer, {
|
||||||
adapter: redisAdapter({host, port}),
|
adapter: redisAdapter({host: REDIS_HOST, port: REDIS_PORT}),
|
||||||
});
|
});
|
||||||
socketServer.io.use(socketSession(session()));
|
socketServer.io.use(socketSession(session()));
|
||||||
socketServer.io.use((socket, next) => {
|
socketServer.io.use((socket, next) => {
|
||||||
|
|
|
@ -3298,6 +3298,11 @@ dot-case@^3.0.3:
|
||||||
no-case "^3.0.3"
|
no-case "^3.0.3"
|
||||||
tslib "^1.10.0"
|
tslib "^1.10.0"
|
||||||
|
|
||||||
|
dotenv@8.2.0:
|
||||||
|
version "8.2.0"
|
||||||
|
resolved "https://npm.i12e.cha0s.io/dotenv/-/dotenv-8.2.0.tgz#97e619259ada750eea3e4ea3e26bceea5424b16a"
|
||||||
|
integrity sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==
|
||||||
|
|
||||||
dottie@^2.0.0:
|
dottie@^2.0.0:
|
||||||
version "2.0.2"
|
version "2.0.2"
|
||||||
resolved "https://npm.i12e.cha0s.io/dottie/-/dottie-2.0.2.tgz#cc91c0726ce3a054ebf11c55fbc92a7f266dd154"
|
resolved "https://npm.i12e.cha0s.io/dottie/-/dottie-2.0.2.tgz#cc91c0726ce3a054ebf11c55fbc92a7f266dd154"
|
||||||
|
|
Loading…
Reference in New Issue
Block a user