-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb.server.js
More file actions
123 lines (108 loc) · 3.74 KB
/
Copy pathweb.server.js
File metadata and controls
123 lines (108 loc) · 3.74 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// =========================================定义基本模块=======================================================
const ServerPort = process.env.PORT;
const ENV_Production = process.env.NODE_ENV === 'production';
const path = require('path');
const UUID = require('uuid');
const morgan = require('koa-morgan');
const Koa = require('koa');
const KoaStatic = require('koa-static');
const KoaBody = require('koa-body');
const cors = require('koa2-cors');
const createError = require('http-errors');
// ==========================================定义全局变量====================================================
const { CookieSession } = require('./constant');
// ==========================================引入核心模块======================================================
const Model = require('./framework/model.js');
const Controller = require('./framework/controller.js');
// ==========================================引入中间件======================================================
const MiddlewareView = require('./middleware/nunjucks');
// ===========================================引入数据库驱动管理器=====================================================
const DBManager = require(`./components/db-manager/index.js`);
// ===========================================引入socket=====================================================
const SocketIO = require(`./components/socket/socket-io`);
const WebSocket = require(`./components/socket/websocket/wss`);
// ============================================引入redis====================================================
const ClientRedis = require(`./components/db-manager/client-redis.js`);
const RedisBiz = require(`./biz/RedisBiz`);
// ================================================================================================
class WebApp {
/**
* 创建数据库链接、定义模型
*/
async initDatabase() {
await DBManager.loadNoSql();
Model.loadNOSQL();
Model.defineNoSql();
let instance = await DBManager.loadSql();
Model.loadSQL();
Model.defineSql(instance);
}
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 Inter Web');
console.error(err);
});
app.use(cors());
app.use(morgan('dev'));
app.use(KoaStatic('./assets/')); //建议加cdn
app.use(RedisBiz.loadSessionFromRedis());
app.use(
KoaBody({
multipart: true,
encoding: 'utf-8',
formidable: {
uploadDir: './upload',
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(
MiddlewareView(path.join(__dirname, 'view'), {
noCache: !ENV_Production,
watch: !ENV_Production,
})
);
app.use(Controller());
this.app = app;
this.start();
this.startSocket();
process.on('uncaughtException', this.stop);
process.on('SIGINT', this.stop);
}
start() {
this.http_server = this.app.listen(ServerPort, () => {
console.log(
'【web】 127.0.0.1:' +
ServerPort +
'启动完成!进程号:' +
process.pid
);
});
}
startSocket() {
new SocketIO().bindWeb(this.http_server);
}
createWebSocketServer() {
require(`./components/socket/websocket/wss`)(this.http_server); //TODO remove me
}
stop(sth) {
setTimeout(() => {
console.log('【Web Server Stopped】', sth);
process.exit(0);
});
}
}
new WebApp().createDefaultApp();