-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (59 loc) · 1.83 KB
/
index.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const express = require("express");
const swaggerUI = require("swagger-ui-express");
const swaggerJsDoc = require("swagger-jsdoc");
const cache = require("memory-cache");
if (process.env.DEV == 'true') {
console.log("DEV MODE");
require("dotenv").config();
}
const routes = require("./routes/routes");
const app = express();
const HTTP_PORT = process.env.PORT || 8000;
const swagger_options = {
definition: {
openapi: "3.0.0",
info: {
title: "Gas Inventory API",
version: "1.0.0",
description: "A simple Gas Emission Inventory API",
},
servers: [{
url: "https://gas-inventory-api.herokuapp.com/",
}],
},
apis: ["./routes/*.js"],
};
const swagger_specs = swaggerJsDoc(swagger_options);
app.use("/api-docs", swaggerUI.serve, swaggerUI.setup(swagger_specs));
app.use('/', routes);
app.use(express.json());
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.contentType('application/json');
next();
});
// set up using cache middleware for 1 minute
const memCache = new cache.Cache();
const cacheMiddleware = (duration) => {
return (req, res, next) => {
let key = '__express__' + req.originalUrl || req.url;
let cacheContent = memCache.get(key);
if (cacheContent) {
console.log("Cache hit for " + key);
res.send(cacheContent);
return;
} else {
res.sendResponse = res.send;
res.send = (body) => {
memCache.put(key, body, duration * 1000);
res.sendResponse(body);
}
next();
}
}
}
app.use(cacheMiddleware(60));
app.listen(HTTP_PORT, '0.0.0.0', () => {
console.log("Server is listening on port " + HTTP_PORT);
});