/** * Copyright 2026 aphrody-code * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * `bxc api` — serve the BXC HTTP API locally (routes: `src/server/app.ts`). */ import { createBxcApp } from "../server/app.ts"; import { type CommonOptions, parseCommonArgs } from "./shared.ts"; // --------------------------------------------------------------------------- // Options // --------------------------------------------------------------------------- interface ApiServerOptions extends CommonOptions { port: number; hostname: string; authToken: string | null; corsOrigin: string; cacheEnabled: boolean; cacheTtlMs: number; cacheMax: number; } const DEFAULTS: Omit = { port: 8787, hostname: "0.0.0.0", authToken: null, corsOrigin: "*", cacheEnabled: true, cacheTtlMs: 60_000, cacheMax: 256, }; // --------------------------------------------------------------------------- // Server // --------------------------------------------------------------------------- /** * Start the BXC HTTP API (`src/server/app.ts`) with bearer auth and CORS * options. GraphQL and Swagger stay enabled; installer downloads are not * served from a local API. */ export async function startApiServer(options: ApiServerOptions): Promise<{ port: number; stop: () => void; }> { const app = await createBxcApp({ authToken: options.authToken, corsOrigin: options.corsOrigin, downloads: false, }); app.listen({ port: options.port, hostname: options.hostname }); return { port: app.server?.port ?? options.port, stop: () => void app.stop(true), }; } // --------------------------------------------------------------------------- // CLI // --------------------------------------------------------------------------- function printUsage(): void { Bun.stdout.write( `bxc api — turn any website into a JSON API Usage: bxc api [options] Options: --port listen port (default 8787) --host listen hostname (default 0.0.0.0) --auth require Authorization: Bearer --help, -h print this help `, ); } export async function main( argv: readonly string[], baseOpts: CommonOptions, ): Promise { const opts: ApiServerOptions = { ...baseOpts, ...DEFAULTS }; for (let i = 0; i < argv.length; i++) { const a = argv[i]; switch (a) { case "--port": opts.port = parseInt(argv[++i], 10); break; case "--host": opts.hostname = argv[++i]; break; case "--auth": opts.authToken = argv[++i]; break; case "--help": case "-h": printUsage(); return; } } await startApiServer(opts); } if (import.meta.main) { const { opts, remaining } = parseCommonArgs(process.argv.slice(2)); main(remaining, opts).catch((err) => { console.error(err); process.exit(1); }); }