forked from AntonLukichev/node-imgoptimize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·76 lines (69 loc) · 2.1 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
73
74
75
76
#!/usr/bin/env nodejs
const routes = require('./routes')
const CONFIG = require('./config')
const swagger = require('./config/swagger')
const mainController = require('./controllers/mainController')
const fastify = require('fastify')({ logger: { level: CONFIG.logLevel } })
if (CONFIG.sentryDsn) {
const Sentry = require('@sentry/node')
let sentryDebug = false
if (process.env.NODE_ENV === 'development') sentryDebug = true
Sentry.init({
dsn: CONFIG.sentryDsn,
release: `${process.env.npm_package_name}@${process.env.npm_package_version}`,
debug: sentryDebug,
serverName: process.env.COMPUTERNAME
})
fastify.setErrorHandler((err, req, reply) => {
Sentry.withScope(scope => {
scope.setUser({
ip_address: req.raw.ip
})
scope.setTag('path', req.raw.url)
scope.addEventProcessor(event => Sentry.Handlers.parseRequest(event, req))
Sentry.captureException(err)
})
})
}
// fastify.register(require('fastify-response-time')) error fastify-response-time\index.js:60 Cannot convert undefined or null to object
fastify.register(require('fastify-favicon'))
fastify.register(require('fastify-static'), { root: __dirname })
fastify.register(require('fastify-swagger'), swagger.options)
const startCheck = () => {
try {
mainController.createFolder(CONFIG.sourceFolder)
mainController.createFolder(CONFIG.destinationFolder)
} catch (e) {
fastify.log.error('can\'t create folder from config', e)
process.exit(1)
}
}
startCheck()
routes.forEach((route) => {
fastify.route(route)
})
process.on('SIGINT', async () => {
console.log('stopping fastify server')
await fastify.close()
process.exit(0)
})
fastify.ready(err => {
if (err) throw err
fastify.swagger()
})
const start = async () => {
try {
await fastify.listen(process.env.PORT || CONFIG.httpPort, CONFIG.httpHost, (err, address) => {
if (err) {
console.error(err)
} else {
console.log(`Server listening on ${address}`)
}
})
} catch (err) {
console.log('Error starting server:', err)
process.exit(1)
}
}
start()
module.exports = fastify