-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·64 lines (53 loc) · 1.88 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
const express = require('express');
const cron = require('node-cron');
const bodyParser = require('body-parser');
const helmet = require("helmet");
const morgan = require("morgan");
const fs = require("fs");
const path = require('path');
const checkJwt = require('./auth/jwt_auth')
const sequalize = require('./utils/database')
const accountRoutes = require('./routes/account')
const assetsRoutes = require('./routes/assets')
const Balance = require('./models/balances')
const Coin = require('./models/coins')
const Portfolio = require('./models/portfolios')
const Transaction = require('./models/transactions')
const coingecko_handler = require('./utils/coingecko_handler/coingecko_handler')
const app = express();
// app.use(bodyParser.urlencoded()); // x-www-form-urlencoded <form>
app.use(bodyParser.json()); // application/json
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET, POST, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
next();
});
const accessLogStream = fs.createWriteStream(path.join(__dirname, 'access.log'), { flags: 'a' })
app.use(helmet())
app.use(morgan('combined', { stream: accessLogStream }));
app.use(checkJwt)
app.use('/account', accountRoutes);
app.use('/assets/', assetsRoutes);
app.use(function (err, req, res, next) {
if (err.name === 'UnauthorizedError') {
res.status(err.status).send(err.inner);
}
else {
next(err)
}
});
sync_db()
async function sync_db() {
try {
await sequalize.sync()
console.log("Database synchronized successfully")
coingecko_handler.handle_coins()
cron.schedule('* * * * *', () => {
coingecko_handler.handle_coins()
});
app.listen(process.env.PORT);
} catch {
console.log(err)
}
}