forked from lisong/code-push-server
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathconfig.ts
149 lines (144 loc) · 5.93 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import os from 'os';
import {
setLogTransports,
ConsoleLogTransport,
logger,
LogLevel,
withLogLevelFilter,
} from 'kv-logger';
function toBool(str: string): boolean {
return str === 'true' || str === '1';
}
function toNumber(str: string, defaultValue: number): number {
const num = Number(str);
if (Number.isNaN(num)) {
return defaultValue;
}
return num;
}
export const config = {
// Config for log
log: {
// debug, info, warn, error
level: process.env.LOG_LEVEL || 'info',
// text, json
format: process.env.LOG_FORMAT || 'text',
},
// Config for database, only support mysql.
db: {
username: process.env.RDS_USERNAME || 'root',
password: process.env.RDS_PASSWORD || 'password',
database: process.env.RDS_DATABASE || 'codepush',
host: process.env.RDS_HOST || '127.0.0.1',
port: toNumber(process.env.RDS_PORT, 3306),
dialect: 'mysql',
logging: false,
},
// Config for qiniu (http://www.qiniu.com/) cloud storage when storageType value is "qiniu".
qiniu: {
accessKey: process.env.QINIU_ACCESS_KEY,
secretKey: process.env.QINIU_SECRET_KEY,
bucketName: process.env.QINIU_BUCKET_NAME,
downloadUrl: process.env.QINIU_DOWNLOAD_URL || process.env.DOWNLOAD_URL,
},
// Config for Amazon s3 (https://aws.amazon.com/cn/s3/) storage when storageType value is "s3".
s3: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
sessionToken: process.env.AWS_SESSION_TOKEN, // (optional)
bucketName: process.env.AWS_BUCKET_NAME,
region: process.env.AWS_REGION,
// binary files download host address.
downloadUrl: process.env.AWS_DOWNLOAD_URL || process.env.DOWNLOAD_URL,
},
// Config for Aliyun OSS (https://www.aliyun.com/product/oss) when storageType value is "oss".
oss: {
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
secretAccessKey: process.env.OSS_SECRET_ACCESS_KEY,
endpoint: process.env.OSS_ENDPOINT,
bucketName: process.env.OSS_BUCKET_NAME,
// Key prefix in object key
prefix: process.env.OSS_PREFIX,
// binary files download host address
downloadUrl: process.env.OSS_DOWNLOAD_URL || process.env.DOWNLOAD_URL,
},
// Config for tencentyun COS (https://cloud.tencent.com/product/cos) when storageType value is "oss".
tencentcloud: {
accessKeyId: process.env.COS_ACCESS_KEY_ID,
secretAccessKey: process.env.COS_SECRET_ACCESS_KEY,
bucketName: process.env.COS_BUCKET_NAME,
region: process.env.COS_REGION,
// binary files download host address
downloadUrl: process.env.COS_DOWNLOAD_URL || process.env.DOWNLOAD_URL,
},
// Config for local storage when storageType value is "local".
local: {
// Binary files storage dir, Do not use tmpdir and it's public download dir.
storageDir: process.env.STORAGE_DIR || os.tmpdir(),
// Binary files download host address which Code Push Server listen to. the files storage in storageDir.
downloadUrl:
process.env.LOCAL_DOWNLOAD_URL ||
process.env.DOWNLOAD_URL ||
'http://127.0.0.1:3000/download',
// public static download spacename.
public: '/download',
},
jwt: {
// Recommended: 63 random alpha-numeric characters
// Generate using: https://www.grc.com/passwords.htm
tokenSecret: process.env.TOKEN_SECRET || 'INSERT_RANDOM_TOKEN_KEY',
},
common: {
// determine whether new account registrations are allowed
allowRegistration: toBool(process.env.ALLOW_REGISTRATION),
/*
* tryLoginTimes is control login error times to avoid force attack.
* if value is 0, no limit for login auth, it may not safe for account. when it's a number, it means you can
* try that times today. but it need config redis server.
*/
tryLoginTimes: toNumber(process.env.TRY_LOGIN_TIMES, 4),
// create patch updates's number. default value is 3
diffNums: toNumber(process.env.DIFF_NUMS, 3),
// data dir for caclulate diff files. it's optimization.
dataDir: process.env.DATA_DIR || os.tmpdir(),
// storageType which is your binary package files store. options value is ("local" | "qiniu" | "s3"| "oss" || "tencentcloud")
storageType: (process.env.STORAGE_TYPE || 'local') as
| 'local'
| 'qiniu'
| 's3'
| 'oss'
| 'tencentcloud',
// options value is (true | false), when it's true, it will cache updateCheck results in redis.
updateCheckCache: toBool(process.env.UPDATE_CHECK_CACHE),
// options value is (true | false), when it's true, it will cache rollout results in redis
rolloutClientUniqueIdCache: toBool(process.env.ROLLOUT_CLIENT_UNIQUE_ID_CACHE),
},
// Config for smtp email,register module need validate user email project source https://github.com/nodemailer/nodemailer
smtpConfig: {
host: process.env.SMTP_HOST,
port: toNumber(process.env.SMTP_PORT, 465),
secure: true,
auth: {
user: process.env.SMTP_USERNAME,
pass: process.env.SMTP_PASSWORD,
},
},
// Config for redis (register module, tryLoginTimes module)
redis: {
host: process.env.REDIS_HOST || '127.0.0.1',
port: toNumber(process.env.REDIS_PORT, 6379),
password: process.env.REDIS_PASSWORD,
db: toNumber(process.env.REDIS_DB, 0),
},
} as const;
// config logger - make sure its ready before anyting else
setLogTransports(
withLogLevelFilter(config.log.level as LogLevel)(
new ConsoleLogTransport(config.log.format as 'text' | 'json'),
),
);
const env = process.env.NODE_ENV || 'development';
logger.info(`use config`, {
env,
storageType: config.common.storageType,
});