-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.server.js
More file actions
113 lines (99 loc) · 3.86 KB
/
Copy pathapi.server.js
File metadata and controls
113 lines (99 loc) · 3.86 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
// =========================================定义基本模块=======================================================
const ENV_Production = process.env.NODE_ENV === 'production';
const ServerPort = process.env.PORT;
const path = require('path');
const UUID = require('uuid');
const Koa = require('koa');
const morgan = require('koa-morgan');
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 Rest = require(`./framework/api.js`);
// ===========================================引入数据库驱动管理器=====================================================
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 ApiApp {
/**
* 创建数据库链接、定义模型
*/
async initDatabase() {
// no sql
await DBManager.loadNoSql();
Model.loadNOSQL();
Model.defineNoSql();
// sql
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 Inner Api');
console.error(err);
});
app.use(cors());
app.use(morgan('short'));
app.use(RedisBiz.loadSessionFromRedis());
app.use(
KoaBody({
multipart: true,
encoding: 'utf-8',
formidable: {
uploadDir: './src/assets/uploaded',
keepExtensions: true,
maxFieldsSize: 5 * 1024 * 1024,
onFileBegin: (name, file) => {
console.log(name, file);
// console.log('onFileBegin', name, file)
//TODO file.path = '/data/www/web-node/upload/upload_8test.jpg';//此处可以修改file来做修改的,比如按照年/月/日创建层级目录
},
},
})
);
app.use(Rest.restify());
app.use(Rest.routes());
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(
'【api】 127.0.0.1:' +
ServerPort +
' 启动完成!进程号:' +
process.pid
);
});
}
startSocket() {
new SocketIO().listen(10099);
}
stop(sth) {
setTimeout(() => {
console.log('【Api Server Stopped】', sth);
process.exit(0);
});
}
}
new ApiApp().createDefaultApp();