import { Elysia } from "elysia"; import { html } from "@elysiajs/html"; import { staticPlugin } from "@elysiajs/static"; import path from "path"; import fs from "fs/promises"; import { renderTemplate } from "./util"; import { recentPosts } from "./recent-posts"; const app = new Elysia() .use( staticPlugin({ assets: "./data/style", prefix: "/style", }), ) .use( staticPlugin({ assets: "./data/public", prefix: "/", }), ) .use(html()) .get("/", async ({ set }) => { try { const content = await fs.readFile( path.join(process.cwd(), "data/pages/about.html"), "utf8", ); return renderTemplate("index.html", { content }); } catch (error) { console.error("Error serving main page: ", error); set.status = 500; set.headers["Content-Type"] = "text/plain"; return "Internal server error"; } }) .get("/projects", async ({ set }) => { try { const content = await fs.readFile( path.join(process.cwd(), "data/pages/projects.html"), "utf8", ); return renderTemplate("index.html", { content }); } catch (error) { console.error("Error serving main page: ", error); set.status = 500; set.headers["Content-Type"] = "text/plain"; return "Internal server error"; } }) .get("/posts/:post_name", async ({ params, set }) => { try { const filePath = path.join( process.cwd(), "data/posts", `${params.post_name}.html`, ); const fileContent = await fs.readFile(filePath, "utf8"); return renderTemplate("index.html", { content: fileContent }); } catch (error) { console.error(`Error serving post ${params.post_name}: `, error); set.status = 404; set.headers["Content-Type"] = "text/plain"; return "Post not found."; } }) .use(recentPosts); const port = Number(process.env.PORT || 3000); app.listen(port, () => { console.log(`🦊 Elysia is running at ${app.server?.hostname}:${port}`); });