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" ;
10
1
import Fastify from "fastify" ;
2
+ import Autoload from "@fastify/autoload" ;
11
3
import pino from "pino" ;
12
4
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" ;
13
9
14
10
import * as dotenv from "dotenv" ;
15
11
dotenv . config ( ) ;
16
12
17
- import registerRoutes from "./routes/routes.js" ;
18
13
19
14
// logger
20
15
const stream = pretty ( {
21
16
translateTime : "SYS:HH:MM:ss Z" ,
22
17
messageFormat : "{msg} {req.method} {req.url}" ,
23
18
include : "time,pid,level" ,
24
19
hideObject : true ,
25
- colorize : false , // enable this will make Vercel log get:
26
- // [15:32:27 UTC] [32mINFO[39m (10): [36mincoming request GET /[39m
27
- //[15:32:27 UTC] [32mINFO[39m (10): [36mrequest completed [39m
20
+ colorize : false ,
28
21
} ) ;
29
22
const loggerInstance = pino ( { level : "info" } , stream ) ;
30
23
31
24
const app = Fastify ( { loggerInstance } ) ;
32
25
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
- } )
45
26
app . decorate ( "authenticate" , async ( request , reply ) => {
46
27
try {
47
28
await request . jwtVerify ( ) ;
@@ -50,50 +31,23 @@ app.decorate("authenticate", async (request, reply) => {
50
31
}
51
32
} ) ;
52
33
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 : / .* .n o - l o a d \. j s / ,
40
+ indexPattern : / ^ n o $ / i,
86
41
} ) ;
87
42
88
- /// routes
43
+ // routes
89
44
await registerRoutes ( app ) ;
90
45
46
+ // books routes
47
+ await registerBooksRoutes ( app ) ;
48
+
49
+ // start server
91
50
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
- */
97
51
const start = async ( ) => {
98
52
try {
99
53
await app . listen ( { port : 3000 } ) ;
0 commit comments