-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
58 lines (51 loc) · 1.28 KB
/
server.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
import 'dotenv-safe/config';
import express from 'express';
import rateLimit from 'express-rate-limit';
import cors from 'cors';
import colors from 'colors';
import { ApolloServer } from 'apollo-server-express';
import { ApolloGateway } from '@apollo/gateway';
import { users } from './server/services';
const initGateway = async (): Promise<void> => {
const PORT = process.env.PORT || 5000;
const app = express();
app.set('trust proxy', 1);
app.use(
rateLimit({
windowMs: 1 * 60 * 1000,
max: 100
})
);
app.use(
cors({
credentials: true,
origin:
process.env.NODE_ENV === 'production'
? 'prod url'
: 'http://localhost:3000'
})
);
const gateway = new ApolloGateway({
serviceList: [{ name: 'user', url: await users.startService(5001) }]
});
const apolloServer = new ApolloServer({
gateway,
subscriptions: false,
playground: true
});
apolloServer.applyMiddleware({
app,
cors: false,
path: '/graphql'
});
app.listen({ port: PORT }, (): void => {
console.log(
colors.cyan(
`\n🚀 Gateway is now running on http://localhost:${PORT}/graphql`
)
);
});
};
initGateway().catch(err =>
console.error(colors.red(`Error starting server ${err.message}`))
);