-
Notifications
You must be signed in to change notification settings - Fork 13
/
app.js
86 lines (77 loc) · 2.65 KB
/
app.js
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
const Koa = require('koa');
const app = new Koa();
const mysql = require('mysql');
const redis = require('redis');
const Db = require('mongodb').Db;
const Server = require('mongodb').Server;
/*-------------------------mysql----------------------------------------*/
const mysqlClient = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE
});
mysqlClient.connect();
async function getMysqlVersion() {
let result = await new Promise((resolve, reject) => {
mysqlClient.query('select version() as version', function (err, rows, fields) {
if (err) return reject(err);
resolve(rows);
});
}).catch((err) => {
throw new Error(err);
});
return result;
}
/*-------------------------mongodb---------------------------------------*/
async function getMongoVersion() {
const db = new Db('test', new Server(process.env.MONGO_HOST, 27017));
let mongoClient = await new Promise((resolve, reject) => {
db.open((err, db) => {
if (err)return reject(err);
resolve(db);
});
}).catch((err) => {
throw new Error(err);
});
const adminDb = mongoClient.admin();
let info = await new Promise((resolve, reject) => {
adminDb.buildInfo(function (err, info) {
if (err)return reject(err);
resolve(info);
});
}).catch((err) => {
throw new Error(err);
});
return info;
}
/*---------------------redis---------------------------------------------*/
const redisClient = redis.createClient('6379', process.env.REDIS_HOST);
redisClient.on("error", (err) => {
throw new Error(err);
});
/*-------------------------koa2----------------------------------------*/
app.use(async(ctx) => {
try {
let mysqlVersion = await getMysqlVersion();
let redisVersion = redisClient.server_info.redis_version;
let mongoVersion = await getMongoVersion();
ctx.body = `
<h1>Dcoker+Koa2+MongoDB+Redis+MySQL</h1>
<h4>Integrate development environment</h4>
MySQL Version: ${mysqlVersion[0].version}<br />
MongoDB Version: ${mongoVersion.version}<br />
Node Version: ${process.version}<br />
Redis Version: ${redisVersion}
<div style="margin-top:20px">
Power by <a href="http://www.myfreax.com/" target="_blank">huangyanxiong</a>
</div>
`;
} catch (e) {
ctx.status = 500;
ctx.body = e.stack;
}
});
app.listen(process.env.APP_PORT, () => {
console.info(`open http://127.0.0.1:${process.env.NGINX_PORT}`);
});