forked from snapshot-labs/snapshot-hub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
51 lines (48 loc) · 1.38 KB
/
index.ts
File metadata and controls
51 lines (48 loc) · 1.38 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
import dotenv from 'dotenv';
import bodyParser from 'body-parser';
import cors from 'cors';
import { graphqlHTTP } from 'express-graphql';
import rateLimit from 'express-rate-limit';
import { createHash } from 'crypto';
import express from 'express';
import api from './server';
import upload from './server/upload';
import { schema, rootValue } from './server/graphql';
import defaultQuery from './server/graphql/examples';
import { queryCountLimit, sendError } from './server/helpers/utils';
import './server/events';
dotenv.config();
const app = express();
app.use(bodyParser.json({ limit: '20mb' }));
app.use(bodyParser.urlencoded({ limit: '20mb', extended: false }));
app.use(cors({ maxAge: 86400 }));
app.set('trust proxy', 1);
app.use(
rateLimit({
windowMs: 10 * 1e3,
max: 32,
handler: (req, res) => {
const id = createHash('sha256')
.update(req.ip)
.digest('hex');
console.log('Too many requests', id.slice(0, 7));
sendError(res, 'too many requests', 429);
}
})
);
app.use('/api', api);
app.use('/api', upload);
app.use(
'/graphql',
graphqlHTTP({
schema,
rootValue,
graphiql: { defaultQuery },
validationRules: [queryCountLimit(5, 5)]
})
);
app.get('/*', (req, res) => res.redirect('/api'));
const PORT = process.env.PORT || 3000;
app.listen(PORT, () =>
console.log(`Snapshot hub started on: http://localhost:${PORT}`)
);