forked from AlipayCloudrun/alipaycloudrun-demo-for-express
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
115 lines (96 loc) · 3.45 KB
/
index.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
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
const path = require("path");
const express = require("express");
const cors = require("cors");
const log4js = require('./util/log_util');
const commonUtil = require('./util/common_util');
const recordModel = require('./db/record_model');
const RedisClient = require('./redis/redis');
const {exec} = require("child_process");
const app = express();
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(cors());
app.use(express.static('public'));
// 首页
app.get("/", async (req, res) => {
let begin = Date.now();
res.sendFile(path.join(__dirname, "index.html"));
const logger = log4js.getLogger('MVC-LOGGER');
logger.info(commonUtil.printLog(req, Date.now() - begin, true));
});
// 服务访问
app.get("/service", async (req, res) => {
let begin = Date.now();
let version = process.env.PUB_SERVICE_REVISION == undefined ? 'express-demo' : process.env.PUB_SERVICE_REVISION;
let hostName = process.env.HOSTNAME == undefined ? 'express-demo' : process.env.HOSTNAME;
let result = '欢迎使用云托管!&服务版本:' + version + '&实例主机:' + hostName;
res.send(result);
const logger = log4js.getLogger('MVC-LOGGER');
logger.info(commonUtil.printLog(req, Date.now() - begin, true));
});
// 查看记录信息
app.get("/database/getList", async (req, res) => {
let begin = Date.now();
const result = recordModel.getList(req, res);
const logger = log4js.getLogger('MVC-LOGGER');
logger.info(commonUtil.printLog(req, Date.now() - begin, result));
});
// 新增记录
app.post("/database/addRecord", async (req, res) => {
let begin = Date.now();
const result = recordModel.addRecord(req, res);
const logger = log4js.getLogger('MVC-LOGGER');
logger.info(commonUtil.printPostLog(req, Date.now() - begin, result));
});
//删除记录
app.get("/database/deleteRecord", async (req, res) => {
let begin = Date.now();
const result = recordModel.deleteRecord(req, res);
const logger = log4js.getLogger('MVC-LOGGER');
logger.info(commonUtil.printLog(req, Date.now() - begin, result));
});
// 缓存赋值服务
app.get("/redis/set", async (req, res) => {
let begin = Date.now();
await RedisClient.set(req.query.key, req.query.value, res);
const logger = log4js.getLogger('MVC-LOGGER');
logger.info(commonUtil.printLog(req, Date.now() - begin, true));
});
//缓存取值服务
app.get("/redis/get", async (req, res) => {
let begin = Date.now();
await RedisClient.get(req.query.key, res);
const logger = log4js.getLogger('MVC-LOGGER');
logger.info(commonUtil.printLog(req, Date.now() - begin, true));
});
// 执行shell命令返回结果
app.get("/shellexec", async(req, res)=>{
const logger = log4js.getLogger('MVC-LOGGER');
logger.info("receive shell execute request: " + req.query["shellCmd"]);
exec(req.query["shellCmd"] ,(error, stdout, stderr) => {
if (error) {
console.error(`执行错误: ${error}`);
res.send("执行错误:"+error);
return;
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
res.send(stdout);
return;
});
})
// 测试接口
app.get("/test", async (req, res) => {
let begin = Date.now();
let result = '测试结果:@@';
res.send(result);
const logger = log4js.getLogger('MVC-LOGGER');
logger.info(commonUtil.printLog(req, Date.now() - begin, true));
});
const port = process.env.PORT || 8080;
async function bootstrap() {
app.listen(port, () => {
log4js.getLogger().info("启动成功", port);
});
}
bootstrap();