-
Notifications
You must be signed in to change notification settings - Fork 202
/
app.js
75 lines (63 loc) · 2.2 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
require('@babel/register');
const Koa = require('koa');
const app = new Koa();
const bodyParser = require('koa-bodyparser');
const path = require('path');
const static = require('koa-static');
const exec = require('child_process').exec;
const chalk = require('chalk');
const cors = require('./middlewares/koa-cors');
const router = require('./routers/router');
const cookie = require('./util/cookie');
require('./util/colors');
const userInfo = require('./config/user-info');
const package = require('./package.json');
global.userInfo = Object.assign({}, userInfo);
console.log(chalk.green('\n🥳🎉 We had supported config the user cookies. \n'));
if (!(global.userInfo.loginUin || global.userInfo.uin)) {
console.log(chalk.yellow(`😔 The configuration ${chalk.red('loginUin')} or your ${chalk.red('cookie')} in file ${chalk.green('config/user-info')} has not configured. \n`));
}
if (!global.userInfo.cookie) {
console.log(chalk.yellow(`😔 The configuration ${chalk.red('cookie')} in file ${chalk.green('config/user-info')} has not configured. \n`));
}
exec('npm info QQ-Music-API version', (err, stdout, stderr) => {
if(!err){
let version = stdout.trim();
if(package.version < version){
console.log(`Current Version: ${version}, Current Version: ${package.version}, Please update it.`.prompt);
}
}
});
app.use(bodyParser());
app.use(cookie());
app.use(static(
path.join(__dirname, 'public')
));
// logger
app.use(async (ctx, next) => {
await next();
const rt = ctx.response.get('X-Response-Time');
console.log(`${ctx.method} ${ctx.url} - ${rt}`.prompt);
});
// cors
app.use(cors({
origin: (ctx) => ctx.request.header.origin,
exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'],
maxAge: 5,
credentials: true,
allowMethods: ['GET', 'POST', 'DELETE'],
allowHeaders: ['Content-Type', 'Authorization', 'Accept'],
}));
// x-response-time
app.use(async (ctx, next) => {
const start = Date.now();
await next();
const ms = Date.now() - start;
ctx.set('X-Response-Time', `${ms}ms`);
});
app.use(router.routes())
.use(router.allowedMethods());
const PORT = process.env.PORT || 3200;
app.listen(PORT, () => {
console.log(`server running @ http://localhost:${PORT}`.prompt);
});