From 57315c52be96d2a2c013f0cfb0de5429980e301a Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 2 Nov 2025 17:49:34 +0900 Subject: refactor(blog): rename directory, services/{blog => nuldoc}/ --- services/nuldoc/nuldoc-src/commands/serve.ts | 52 ++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 services/nuldoc/nuldoc-src/commands/serve.ts (limited to 'services/nuldoc/nuldoc-src/commands/serve.ts') diff --git a/services/nuldoc/nuldoc-src/commands/serve.ts b/services/nuldoc/nuldoc-src/commands/serve.ts new file mode 100644 index 0000000..6b7d8a0 --- /dev/null +++ b/services/nuldoc/nuldoc-src/commands/serve.ts @@ -0,0 +1,52 @@ +import { parseArgs } from "@std/cli"; +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", + ".mjs", + ".png", + ".svg", + ]; + return EXTENSIONS.some((ext) => pathname.endsWith(ext)); +} + +export function runServeCommand(config: Config) { + const parsedArgs = parseArgs(Deno.args, { + boolean: ["no-rebuild"], + }); + + const doRebuild = !parsedArgs["no-rebuild"]; + 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) && doRebuild) { + 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