-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
87 lines (78 loc) · 2.49 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"use strict";
import path from "path";
import AutoLoad from "@fastify/autoload";
import { fastifyTRPCPlugin } from "@trpc/server/adapters/fastify";
import { createContext } from "@/lib/trpc";
import { appRouter } from "@/routes";
import fastify from "fastify";
import cors from "@fastify/cors";
import { openApiDocument } from "@/lib/openapi";
import { fastifyTRPCOpenApiPlugin } from "trpc-openapi";
import fastifySwagger from "@fastify/swagger";
async function main() {
const server = fastify();
server.get("/ping", async (request, reply) => {
return "pong\n";
});
// await server.register(require("@fastify/static"), {
// root: path.join(__dirname, "public"),
// prefix: "/public/", // optional: default '/'
// // constraints: { host: 'example.com' } // optional: default {}
// });
await server.register(cors, {
// put your options here
});
// await server.register(AutoLoad, {
// dir: path.join(__dirname, "plugins"),
// // options: Object.assign({}, opts)
// });
// // This loads all plugins defined in routes
// // define your routes in one of these
// await server.register(AutoLoad, {
// dir: path.join(__dirname, "routes"),
// // options: Object.assign({}, opts)
// });
await server.register(fastifyTRPCPlugin, {
prefix: "/trpc",
useWss: false,
trpcOptions: { router: appRouter, createContext },
} as any);
await server.register(fastifyTRPCOpenApiPlugin, {
basePath: "/api",
router: appRouter,
createContext,
});
server.get("/openapi.json", () => openApiDocument);
// Server Swagger UI
// @ts-ignore
await server.register(fastifySwagger, {
// routePrefix: "/docs",
mode: "static",
specification: { document: openApiDocument },
// uiConfig: { displayOperationId: true },
// exposeRoute: true,
});
await server.register(require('@fastify/swagger-ui'), {
routePrefix: '/swagger',
uiConfig: {
docExpansion: 'full',
deepLinking: false
},
// uiHooks: {
// onRequest: function (request, reply, next) { next() },
// preHandler: function (request, reply, next) { next() }
// },
staticCSP: true,
// transformStaticCSP: (header) => header,
// transformSpecification: (swaggerObject, request, reply) => { return swaggerObject },
transformSpecificationClone: true
})
server.listen({ port: 3000 }, (err, address) => {
if (err) {
console.error(err);
process.exit(1);
}
console.log(`Server listening at ${address}`);
});
}
main();