forked from jhonderson/actual-http-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
55 lines (44 loc) · 1.71 KB
/
server.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
const express = require('express');
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config();
}
const v1Routes = require("./src/v1/routes");
const app = express();
app.use(express.json());
app.use("/v1", v1Routes);
// Catch-all error handler
app.use(function(err, req, res, next) {
console.log('Internal server error:', err);
res.status(err.status || 500).json({"error": "Internal server error"});
});
const swaggerUi = require('swagger-ui-express');
const { openapiSpecification } = require('./src/config/swagger');
app.use('/api-docs', swaggerUi.serve);
// Workaround to allow user to download swagger.json file
app.get('/api-docs', swaggerUi.setup(null, {
swaggerOptions: {
url: '/api-docs/swagger.json'
}
}));
app.get('/api-docs/swagger.json', (req, res) => res.json(openapiSpecification));
const port = process.env.PORT || 5007;
app.listen(port, () => {
console.log("Actual HTTP Server Listening on PORT: ", port);
});
/**
* Errors generated by @actual-app/api library make the server crash.
* This can be problematic for the HTTP api since a normal behaviour such as looking
* for an nonexisting account would make the app crash. Preventing this by capturing
* the unhandled rejection errors and ignoring them if they come from @actual-app/api
*/
function ignoreUnhandledRejectionsCausedByActualApiLibrary(reason, promise) {
if (reason
&& ((reason.stack && reason.stack.indexOf('@actual-app/api') != -1)
|| reason.type == 'APIError')) {
console.log('Ignoring unhandledRejection caused by Actual api library');
return;
}
console.log('unhandledRejection', reason);
process.exit(1);
}
process.on('unhandledRejection', ignoreUnhandledRejectionsCausedByActualApiLibrary);