-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
72 lines (61 loc) · 1.76 KB
/
app.js
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
65
66
67
68
69
70
71
72
import Fastify from "fastify";
import fastifyAutoload from "@fastify/autoload";
import { fileURLToPath } from 'url'
import { dirname, join } from 'path'
import { connect } from './database/database.js'
import UserService from "./services/userService.js";
import ArticleService from "./services/articleService.js";
import fastifyPlugin from "fastify-plugin";
import swagger from "./config/docs/swagger.js";
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const envLogger = {
developement: {
level: 'info',
transport: {
target: 'pino-pretty',
options: {
translateTime: 'HH:MM:ss Z',
ignore: 'pid,hostname',
},
},
},
production: true,
test: false,
}
const fastify = Fastify({
logger: envLogger['developement'] || true
})
//Autoload plugins from fastify ecosystem
fastify.register(fastifyAutoload, {
dir: join(__dirname, 'plugins')
})
async function decorateFunctions(fastify) {
const userService = new UserService(fastify)
fastify.decorate('userService', userService)
const articleService = new ArticleService(fastify)
fastify.decorate('articleService', articleService)
}
fastify.register(fastifyPlugin(decorateFunctions))
//Autoload services
fastify.register(fastifyAutoload, {
dir: join(__dirname, 'routes'),
options: { prefix: '/api/v1' }
})
//Swagger
fastify.register(swagger)
await fastify.ready()
const start = async (port) => {
try {
await fastify.listen({port})
await connect(fastify)
} catch(error) {
fastify.log.error(error)
process.exit(1)
}
}
fastify.ready(async (err) => {
if (err) console.log(err)
const port = fastify.config.PORT
start(port)
})