@@ -3,6 +3,16 @@ import Fastify from 'fastify';
33import registerMiddlewares from './middlewares' ;
44import registerRoutes from './routers' ;
55import { PrismaClient } from '@prisma/client' ;
6+ import fastifySwagger from '@fastify/swagger' ;
7+ import fastifySwaggerUi from '@fastify/swagger-ui' ;
8+
9+ const isProd = process . env . ENV ? ( process . env . ENV === 'PROD' ) : false ;
10+
11+ const host = process . env . HOST || 'localhost' ;
12+ const port = process . env . PORT ? Number ( process . env . PORT ) : 3000 ;
13+
14+ const domain = process . env . DOMAIN || host + ':' + port ;
15+
616
717// Load environment variables from .env
818dotenv . config ( ) ;
@@ -38,6 +48,31 @@ async function startServer() {
3848 // 1. Register middlewares
3949 registerMiddlewares ( fastify ) ;
4050
51+
52+ // 2. Register Swagger plugin (generates the OpenAPI JSON/YAML endpoints)
53+ fastify . register ( fastifySwagger as any , {
54+ swagger : {
55+ info : {
56+ title : 'My API' ,
57+ description : 'API Documentation generated with Fastify Swagger' ,
58+ version : '1.0.0'
59+ } ,
60+ externalDocs : {
61+ url : 'https://swagger.io' ,
62+ description : 'Find more info here'
63+ } ,
64+ host : domain ,
65+ schemes : isProd ? [ 'https' ] : [ 'http' ] ,
66+ consumes : [ 'application/json' ] ,
67+ produces : [ 'application/json' ]
68+ } ,
69+ exposeRoute : true ,
70+ } ) ;
71+
72+ fastify . register ( fastifySwaggerUi , {
73+ routePrefix : '/docs'
74+ } ) ;
75+
4176 // 2. Register routes
4277 registerRoutes ( fastify ) ;
4378
0 commit comments