Skip to content

Commit

Permalink
feat: support for serving static files (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuapp authored Nov 29, 2024
1 parent 7852c30 commit 7462c47
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 52 deletions.
1 change: 1 addition & 0 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@std/cli": "jsr:@std/[email protected]",
"@std/fmt": "jsr:@std/fmt@^1.0.0",
"@std/fs": "jsr:@std/fs@^1.0.1",
"@std/http": "jsr:@std/http@^1.0.11",
"md4w": "npm:md4w@^0.2.6"
},
"exclude": [".gitattributes", ".github", "src/testdata", "**/*/*_test.ts"]
Expand Down
127 changes: 75 additions & 52 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions src/utils/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Hono } from "@hono/hono/tiny";
import { LinearRouter } from "@hono/hono/router/linear-router";
import { init as initMd4w, mdToHtml } from "md4w";
import type { MdToPdfOptions } from "../types.ts";
import { serveFile } from "@std/http/file-server";

export const DEFAULT_PORT = 33433;

Expand Down Expand Up @@ -36,5 +37,16 @@ export function launchHttpServer(
</html>`,
);
});
app.get("*", async (c) => {
const filePath = "." + decodeURI(c.req.path);
try {
const fileInfo = await Deno.lstat(filePath);
if (fileInfo.isFile) {
return serveFile(c.req.raw, filePath);
}
} catch (_e) {
return c.notFound();
}
});
return Deno.serve({ onListen: () => "", port: DEFAULT_PORT }, app.fetch);
}

0 comments on commit 7462c47

Please sign in to comment.