import { getImages, getImage, saveImage } from "./routes";

const server = Bun.serve({
  port: 8080,
  async fetch(req) {
    const path = new URL(req.url).pathname;

    // send back the image paths uploaded
    if (path === "/images") {
      return await getImages(req);
    }

    // send back the image that was uploaded
    if (path.split("/")[1] === "image") {
      return await getImage(req);
    }

    // receive image upload fromt the client
    if (req.method === "POST" && path === "/upload") {
      return await saveImage(req);
    }

    // default to sending back the web app
    return new Response(Bun.file("./index.html"));
  },
});

console.log(`Listening on ${server.url}`);