Skip to content

Commit

Permalink
Fixed static file serving
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsVomMars committed Mar 5, 2021
1 parent b8448cc commit 6bfe4de
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 14 deletions.
29 changes: 22 additions & 7 deletions app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import {
HTTPOptions,
join,
log,
sep,
serve,
Server,
} from "./deps.ts";
import { getNestedDirectories } from "./util.ts";

export type Handler = (c: Context) => Promise<void> | void;
export type Middleware = (next: Handler) => Handler;
Expand Down Expand Up @@ -176,18 +178,31 @@ export class Application {
directory: string,
...middlewares: Middleware[]
): Application {
// TODO: Fallback handler if file does not exist
if (!path.endsWith("/")) path += "/";
const hdl = async (c: Context) => {
const file = c.path.substring(path.length);
if (file.length === 0) return await c.file(join(directory, "index.html"));
return await c.file(join(directory, file));
};
this.addPath(`${path}*`, Method.GET, hdl, ...middlewares);
(async () => {
const directories = await getNestedDirectories(directory);
directories.push("");
for (const dir of directories) {
const hdl = async (c: Context) => {
const file = c.path.substring(join(path, dir, sep).length);
if (file.length === 0) {
return await c.file(join(directory, dir, "index.html"));
}
return await c.file(join(directory, dir, file));
};
this.addPath(
`${path + dir}/*`.replace(/\/+/, "/"),
Method.GET,
hdl,
...middlewares,
);
}
})();
return this;
}
}

// Logging
await log.setup({
handlers: {
timeHandler: new log.handlers.ConsoleHandler("INFO", {
Expand Down
12 changes: 6 additions & 6 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ export {
ServerRequest,
setCookie,
Status,
} from "https://deno.land/std@0.88.0/http/mod.ts";
} from "https://deno.land/std@0.89.0/http/mod.ts";

export type {
Cookie,
HTTPOptions,
Response,
} from "https://deno.land/std@0.88.0/http/mod.ts";
} from "https://deno.land/std@0.89.0/http/mod.ts";

export { MultipartReader } from "https://deno.land/std@0.88.0/mime/mod.ts";
export { MultipartReader } from "https://deno.land/std@0.89.0/mime/mod.ts";

export * as log from "https://deno.land/std@0.88.0/log/mod.ts";
export * as log from "https://deno.land/std@0.89.0/log/mod.ts";

export { format } from "https://deno.land/std@0.88.0/datetime/mod.ts";
export { format } from "https://deno.land/std@0.89.0/datetime/mod.ts";

export { extname, join } from "https://deno.land/std@0.88.0/path/mod.ts";
export { extname, join, sep } from "https://deno.land/std@0.89.0/path/mod.ts";
2 changes: 1 addition & 1 deletion router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class Node {
}

export class PathHandler {
private name?: string;
private readonly name?: string;
constructor(
private readonly path: string,
private readonly method: Method,
Expand Down
15 changes: 15 additions & 0 deletions util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { join } from "./deps.ts";

export async function getNestedDirectories(dirname: string): Promise<string[]> {
let items: string[] = [];
for await (const item of Deno.readDir(dirname)) {
if (item.isDirectory) {
items.push(item.name);
const jp = join(dirname, item.name);
let dirs = await getNestedDirectories(jp);
dirs = dirs.map((subdir) => join(item.name, subdir));
items = items.concat(dirs);
}
}
return items;
}

0 comments on commit 6bfe4de

Please sign in to comment.