A Docker-like CLI and HTTP API for managing headless VMs
1import machines from "./machines.ts";
2import images from "./images.ts";
3import volumes from "./volumes.ts";
4import { Hono } from "hono";
5import { logger } from "hono/logger";
6import { cors } from "hono/cors";
7import { bearerAuth } from "hono/bearer-auth";
8import { parseFlags } from "@cliffy/flags";
9
10export { images, machines, volumes };
11
12export default function () {
13 const token = Deno.env.get("VMX_API_TOKEN") || crypto.randomUUID();
14 const { flags } = parseFlags(Deno.args);
15
16 if (!Deno.env.get("VMX_API_TOKEN")) {
17 console.log(`Using API token: ${token}`);
18 } else {
19 console.log(
20 `Using provided API token from environment variable VMX_API_TOKEN`,
21 );
22 }
23
24 const app = new Hono();
25
26 app.use(logger());
27 app.use(cors());
28
29 app.use("/images/*", bearerAuth({ token }));
30 app.use("/machines/*", bearerAuth({ token }));
31 app.use("/volumes/*", bearerAuth({ token }));
32
33 app.route("/images", images);
34 app.route("/machines", machines);
35 app.route("/volumes", volumes);
36
37 const port = Number(
38 flags.port ||
39 flags.p ||
40 (Deno.env.get("VMX_API_PORT")
41 ? Number(Deno.env.get("VMX_API_PORT"))
42 : 8889),
43 );
44
45 Deno.serve({ port }, app.fetch);
46}