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

增加对环境变量支持。 #189

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
47 changes: 47 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
MAIN_DOMAIN= # 主域名
LOCAL_SERVER_DOMAIN=localhost:8011
LOCAL_ASSETS_DOMAIN=localhost:8010
LOCAL_SERVER_PORT=8011
LOCAL_ASSETS_PORT=8010

PURCHASE_CODE= # 激活码

MESSAGE_QUEUE=false
# amqp读写格式:[protocol]://[user]:[pass]@[host]:[port]/[vhost]?[connection params url query]
# 示例: amqp://guest:[email protected]:6379/
QUEUE_HOST=amqp://localhost


OPEN_MONITOR=true

SAVE_DAYS=8

STAY_TIME_SCOPE_MIN=100
STAY_TIME_SCOPE_MAX=100000

MYSQL_HOST=127.0.0.1
MYSQL_PORT=3306
MYSQL_USER=root
MYSQL_PASS=root
MYSQL_DBNAME=webfunny_monitor

MYSQL_POOL_MIN=0
MYSQL_POOL_MAX=500
MYSQL_POOL_IDLE=10000
MYSQL_POOL_ACQUIRE=30000

# mysql读写格式:[protocol]://[user]:[pass]@[host]:[port]/[dbname]?[connection params url query]
# 示例: mysql://root:[email protected]:3306/webfunny_monitor?timeout=10&charset=utf8
#MYSQL_WRIT=
## 多个用','分隔开
#MYSQL_READ=

USE_CUS_EMAIL_SYS=false
# 以下是163邮箱账号
EMAIL_USER=
EMAIL_PASS=

# 请求最大长度
REQUEST_TEXT_LENGTH=1000
# 响应最大长度
RESPONSE_TEXT_LENGTH=1000
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ lib/temp.min.js.map
webfunny/
schema_temp/
.eslintrc
lib/*.map
lib/*.map
.env
test.js
package-lock.json
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone
EXPOSE 8010
EXPOSE 8011
CMD npm run prd
CMD npm run prd
50 changes: 34 additions & 16 deletions config/AccountConfig.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require('dotenv').config() // dotenv autoload
const { saveDays } = require("../bin/saveDays")
const stayTimeScope = require("../bin/stayTimeScope")
const mysqlConfig = require("../bin/mysqlConfig")
Expand All @@ -8,33 +9,50 @@ const httpReqRes = require("../bin/httpReqRes")
const { useCusEmailSys, emailUser, emailPassword } = require("../bin/useCusEmailSys")
const { localServerDomain, localAssetsDomain, localServerPort, localAssetsPort, mainDomain } = require("../bin/domain")

const { getenv, decodeUri } = require("../util/utils")

const accountInfo = {

localServerDomain, // 日志服务域名
localServerPort, // 日志服务端口号
localServerDomain: getenv('LOCAL_SERVER_DOMAIN', localServerDomain), // 日志服务域名
localServerPort: getenv('LOCAL_SERVER_PORT', localServerPort), // 日志服务端口号

localAssetsDomain, // 数据可视化服务域名
localAssetsPort, // 可视化系统端口号
localAssetsDomain: getenv('LOCAL_ASSETS_DOMAIN', localAssetsDomain), // 数据可视化服务域名
localAssetsPort: getenv('LOCAL_ASSETS_PORT', localAssetsPort), // 可视化系统端口号

mainDomain, // 主域名
mainDomain: getenv('MAIN_DOMAIN', mainDomain), // 主域名

purchaseCode, // 激活码
messageQueue, // 消息队列默认关闭,需手动开启。 前提:安装RabbitMq;
openMonitor, // 开启系统监控
purchaseCode: getenv('PURCHASE_CODE', purchaseCode), // 激活码
messageQueue: getenv('MESSAGE_QUEUE', messageQueue), // 消息队列默认关闭,需手动开启。 前提:安装RabbitMq;
openMonitor: getenv('OPEN_MONITOR', openMonitor), // 开启系统监控

saveDays, // 日志保存周期
saveDays: getenv('SAVE_DAYS', saveDays), // 日志保存周期

stayTimeScope, // 用户停留时间范围
stayTimeScope: {
min: getenv('STAY_TIME_SCOPE_MIN', stayTimeScope.min),
max: getenv('STAY_TIME_SCOPE_MAX', stayTimeScope.max),
}, // 用户停留时间范围

mysqlConfig,
mysqlConfig: {
write: {
ip: getenv('MYSQL_HOST', mysqlConfig.write.ip),
port: getenv('MYSQL_PORT', mysqlConfig.write.port),
dataBaseName: getenv('MYSQL_DBNAME', mysqlConfig.write.dataBaseName),
userName: getenv('MYSQL_USER', mysqlConfig.write.userName),
password: getenv('MYSQL_PASS', mysqlConfig.write.password)
},
read: !!getenv('MYSQL_READ') ? getenv('MYSQL_READ').split(',').map(uri => decodeUri(uri)): [],
},

useCusEmailSys, // 是否开启自定义邮件系统
emailUser,
emailPassword,
useCusEmailSys: !!getenv('USE_CUS_EMAIL_SYS', useCusEmailSys), // 是否开启自定义邮件系统
emailUser: getenv('EMAIL_USER', emailUser),
emailPassword: getenv('EMAIL_PASS', emailPassword),

httpReqRes, // 接口内容长度限制
httpReqRes: {
requestTextLength: getenv('REQUEST_TEXT_LENGTH', httpReqRes.requestTextLength),
responseTextLength: getenv('RESPONSE_TEXT_LENGTH', httpReqRes.responseTextLength),
}, // 接口内容长度限制
}

module.exports = {
accountInfo
}
}
26 changes: 13 additions & 13 deletions config/db_local.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@

const Sequelize = require('sequelize');
const mysqlConfig = require("../bin/mysqlConfig")
const {write, read} = mysqlConfig
const {accountInfo} = require('./AccountConfig');
const {getenv} = require('../util/utils');

const replication = {
read: read,
write: { host: write.ip, username: write.userName, password: write.password }
read: accountInfo.mysqlConfig.read,
write: accountInfo.mysqlConfig.write
}
const configList = {
host: write.ip,
port: write.port,
host: accountInfo.mysqlConfig.write.ip,
port: accountInfo.mysqlConfig.write.port,
logging: (sql) => {
// 这里处理sql的日志,暂时不打印
// console.log(sql)
Expand All @@ -21,18 +21,18 @@ const configList = {
bigNumberStrings: true
},
pool: {
max: 500,
min: 0,
acquire: 30000,
idle: 10000
max: parseInt(getenv('MYSQL_POOL_MAX', '500'), 10),
min: parseInt(getenv('MYSQL_POOL_MIN', '0'), 10),
acquire: parseInt(getenv('MYSQL_POOL_ACQUIRE','30000'), 10),
idle: parseInt(getenv('MYSQL_POOL_IDLE', '10000'), 10)
},
timezone: '+08:00' //东八时区
}
if (read && read.length > 0) {
if (accountInfo.mysqlConfig.read && accountInfo.mysqlConfig.read.length > 0) {
configList.replication = replication
}
// 下一个迭代数据库
const sequelize = new Sequelize(write.dataBaseName, write.userName, write.password, {
const sequelize = new Sequelize(accountInfo.mysqlConfig.write.dataBaseName, accountInfo.mysqlConfig.write.userName, accountInfo.mysqlConfig.write.password, {
...configList
})

Expand Down
4 changes: 3 additions & 1 deletion lib/RabbitMQ.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
require('dotenv').config() // dotenv autoload
let amqp = require('amqplib');
const { getenv } = require("../util/utils")


module.exports = class RabbitMQ {
constructor() {
this.hosts = ["amqp://localhost"];
this.hosts = !!getenv('AMQP_HOST')? getenv('AMQP_HOST').split(',') : ["amqp://localhost"];
this.index = 0;
this.length = this.hosts.length;
this.open = amqp.connect(this.hosts[this.index]);
Expand Down
Loading