From 674fe965550444db87edc7937ff6932e1a918d9d Mon Sep 17 00:00:00 2001 From: nsfisis Date: Fri, 27 Jun 2025 23:39:31 +0900 Subject: feat(meta): rename vhosts/ directory to services/ --- services/blog/nuldoc-src/commands/serve.ts | 45 ++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 services/blog/nuldoc-src/commands/serve.ts (limited to 'services/blog/nuldoc-src/commands/serve.ts') diff --git a/services/blog/nuldoc-src/commands/serve.ts b/services/blog/nuldoc-src/commands/serve.ts new file mode 100644 index 00000000..e944aaf0 --- /dev/null +++ b/services/blog/nuldoc-src/commands/serve.ts @@ -0,0 +1,45 @@ +import { serveDir, STATUS_CODE, STATUS_TEXT } from "@std/http"; +import { join } from "@std/path"; +import { Config } from "../config.ts"; +import { runBuildCommand } from "./build.ts"; + +function isResourcePath(pathname: string): boolean { + const EXTENSIONS = [ + ".css", + ".gif", + ".ico", + ".jpeg", + ".jpg", + ".js", + ".png", + ".svg", + ]; + return EXTENSIONS.some((ext) => pathname.endsWith(ext)); +} + +export function runServeCommand(config: Config) { + const rootDir = join(Deno.cwd(), config.locations.destDir); + Deno.serve({ hostname: "127.0.0.1" }, async (req) => { + const pathname = new URL(req.url).pathname; + if (!isResourcePath(pathname)) { + await runBuildCommand(config); + console.log("rebuild"); + } + const res = await serveDir(req, { + fsRoot: rootDir, + showIndex: true, + }); + if (res.status !== STATUS_CODE.NotFound) { + return res; + } + + const notFoundHtml = await Deno.readTextFile(join(rootDir, "404.html")); + return new Response(notFoundHtml, { + status: STATUS_CODE.NotFound, + statusText: STATUS_TEXT[STATUS_CODE.NotFound], + headers: { + "content-type": "text/html", + }, + }); + }); +} -- cgit v1.2.3-70-g09d2