OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-booking / index.js

68 lines · javascript ref:
#!/usr/bin/env bare

/**
 * hyper-booking — P2P booking platform
 *
 * Phase 1: Core booking + auth
 */

await import('bare-node-runtime/global')

import fs from 'bare-fs'
import path from 'bare-path'
import { HyperBookingServer } from './lib/server.js'

// Version
let version = '0.1.0'
{
  const appDir = path.dirname(new URL(import.meta.url).pathname)
  try {
    const pkg = JSON.parse(fs.readFileSync(path.join(appDir, 'package.json'), 'utf-8'))
    version = pkg.version || version
  } catch {}
}

process.on('uncaughtException', (err) => {
  console.error('[FATAL] Uncaught exception:', err.message, err.stack)
})
process.on('unhandledRejection', (reason) => {
  console.error('[WARN] Unhandled rejection:', reason && reason.message ? reason.message : reason)
})

// Parse args
const rawArgs = process.argv.slice(2)
const args = rawArgs[0] === '--' ? rawArgs.slice(1) : rawArgs
const command = args[0] || 'serve'

if (command === 'serve') {
  let port = 4050
  let storage = null
  let network = false
  let prefix = ''
  let host = 'localhost'

  for (const arg of args.slice(1)) {
    if (arg.startsWith('--port=')) port = parseInt(arg.split('=')[1])
    else if (arg.startsWith('--storage=')) storage = arg.split('=')[1]
    else if (arg === '--network') { network = true; host = '0.0.0.0' }
    else if (arg.startsWith('--prefix=')) prefix = arg.split('=')[1]
    else if (/^\d+$/.test(arg)) port = parseInt(arg)
  }

  const server = new HyperBookingServer({
    port,
    host,
    storage,
    pathPrefix: prefix,
    network
  })

  await server.ready()
  await server.start()

  console.log(`[hyper-booking] v${version} listening on http://${host}:${port}${prefix}`)
} else {
  console.log(`Unknown command: ${command}`)
  console.log('Usage: bare index.js serve [port] [--storage=path] [--network] [--prefix=/path]')
}