-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb.server.js
More file actions
102 lines (90 loc) · 4.13 KB
/
Copy pathweb.server.js
File metadata and controls
102 lines (90 loc) · 4.13 KB
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
// =========================================定义基本模块============================================
const path = require('path');
const UUID = require('uuid');
const morgan = require('koa-morgan');
const Koa = require('koa');
const KoaStaticCache = require('koa-static-cache');
const KoaBody = require('koa-body');
const cors = require('koa2-cors');
const createError = require('http-errors');
const ServerPort = process.env.PORT;
const ENV_Production = process.env.NODE_ENV === 'production';
// ==========================================定义全局变量===========================================
const { CookieSession } = require('./constant');
// ==========================================引入核心模块===========================================
const SBiz = require('./base/SBiz');
// ==========================================引入中间件=============================================
const Nunjucks = require('./middleware/nunjucks');
const UserSession = require('./middleware/user-session');
// ===========================================引入数据库驱动管理器====================================
const DBManager = require(`./components/db-client/index.js`);
// ===========================================引入socket===========================================
const SocketIOManager = require(`./components/socket/socket-io`);
// const WebSocket = require(`./components/socket/websocket/wss`);
// ============================================引入redis===========================================
const ClientRedis = require(`./components/db-client/client-redis.js`);
// ===============================================================================================
class WebApp {
/**
* 创建数据库链接、定义模型
*/
async _initDatabase() {
await DBManager.connectMongodb();
SBiz.setMysqlInstance(await DBManager.connectMysql());
}
async _initRedis() {
global.DB_Redis = await ClientRedis.getClient();
}
async createDefaultApp() {
await this._initDatabase();
await this._initRedis();
const app = new Koa({ keys: CookieSession.CookieKeys });
app.on('error', (err, ctx) => {
console.error('Oh~Oh~Oh!!!! Find An Error Inner Api');
console.error(err);
});
app.use(cors());
app.use(morgan('dev'));
app.use(KoaStaticCache(__dirname + '/assets', { maxAge: 30 * 24 * 60 * 60 })); //建议加cdn
app.use(KoaBody({
multipart: true,
encoding: 'utf-8',
formidable: {
uploadDir: __dirname + '/assets/uploaded',// TODO use oss
keepExtensions: true,
maxFieldsSize: 5 * 1024 * 1024,
onFileBegin: (name, file) => {
// console.log('onFileBegin', name, file)
//TODO file.path = '/data/www/web-node/upload/upload_8test.jpg';//此处可以修改file来做修改的,比如按照年/月/日创建层级目录
},
},
}));
app.use(Nunjucks(path.join(__dirname, 'view'), { noCache: !ENV_Production, watch: !ENV_Production }));
app.use(UserSession.loadSessionFromRedis());
const Router = require('./router');
app.use(Router.routerWeb.routes());
app.use(Router.routerWeb.allowedMethods());
this.app = app;
this.start();
process.on('uncaughtException', function (err) {
console.log('【Web Server uncaughtException', err);
});
process.on('SIGINT', this.stop);
}
start() {
this.http_server = this.app.listen(ServerPort, () => {
console.log('【web】 http://127.0.0.1:' + ServerPort + '/ 启动完成!进程号:' + process.pid);
});
new SocketIOManager(this.http_server).build();
// require(`./components/socket/websocket/wss`)(this.http_server); //TODO remove me
}
stop(sth) {
process.nextTick(() => {
DBManager.close((err) => {
console.log('【Web Server Stopped】', sth, err);
process.exit(0);
});
});
}
}
new WebApp().createDefaultApp();