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