-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
73 lines (62 loc) · 1.99 KB
/
app.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
70
71
72
73
const compression = require("compression");
const helmet = require("helmet");
const express = require("express");
const cors = require("cors");
const morgan = require("morgan");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
// Local Imports
const Data = require("./models/data");
const getData = require("./scraping/get_data");
const getDataFunctions = require("./helpers/get_data_functions");
// Create a new Express app
const app = express();
// Middleware
app.use(morgan("dev"));
app.use(compression());
app.use(helmet());
app.use(cors());
app.use(express.json());
app.use(bodyParser.json());
/**
* Database connection.
* @todo Move to separate file.
* @todo Use env for connection strings
*/
mongoose.Promise = global.Promise;
let dburi = "mongodb://10.0.0.2:27017/commondata";
if (process.env.NODE_ENV === "test") {
dburi = dburi + "TEST";
}
const connectDB = async () => {
try {
const connection = await mongoose.connect(dburi, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.info(`MongoDB connected: ${connection.connection.name}`);
} catch (error) {
console.error(`MongoDB error when connecting: ${error}`);
}
};
connectDB();
/**
* Admin actions
* @todo Flesh these out a bit more. What does the UX look like for an admin user? Probably should be manual triggers for a while, but what does that look like??
* @todo Move these to a more appropriate file with more appropriate trigger.
*/
// getDataFunctions.upsertAllData(getData, Data);
// getDataFunctions.diffAllData(getData, Data);
/**
* Create all routes
* @todo Move to a more appropriate file
*/
for (let key in getData) {
app.get(`/${key}`, async (req, res) => {
const storedData = await Data.findOne({ name: key });
res.status(200).json(storedData);
});
}
const port = process.env.SERVER_PORT || 8080;
app.listen(port, () => console.info(`Server listening on port ${port}`));
module.exports = app;