-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
51 lines (41 loc) · 1.38 KB
/
index.ts
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
/* eslint-disable no-console */
import cors from 'cors';
import dayjs from 'dayjs';
import customParseFormat from 'dayjs/plugin/customParseFormat';
import utc from 'dayjs/plugin/utc';
import dotenv from 'dotenv';
import express from 'express';
import mongoose from 'mongoose';
import schedule from 'node-schedule';
import errorMiddleware from './middlewares/errorMiddleware';
import currencyRouter from './routes/currencyRouter';
import exchangeRouter from './routes/exchangeRouter';
import CurrencyService from './services/CurrencyService';
import ExchangeService from './services/ExchangeService';
import swaggerDocs from './utils/swagger';
dayjs.extend(utc);
dayjs.extend(customParseFormat);
dotenv.config();
const port = Number(process.env.PORT) || 3000;
const app = express();
app.use(cors());
app.use('/', exchangeRouter);
app.use('/', currencyRouter);
app.use(errorMiddleware);
const startApp = async () => {
try {
const dbUrl = process.env.MONGO_CONNECT_URI;
if (!dbUrl) {
throw new Error('MONGO_CONNECT_URI is empty');
}
await mongoose.connect(dbUrl);
await CurrencyService.fetch();
app.listen(port, () => console.log(`Server started on port ${port}`));
swaggerDocs(app, port);
const cronRule = process.env.CRON_RULE || '5 * * * *';
schedule.scheduleJob(cronRule, ExchangeService.fetchCurrentRates);
} catch (e) {
console.log(e);
}
};
startApp();