forked from hahnavi/telbot-otp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
77 lines (62 loc) · 1.75 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
77
const path = require('path')
require('dotenv').config({
path: path.join(__dirname, '/.env')
})
const express = require('express')
const http = require('http')
const bot = require('./bot')
const { connectDb } = require('./models')
const app = express()
app.use(express.json())
app.use('/user', require('./routes/user'))
app.use('/otp', require('./routes/otp'))
const swaggerUi = require('swagger-ui-express')
const swaggerDocument = require('./swagger.json')
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument))
const normalizePort = (val) => {
const port = parseInt(val, 10)
if (Number.isNaN(port)) {
return val
}
if (port >= 0) {
return port
}
return false
}
const port = normalizePort(process.env.PORT || '3000')
app.set('port', port)
const onError = (error) => {
if (error.syscall !== 'listen') {
throw error
}
const bind = typeof port === 'string'
? `Pipe ${port}`
: `Port ${port}`
if (error.code === 'EACCES') {
console.error(`${bind} requires elevated privileges`)
process.exit(1)
} else if (error.code === 'EADDRINUSE') {
console.error(`${bind} is already in use`)
process.exit(1)
} else {
throw error
}
}
const server = http.createServer(app)
const onListening = () => {
const addr = server.address()
console.log(`Listening on http://${addr.address}:${addr.port}`)
console.log(`API Documentation at http://${addr.address}:${addr.port}/api-docs`)
bot.launch()
console.log('Bot is launched')
}
server.on('error', onError)
server.on('listening', onListening)
connectDb()
.then(async () => {
console.log('Connected to database')
server.listen(port, process.env.HOST || '0.0.0.0')
})
.catch((error) => {
console.log(`Database error: ${error.message}`)
})