-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathapp.ts
69 lines (51 loc) · 1.46 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
import express, { NextFunction, Request, Response } from 'express';
import graphqlHTTP from 'express-graphql';
import {schema} from './lib/gql/schema';
import {db} from './models'
import session from 'express-session';
import rateLimit from 'express-rate-limit';
import cors from 'cors';
const app = express();
const port = 3000;
app.use(cors());
var user_id = 2;
async function GetCurrentUser(req: any, res: Response, next: NextFunction) {
// No need for a real login system for this demo API.
// Just assign a user to each session on first visit...
if (!req.session.user_id) {
req.session.user_id = user_id;
console.log("Assigned user ID", user_id);
if (user_id == 50){
user_id = 2;
}
else {
user_id++;
}
}
let user = await db.User.findByPk(req.session.user_id);
req.user = user;
next();
}
app.use(session(
{
secret: "a very good and secure secret",
resave: false,
saveUninitialized: false
}
));
app.use(GetCurrentUser);
// Set up rate-limiting.
// We wouldn't want anyone brute-forcing password reset tokens!
const limiter = rateLimit({
windowMs:60 * 1000, // one minute
max: 100 // limit to 100 requests/minute
});
app.use(limiter);
app.get('/', (_req,res) => {
return res.redirect('/graphql');
})
app.use('/graphql', graphqlHTTP({
schema: schema,
graphiql: true
}))
app.listen(port, () => console.log("API started."));