-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
77 lines (64 loc) · 2.12 KB
/
app.js
File metadata and controls
77 lines (64 loc) · 2.12 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
import './src/config/env.js'
import express from 'express'
import passport from './src/config/passport.js'
import userRoute from './src/routes/userRoute.js'
import oauthGoogleRoute from './src/routes/oauthGoogle.js'
import oauthOSMRoute from './src/routes/oauthOSM.js'
import apiRoute from './src/routes/apiRoute.js'
import countryRoute from './src/routes/countryRoute.js'
import layerRoute from './src/routes/layers.js'
import searchRoute from './src/routes/searchRoute.js'
import { rateLimit } from 'express-rate-limit'
import cookieParser from 'cookie-parser'
import cors from 'cors'
import { databaseErrorHandler, oauthErrorHandler, generalErrorHandler } from './src/routes/error-handlers.js'
// setup
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
// allow CORS for frontend
const allowedOrigin = process.env.NODE_ENV == 'development' ? process.env.FRONTEND_DEV_URL : process.env.FRONTEND_PROD_URL
const frontendUrl = process.env.NODE_ENV == 'development' ? process.env.FRONTEND_DEV_URL : process.env.FRONTEND_PROD_URL;
app.use(cors({
origin: allowedOrigin,
credentials: true
}));
// API rate limit
const apiLimiter = rateLimit({
windowMs: 15 * 60 * 1000,
limit: 50,
standardHeaders: true,
legacyHeaders: false,
ipv6Subnet: 56,
message: {
status: 'ERROR',
message: 'API rate limit exceeded'
}
});
app.use('api/v1/', apiLimiter);
// Initialize Passport
app.use(passport.initialize());
// endpoints
app.use('/auth/google', oauthGoogleRoute)
app.use('/auth/osm', oauthOSMRoute)
app.use('/user', userRoute)
app.use('/layer', layerRoute)
app.use('/country', countryRoute)
app.use('/search', searchRoute)
// health check
app.get('/health', (req, res) => res.sendStatus(200));
// API
app.use('/api/v1/', apiRoute)
// error handlers
app.use(oauthErrorHandler);
app.use(databaseErrorHandler);
app.use(generalErrorHandler);
// start server (only if not in test environment)
if (process.env.NODE_ENV !== 'test') {
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
}
export default app;