-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
64 lines (55 loc) · 1.23 KB
/
Copy pathserver.js
File metadata and controls
64 lines (55 loc) · 1.23 KB
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
import Fastify from 'fastify';
import fastifyMongodb from '@fastify/mongodb';
import swagger from '@fastify/swagger';
import swaggerUI from '@fastify/swagger-ui';
import userRouter from './src/routes/user.js';
const fastify = Fastify({
logger: true,
});
/**
* Swagger (OpenAPI) setup
*/
await fastify.register(swagger, {
openapi: {
info: {
title: 'Learning Fastify API',
description: 'Fastify + MongoDB learning project',
version: '1.0.0',
},
},
});
await fastify.register(swaggerUI, {
routePrefix: '/docs',
});
/**
* MongoDB connection
*/
fastify.register(fastifyMongodb, {
forceClose: true,
url: process.env.MONGODB_URI || 'mongodb://localhost:27017/learning_fastify',
});
/**
* Root route
*/
fastify.get('/', async () => {
return { message: 'Welcome To Auth Service!' };
});
/**
* Register routes
*/
fastify.register(userRouter);
/**
* Start server
*/
const startServer = async () => {
const PORT = process.env.PORT || 3000;
try {
await fastify.listen({ port: PORT });
fastify.log.info(`Server running on port ${PORT}`);
fastify.log.info(`Swagger docs at http://localhost:${PORT}/docs`);
} catch (err) {
fastify.log.error(err);
process.exit(1);
}
};
startServer();