|
1 | 1 | import Fastify from "fastify"; |
2 | 2 | import cors from "@fastify/cors"; |
3 | 3 | import helmet from "@fastify/helmet"; |
4 | | -import rateLimit from "@fastify/rate-limit"; |
5 | 4 | import swagger from "@fastify/swagger"; |
| 5 | +import { getJobPosts } from "./features/jobs-moderation/job-mod-helpers"; |
6 | 6 |
|
7 | | -const fastify = Fastify({ |
8 | | - logger: { |
9 | | - transport: { |
10 | | - target: "pino-pretty", |
| 7 | +const fastify = Fastify({ logger: true }); |
| 8 | + |
| 9 | +const openApiConfig = { |
| 10 | + openapi: "3.0.0", |
| 11 | + info: { |
| 12 | + title: "Job Board API", |
| 13 | + version: "1.0.0", |
| 14 | + }, |
| 15 | + components: { |
| 16 | + schemas: { |
| 17 | + JobPost: { |
| 18 | + type: "object", |
| 19 | + required: ["tags", "description", "authorId", "message", "createdAt"], |
| 20 | + properties: { |
| 21 | + tags: { |
| 22 | + type: "array", |
| 23 | + items: { type: "string" }, |
| 24 | + }, |
| 25 | + description: { type: "string" }, |
| 26 | + authorId: { |
| 27 | + type: "string", |
| 28 | + format: "snowflake", |
| 29 | + }, |
| 30 | + message: { |
| 31 | + type: "object", |
| 32 | + description: "Discord Message object", |
| 33 | + }, |
| 34 | + createdAt: { |
| 35 | + type: "string", |
| 36 | + format: "date-time", |
| 37 | + }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + JobBoardCache: { |
| 41 | + type: "object", |
| 42 | + required: ["forHire", "hiring"], |
| 43 | + properties: { |
| 44 | + forHire: { |
| 45 | + type: "array", |
| 46 | + items: { $ref: "JobPost" }, |
| 47 | + }, |
| 48 | + hiring: { |
| 49 | + type: "array", |
| 50 | + items: { $ref: "JobPost" }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
11 | 54 | }, |
12 | 55 | }, |
13 | | -}); |
14 | | - |
15 | | -// Swagger documentation |
16 | | -const swaggerConfig = { |
17 | | - swagger: { |
18 | | - info: { |
19 | | - title: "API Documentation", |
20 | | - version: "1.0.0", |
| 56 | + paths: { |
| 57 | + "/jobs": { |
| 58 | + get: { |
| 59 | + tags: ["jobs"], |
| 60 | + summary: "Get all job posts", |
| 61 | + responses: { |
| 62 | + "200": { |
| 63 | + description: "Successful response", |
| 64 | + content: { |
| 65 | + "application/json": { |
| 66 | + schema: { |
| 67 | + $ref: "JobBoardCache", |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
21 | 74 | }, |
22 | 75 | }, |
23 | 76 | }; |
24 | 77 |
|
25 | | -// Register plugins |
26 | | -await fastify.register(cors); |
27 | | -await fastify.register(helmet); |
28 | | -await fastify.register(rateLimit, { |
29 | | - max: 100, |
30 | | - timeWindow: "1 minute", |
31 | | -}); |
32 | | -await fastify.register(swagger, swaggerConfig); |
33 | | - |
34 | | -// Example route with schema validation |
35 | | -// fastify.get( |
36 | | -// "/items", |
37 | | -// { |
38 | | -// schema: { |
39 | | -// response: { |
40 | | -// 200: { |
41 | | -// type: "array", |
42 | | -// items: { |
43 | | -// type: "object", |
44 | | -// properties: { |
45 | | -// id: { type: "string" }, |
46 | | -// name: { type: "string" }, |
47 | | -// }, |
48 | | -// }, |
49 | | -// }, |
50 | | -// }, |
51 | | -// }, |
52 | | -// }, |
53 | | -// async (request, reply) => { |
54 | | -// return [{ id: "1", name: "Item 1" }]; |
55 | | -// }, |
56 | | -// ); |
| 78 | +try { |
| 79 | + Object.entries(openApiConfig.components.schemas).forEach(([k, schema]) => { |
| 80 | + fastify.addSchema({ ...schema, $id: k }); |
| 81 | + }); |
| 82 | + // @ts-expect-error something's busted but it works |
| 83 | + await fastify.register(swagger, openApiConfig); |
| 84 | + await fastify.register(cors); |
| 85 | + await fastify.register(helmet); |
| 86 | +} catch (e) { |
| 87 | + console.log(e); |
| 88 | +} |
57 | 89 |
|
58 | 90 | fastify.get( |
59 | | - "/", |
| 91 | + "/jobs", |
60 | 92 | { |
61 | 93 | schema: { |
62 | 94 | response: { |
63 | 95 | 200: { |
64 | | - type: "object", |
65 | | - properties: { |
66 | | - hello: { type: "string" }, |
67 | | - }, |
| 96 | + $ref: "JobBoardCache", |
68 | 97 | }, |
69 | 98 | }, |
70 | 99 | }, |
71 | 100 | }, |
72 | 101 | async () => { |
73 | | - return { hello: "world" }; |
| 102 | + return getJobPosts(); |
74 | 103 | }, |
75 | 104 | ); |
76 | 105 |
|
77 | | -// Error handler |
78 | | -fastify.setErrorHandler((error, request, reply) => { |
79 | | - fastify.log.error(error); |
80 | | - reply.status(500).send({ error: "Internal Server Error" }); |
81 | | -}); |
82 | | - |
83 | | -try { |
84 | | - await fastify.listen({ port: 3000 }); |
85 | | -} catch (err) { |
86 | | - fastify.log.error(err); |
87 | | - process.exit(1); |
88 | | -} |
| 106 | +await fastify.listen({ port: 3000 }); |
0 commit comments