This repository was archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
95 lines (81 loc) · 2.24 KB
/
Copy pathserver.js
File metadata and controls
95 lines (81 loc) · 2.24 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
85
86
87
88
89
90
91
92
93
94
95
"use strict";
// init is required to access AWS Secret Manager before running any other code.
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const getAnalyticsRoute = require("./routes/analyticsRoutes");
const getPortfolioRoute = require("./routes/portfolio");
const getStocksRoute = require("./routes/stocks");
const stockWorkers = require("./workers/stocks_worker");
const chartWorkers = require("./workers/charts_worker");
const cron = require("node-cron");
const app = express();
async function initialize() {
await require("./secret/secret")();
require("./auth/auth");
const { PORT = 3000 } = process.env;
const swaggerJsDoc = require("swagger-jsdoc");
const swaggerUi = require("swagger-ui-express");
const swaggerOptions = {
encoding: "utf-8",
swaggerDefinition: {
info: {
title: "Milou Backend API",
description: "Information about users, portfolios and stocks",
servers: ["http://localhost:" + PORT],
},
},
definition: {},
apis: [
"./routes/swagger.js",
"./routes/user.js",
"./models/user.js",
"./models/userToken.js",
"./auth/auth.js",
],
};
const swaggerDocs = swaggerJsDoc(swaggerOptions);
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocs));
const getUserRoute = require("./routes/user");
app.use("/user", getUserRoute);
require("./db/index.js")(app);
}
if (process.env.NODE_ENV != "test") {
initialize();
}
if (process.env.NODE_ENV == "staging") {
cron.schedule(
"0 13 * * *",
() => {
stockWorkers.updateAllStocks();
chartWorkers.updateAllCharts();
},
{
timezone: "Europe/Berlin",
}
);
} else {
cron.schedule(
"0 4 * * *",
() => {
stockWorkers.updateAllStocks();
chartWorkers.updateAllCharts();
},
{
timezone: "Europe/Berlin",
}
);
}
// database setup
app.get("/", (req, res) => {
res.statusCode = 200;
res.send(
"please visit <b> https://api.milou.io/api-docs/ </b> for documentation"
);
});
app.use(bodyParser.json());
app.use(cors());
app.use("/portfolio", getPortfolioRoute);
app.use("/stocks", getStocksRoute);
app.use("/analytics", getAnalyticsRoute);
module.exports = app;