Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
84 lines (70 loc) · 2.84 KB
/
Copy pathserver.js
File metadata and controls
84 lines (70 loc) · 2.84 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
const { config } = require('./src/config/config');
const express = require('express');
const rateLimit = require('express-rate-limit');
const helmet = require('helmet');
const yaml = require('js-yaml');
const v1Routes = require("./src/v1/routes");
const { authorizeRequest } = require('./src/v1/middlewares/api-key-authorization');
const app = express();
app.use(helmet());
app.use(rateLimit({
windowMs: 60 * 1000,
limit: 300,
standardHeaders: 'draft-8',
legacyHeaders: false,
}));
app.use(express.json({ limit: '1mb' }));
app.use("/v1", v1Routes);
// Catch-all error handler
app.use(function(err, req, res, _next) {
console.error('Internal server error:', err.message);
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', authorizeRequest, swaggerUi.serve);
// Workaround to allow user to download swagger.json file
app.get('/api-docs', authorizeRequest, swaggerUi.setup(null, {
swaggerOptions: {
url: '/api-docs/swagger.json'
}
}));
app.get('/api-docs/swagger.json', authorizeRequest, (req, res) => res.json(openapiSpecification));
app.get('/api-docs/swagger.yaml', authorizeRequest, (req, res) => {
res.type('yaml').send(yaml.dump(openapiSpecification));
});
app.listen(config.port, () => {
console.log("Actual HTTP Server Listening on PORT: ", config.port);
if (config.swagger.protocol !== 'https') {
console.warn(
'WARNING: SWAGGER_PROTOCOL is not set to "https". ' +
'Run this service behind a TLS-terminating proxy to protect credentials in transit.'
);
}
});
/**
* 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 (isErrorComingFromActualApi(reason) && !doesActualErrorRequiresRestartingTheHttpService(reason)) {
console.log('Ignoring unhandledRejection caused by Actual api library');
return;
}
console.log('unhandledRejection', reason);
process.exit(1);
}
function isErrorComingFromActualApi(reason) {
return reason
&& ((reason.stack && reason.stack.indexOf('@actual-app/api') != -1) || reason.type == 'APIError');
}
/**
* Forcing a restart if there is a problem opening a budget that was successfully
* opened before.
*/
function doesActualErrorRequiresRestartingTheHttpService(reason) {
return reason && reason.stack && reason.stack.indexOf('We had an unknown problem opening') != -1;
}
process.on('unhandledRejection', ignoreUnhandledRejectionsCausedByActualApiLibrary);