-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
67 lines (54 loc) · 1.46 KB
/
index.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
import path from 'path'
import pino from 'pino'
import Fastify from 'fastify'
import fastifyStatic from '@fastify/static'
import child_process from 'child_process'
import { program } from 'commander'
import { fileURLToPath } from 'url'
import autocannonUiBackend from './packages/autocannon-ui-backend/index.js'
program
.option("-p, --port <port>", "listening port")
.option("-h, --host <host>", "host to bind", "localhost")
.parse();
const options = program.opts();
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const transport = pino.transport({
target: 'pino-pretty',
options: { colorize: true }
})
const logger = pino(
{
level: 'info',
base: null,
timestamp: false
},
transport
)
async function start() {
try {
const address = await startServer()
logger.info(`autocannon-ui started on ${address}.`)
} catch (e) {
logger.error(e)
}
}
async function startServer() {
const fastify = Fastify()
fastify.register(autocannonUiBackend)
const uiRoot = path.join(__dirname, 'packages/autocannon-ui-frontend/dist')
fastify.register(fastifyStatic, {
root: uiRoot
})
const address = await fastify.listen({port: options.port, host: options.host})
await fastify.ready()
const start =
process.platform == 'darwin'
? 'open'
: process.platform == 'win32'
? 'start'
: 'xdg-open'
child_process.exec(start + ' ' + address)
return address
}
start()