-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
11 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,24 @@ | ||
import { Handler } from "https://deno.land/[email protected]/http/server.ts"; | ||
import { serveDir } from "https://deno.land/[email protected]/http/file_server.ts"; | ||
import { normalize as normalise } from "https://deno.land/[email protected]/path/posix.ts"; | ||
|
||
interface ServerOptions { | ||
base?: string; | ||
base_path: string; | ||
build_dir: string; | ||
} | ||
|
||
export const create_handler = ( | ||
{ base = "", build_dir }: ServerOptions, | ||
{ base_path, build_dir }: ServerOptions, | ||
): Handler => ((req) => { | ||
const url = new URL(req.url); | ||
|
||
if (url.pathname.startsWith("/" + base)) { | ||
return serveDir(req, { fsRoot: build_dir, urlRoot: base }); | ||
const normalised_base_path = normalise("/" + base_path + "/"); | ||
|
||
if (url.pathname.startsWith(normalised_base_path)) { | ||
return serveDir(req, { fsRoot: build_dir, urlRoot: base_path }); | ||
} else { | ||
return Response.redirect(new URL(base + url.pathname, url.origin)); | ||
return Response.redirect( | ||
new URL(normalise(normalised_base_path + url.pathname), url.origin), | ||
); | ||
} | ||
}); |