-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
64 lines (54 loc) · 1.63 KB
/
index.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
import "reflect-metadata";
import { ApolloServer } from "@autotelic/apollo-server-fastify";
import dotenv from "dotenv";
import fastify from "fastify";
import fastifyCookie from "fastify-cookie";
import fastifyCors from "fastify-cors";
import jwt from "fastify-jwt";
import fastifySession from "fastify-session";
import { FastifyContext } from "./types/Context";
import { connection } from "./utils/connection";
import { createSchema } from "./utils/createSchema";
import { sessionOptions } from "./utils/session";
dotenv.config();
const main = async () => {
const PORT: number = Number(process.env.PORT) || 4000;
await connection;
const schema = await createSchema();
const server = new ApolloServer({
schema,
context: ({ request, reply }: FastifyContext) => ({
request,
reply,
}),
});
const app = fastify({
logger: true,
trustProxy: process.env.NODE_ENV === "production",
});
app.register(server.createHandler({ cors: false }));
app.register(fastifyCors, {
origin:
process.env.NODE_ENV === "production" ? "example.com" : "localhost:4000",
credentials: true,
});
app.register(fastifyCookie);
app.register(jwt, {
secret: "boilerplate-secret",
cookie: { cookieName: "jid" },
});
app.register(fastifySession, sessionOptions);
app.listen(PORT, (err, address) => {
if (err) {
app.log.error(err.message);
process.exit(1);
}
app.log.info(
`🚀 Server launched on address ${address} in ${process.env.NODE_ENV?.toUpperCase()}`,
);
});
app.get("/", async (_, reply) => {
reply.redirect(302, "http://github.com/NickMandylas");
});
};
main();