-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
85 lines (68 loc) · 2.79 KB
/
app.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
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
import express, { Express, Request, Response } from "express";
import path from 'path';
import fs from 'fs';
import YAML from 'yaml';
import * as url from 'url';
import dotenv from 'dotenv';
import cors from 'cors';
import helmet from "helmet";
import logger from 'morgan';
import rfs from 'rotating-file-stream';
import swaggerUi from 'swagger-ui-express';
import { errorHandler, notFound } from "./src/middlewares/error";
import { S3Router } from './src/routes/s3';
import { CognitoRouter } from "./src/routes/cognito";
import { validateAuth } from "./src/middlewares/auth";
import { setUserId } from "./src/middlewares/setUserId";
import { setSSEkey } from "./src/middlewares/setSSEkey";
dotenv.config();
const __filename = url.fileURLToPath(import.meta.url);
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
const app: Express = express()
const port = process.env.PROD_PORT || process.env.PORT || 3000;
// serve static files from the public directory
app.use(express.static(path.join(__dirname, 'public')));
// Enable all cors requests
// app.use(cors());
app.all('*', (req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Headers', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DELETE');
next();
});
app.options('*', (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Headers', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DELETE');
res.sendStatus(200);
});
// Secure the app by setting various HTTP headers
app.use(helmet());
// Logger middleware to console and to single file with 1d rotation
const loggerPath = path.join(__dirname, 'logs/server.log');
if (fs.existsSync(loggerPath)) {
fs.unlinkSync(loggerPath);
}
const serverLogStream = rfs.createStream('server.log', { maxFiles: 5, size: '2M', path: path.join(__dirname, 'logs') });
app.use(logger('combined'));
app.use(logger('combined', { stream: serverLogStream }));
// used to parse the incoming requests with JSON payloads and is based upon the bodyparser
app.use(express.json());
// Swagger configuration
const file = fs.readFileSync('./docs/swagger.yaml', 'utf8');
const swaggerDocument = YAML.parse(file);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
// Static website
app.get('/', (req: Request, res: Response) => {
res.sendFile(__dirname + '/public/index.html');
})
// Auth middleware to check if user token is valid before protected apis
app.all('/api/*', validateAuth, setUserId, setSSEkey)
app.use('/api/s3', S3Router)
app.use('/api/cognito', CognitoRouter)
// error middlewares
app.use(notFound);
app.use(errorHandler);
app.listen(port, async () => {
console.log(`⚡️[server]: Server is running at http://localhost:${port}`)
})