const async_build_js_script = async function (path_js_entry_script, build_options) { const result = await Bun.build({ entrypoints: [ path_js_entry_script ], env: 'disable', minify: false, ...build_options, }) const str_out = await result.outputs[0].text() return str_out } const get_siteroot_html = function ({ page_title }) {return ` ${page_title || 'Page Title'} `.trim()} const start_server = function ({ config, jsbuild_app_frontend }) { const server = Bun.serve({ hostname: config.host || '127.0.0.1', port : config.port || 8800, async fetch (req, server) { const url = new URL(req.url) let resp if (url.pathname === '/') { resp = new Response(get_siteroot_html({ page_title: config.page_title }), { status: 200, headers: { 'Content-Type': 'text/html' } }) } else if (url.pathname === '/index.js') { const str_js = await jsbuild_app_frontend() resp = new Response( await async_build_js_script(import.meta.dir + '/../frontend/index.js', { define: { JS_APP_FRONTEND: JSON.stringify(str_js), } }), { status: 200, headers: { 'Content-Type': 'text/javascript' } } ) } else if (url.pathname === '/ws') { if (server.upgrade(req)) { // do not return a Response if upgrade fails resp = undefined } resp = new Response('Upgrade failed', { status: 500 }) } else { resp = new Response(null, { status: 404 }) } return resp }, websocket: { message: function (ws, message) {}, }, }) return server } const async_run = async function ({ config, jsbuild_app_frontend, }) { const server = start_server({ config, jsbuild_app_frontend }) console.info(server) process.on('SIGINT', () => { console.log('SIGINT intercepted') process.exit() }) return } export { async_run }