Skip to content

Commit 76a64c9

Browse files
committed
feat: split plugins and routes
1 parent eec385b commit 76a64c9

File tree

14 files changed

+263
-202
lines changed

14 files changed

+263
-202
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pnpm-lock.yaml
12
bun.lockb
23

34
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore

api/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import app from "../app.js";
22

3-
export default async (req, res) => {
3+
export default async (request, reply) => {
44
await app.ready();
5-
app.server.emit("request", req, res);
5+
app.server.emit("request", request, reply);
66
};

app.js

Lines changed: 18 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,28 @@
1-
import compress from "@fastify/compress";
2-
import cors from "@fastify/cors";
3-
import formbody from "@fastify/formbody";
4-
import helmet from "@fastify/helmet";
5-
import jwt from "@fastify/jwt";
6-
import postgres from "@fastify/postgres";
7-
import rateLimit from "@fastify/rate-limit";
8-
import swagger from "@fastify/swagger";
9-
import swaggerUI from "@fastify/swagger-ui";
101
import Fastify from "fastify";
2+
import Autoload from "@fastify/autoload";
113
import pino from "pino";
124
import pretty from "pino-pretty";
5+
import { dirname,join } from "node:path";
6+
import { fileURLToPath } from "node:url";
7+
import registerRoutes from "./routes/routes.js";
8+
import registerBooksRoutes from "./routes/booksRoutes.js";
139

1410
import * as dotenv from "dotenv";
1511
dotenv.config();
1612

17-
import registerRoutes from "./routes/routes.js";
1813

1914
// logger
2015
const stream = pretty({
2116
translateTime: "SYS:HH:MM:ss Z",
2217
messageFormat: "{msg} {req.method} {req.url}",
2318
include: "time,pid,level",
2419
hideObject: true,
25-
colorize: false, // enable this will make Vercel log get:
26-
// [15:32:27 UTC] INFO (10): incoming request GET /
27-
//[15:32:27 UTC] INFO (10): request completed 
20+
colorize: false,
2821
});
2922
const loggerInstance = pino({ level: "info" }, stream);
3023

3124
const app = Fastify({ loggerInstance });
3225

33-
/// plugins
34-
await app.register(compress, { encodings: ["gzip"] });
35-
36-
// security
37-
// not using RegExp or a function for origin
38-
// avoid DoS attacks https://github.com/fastify/fastify-cors#warning-dos-attacks
39-
await app.register(cors);
40-
await app.register(formbody);
41-
await app.register(helmet);
42-
await app.register(jwt, {
43-
secret: process.env.JWT_SECRET,
44-
})
4526
app.decorate("authenticate", async (request, reply) => {
4627
try {
4728
await request.jwtVerify();
@@ -50,50 +31,23 @@ app.decorate("authenticate", async (request, reply) => {
5031
}
5132
});
5233

53-
await app.register(rateLimit, {
54-
max: 100,
55-
timeWindow: "1 minute",
56-
});
57-
await app.register(swagger, {
58-
openapi: {
59-
info: {
60-
title: "tianheg's API",
61-
description: "Recording things in my life",
62-
version: "1.0.0",
63-
},
64-
// components: {
65-
// securitySchemes: {
66-
// bearerAuth: {
67-
// type: 'http',
68-
// scheme: 'bearer',
69-
// bearerFormat: 'JWT',
70-
// },
71-
// },
72-
// },
73-
},
74-
});
75-
await app.register(swaggerUI, {
76-
routePrefix: "/doc",
77-
uiConfig: {
78-
docExpansion: "list",
79-
deepLinking: false,
80-
},
81-
});
82-
83-
// database
84-
await app.register(postgres, {
85-
connectionString: process.env.POSTGRES_URL,
34+
const __filename = fileURLToPath(import.meta.url);
35+
const __dirname = dirname(__filename);
36+
await app.register(Autoload, {
37+
dir: join(__dirname, "plugins"),
38+
dirNameRoutePrefix: false,
39+
ignorePattern: /.*.no-load\.js/,
40+
indexPattern: /^no$/i,
8641
});
8742

88-
/// routes
43+
// routes
8944
await registerRoutes(app);
9045

46+
// books routes
47+
await registerBooksRoutes(app);
48+
49+
// start server
9150
if (process.env.NODE_ENV === "development") {
92-
/**
93-
* A function that asynchronously starts the application listening on port 3000.
94-
*
95-
* @return {Promise} A promise that resolves when the application starts listening successfully.
96-
*/
9751
const start = async () => {
9852
try {
9953
await app.listen({ port: 3000 });

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"@fastify/swagger-ui": "^5.0.1",
2424
"@fastify/under-pressure": "^9.0.1",
2525
"fastify": "^5.0.0",
26+
"fastify-plugin": "^5.0.1",
2627
"pg": "^8.13.0"
2728
},
2829
"scripts": {

plugins/compress.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import fp from "fastify-plugin";
2+
import compress from "@fastify/compress";
3+
4+
export default fp(async (app, opts) => {
5+
await app.register(compress, { encodings: ["gzip"] });
6+
});

plugins/cors.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import fp from "fastify-plugin";
2+
import cors from "@fastify/cors";
3+
4+
// not using RegExp or a function for origin
5+
// avoid DoS attacks https://github.com/fastify/fastify-cors#warning-dos-attacks
6+
7+
export default fp(async (app, opts) => {
8+
await app.register(cors);
9+
});

plugins/database.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import fp from "fastify-plugin";
2+
import pg from "@fastify/postgres";
3+
4+
import { config } from "dotenv";
5+
6+
config();
7+
8+
export default fp(async (app, opts) => {
9+
await app.register(pg, {
10+
connectionString: process.env.POSTGRES_URL,
11+
});
12+
})

plugins/formbody.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import fp from "fastify-plugin";
2+
import formbody from "@fastify/formbody";
3+
4+
export default fp(async (app, opts) => {
5+
await app.register(formbody);
6+
});

plugins/helmet.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import fp from "fastify-plugin";
2+
import helmet from "@fastify/helmet";
3+
4+
export default fp(async (app, opts) => {
5+
await app.register(helmet);
6+
});

plugins/jwt.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import fp from "fastify-plugin";
2+
import jwt from "@fastify/jwt";
3+
4+
export default fp(async (app, opts) => {
5+
await app.register(jwt, {
6+
secret: process.env.JWT_SECRET,
7+
});
8+
});

0 commit comments

Comments
 (0)