-
-
Notifications
You must be signed in to change notification settings - Fork 333
/
index.ts
129 lines (109 loc) · 3.62 KB
/
index.ts
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
124
125
126
127
128
129
/*
* Copyright 2021 WPPConnect Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { defaultLogger } from '@wppconnect-team/wppconnect';
import cors from 'cors';
import express, { NextFunction } from 'express';
import boolParser from 'express-query-boolean';
import { createServer } from 'http';
import mergeDeep from 'merge-deep';
import { Server as Socket } from 'socket.io';
import { version } from '../package.json';
import config from './config';
import { convert } from './mapper/index';
import routes from './routes';
import {
createFolders,
setMaxListners,
startAllSessions,
} from './util/functions';
import { createLogger } from './util/logger';
//require('dotenv').config();
export const logger = createLogger(config.log);
export function initServer(serverOptions: any) {
if (typeof serverOptions !== 'object') {
serverOptions = {};
}
serverOptions = mergeDeep({}, config, serverOptions);
defaultLogger.level = serverOptions.log.level;
setMaxListners(serverOptions);
const app = express();
const PORT = process.env.PORT || serverOptions.port;
app.use(cors());
app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ limit: '50mb', extended: true }));
app.use('/files', express.static('WhatsAppImages'));
app.use(boolParser());
// Add request options
app.use((req: any, res: any, next: NextFunction) => {
req.serverOptions = serverOptions;
req.logger = logger;
req.io = io as any;
const oldSend = res.send;
res.send = async function (data: any) {
const content = req.headers['content-type'];
if (content == 'application/json') {
data = JSON.parse(data);
if (!data.session) data.session = req.client ? req.client.session : '';
if (data.mapper && req.serverOptions.mapper.enable) {
data.response = await convert(
req.serverOptions.mapper.prefix,
data.response,
data.mapper
);
delete data.mapper;
}
}
res.send = oldSend;
return res.send(data);
};
next();
});
app.use(routes);
createFolders();
const http = createServer(app);
const io = new Socket(http, {
cors: {
origin: '*',
},
});
io.on('connection', (sock) => {
logger.info(`ID: ${sock.id} entrou`);
sock.on('disconnect', () => {
logger.info(`ID: ${sock.id} saiu`);
});
});
http.listen(PORT, () => {
logger.info(`Server is running on port: ${PORT}`);
logger.info(
`\x1b[31m Visit ${serverOptions.host}:${PORT}/api-docs for Swagger docs`
);
logger.info(`WPPConnect-Server version: ${version}`);
if (serverOptions.startAllSession) startAllSessions(serverOptions, logger);
});
if (config.log.level === 'error' || config.log.level === 'warn') {
console.log(`\x1b[33m ======================================================
Attention:
Your configuration is configured to show only a few logs, before opening an issue,
please set the log to 'silly', copy the log that shows the error and open your issue.
======================================================
`);
}
return {
app,
routes,
logger,
};
}