summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xstd/http/file_server.ts40
-rw-r--r--std/http/file_server_test.ts28
-rw-r--r--std/http/testdata/file_server_as_library.ts12
3 files changed, 60 insertions, 20 deletions
diff --git a/std/http/file_server.ts b/std/http/file_server.ts
index 84ca45692..7614c3ab2 100755
--- a/std/http/file_server.ts
+++ b/std/http/file_server.ts
@@ -34,10 +34,7 @@ interface FileServerArgs {
const encoder = new TextEncoder();
const serverArgs = parse(args) as FileServerArgs;
-
-const CORSEnabled = serverArgs.cors ? true : false;
const target = posix.resolve(serverArgs._[0] ?? "");
-const addr = `0.0.0.0:${serverArgs.port ?? serverArgs.p ?? 4507}`;
const MEDIA_TYPES: Record<string, string> = {
".md": "text/markdown",
@@ -60,23 +57,6 @@ function contentType(path: string): string | undefined {
return MEDIA_TYPES[extname(path)];
}
-if (serverArgs.h ?? serverArgs.help) {
- console.log(`Deno File Server
- Serves a local directory in HTTP.
-
-INSTALL:
- deno install --allow-net --allow-read https://deno.land/std/http/file_server.ts
-
-USAGE:
- file_server [path] [options]
-
-OPTIONS:
- -h, --help Prints help information
- -p, --port <PORT> Set port
- --cors Enable CORS via the "Access-Control-Allow-Origin" header`);
- exit();
-}
-
function modeToString(isDir: boolean, maybeMode: number | null): string {
const modeMap = ["---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"];
@@ -319,6 +299,26 @@ function html(strings: TemplateStringsArray, ...values: unknown[]): string {
}
function main(): void {
+ const CORSEnabled = serverArgs.cors ? true : false;
+ const addr = `0.0.0.0:${serverArgs.port ?? serverArgs.p ?? 4507}`;
+
+ if (serverArgs.h ?? serverArgs.help) {
+ console.log(`Deno File Server
+ Serves a local directory in HTTP.
+
+ INSTALL:
+ deno install --allow-net --allow-read https://deno.land/std/http/file_server.ts
+
+ USAGE:
+ file_server [path] [options]
+
+ OPTIONS:
+ -h, --help Prints help information
+ -p, --port <PORT> Set port
+ --cors Enable CORS via the "Access-Control-Allow-Origin" header`);
+ exit();
+ }
+
listenAndServe(
addr,
async (req): Promise<void> => {
diff --git a/std/http/file_server_test.ts b/std/http/file_server_test.ts
index 97c07c250..dbbaf81ff 100644
--- a/std/http/file_server_test.ts
+++ b/std/http/file_server_test.ts
@@ -38,6 +38,24 @@ async function startFileServer({
assert(s !== null && s.includes("server listening"));
}
+async function startFileServerAsLibrary({}: FileServerCfg = {}): Promise<void> {
+ fileServer = await Deno.run({
+ cmd: [
+ Deno.execPath(),
+ "run",
+ "--allow-read",
+ "--allow-net",
+ "http/testdata/file_server_as_library.ts",
+ ],
+ stdout: "piped",
+ stderr: "null",
+ });
+ assert(fileServer.stdout != null);
+ const r = new TextProtoReader(new BufReader(fileServer.stdout));
+ const s = await r.readLine();
+ assert(s !== null && s.includes("Server running..."));
+}
+
async function killFileServer(): Promise<void> {
fileServer.close();
// Process.close() kills the file server process. However this termination
@@ -166,3 +184,13 @@ test("contentType", async () => {
assertEquals(contentType, "text/html");
(response.body as Deno.File).close();
});
+
+test("file_server running as library", async function (): Promise<void> {
+ await startFileServerAsLibrary();
+ try {
+ const res = await fetch("http://localhost:8000");
+ assertEquals(res.status, 200);
+ } finally {
+ await killFileServer();
+ }
+});
diff --git a/std/http/testdata/file_server_as_library.ts b/std/http/testdata/file_server_as_library.ts
new file mode 100644
index 000000000..ab5fdcaab
--- /dev/null
+++ b/std/http/testdata/file_server_as_library.ts
@@ -0,0 +1,12 @@
+import { serve } from "../server.ts";
+import { serveFile } from "../file_server.ts";
+
+const server = serve({ port: 8000 });
+
+console.log('Server running...');
+
+for await (const req of server) {
+ serveFile(req, './http/testdata/hello.html').then(response => {
+ req.respond(response);
+ });
+} \ No newline at end of file