-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
49 lines (42 loc) · 1.15 KB
/
main.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
import fs from 'fs'
import dotenv from 'dotenv'
import winston from 'winston'
import libp2pServer from './src/server.js'
import httpServer, { httpRouter } from './src/http.js'
winston.configure({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'p2p-instance' },
transports: [
new winston.transports.Console({
level: 'info',
format: winston.format.simple()
}),
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' })
]
})
if (!fs.existsSync('./.env')) {
logger.error('Please setup .env file before launch')
process.exit(1)
}
dotenv.config()
async function start() {
const p2pNode = await libp2pServer()
if (process.env.HTTP_PORT) {
httpServer.use((req, res, next) => {
req.p2pNode = p2pNode
req.logger = winston
next()
})
httpServer.use(httpRouter)
httpServer.listen(process.env.HTTP_PORT, () => {
winston.info(`App llistening on port ${process.env.HTTP_PORT}`)
})
} else {
winston.warn(
'HTTP port not specified, starting instance without http server'
)
}
}
start()