Skip to content

Commit

Permalink
Revert "Redesign of http server module (#188)"
Browse files Browse the repository at this point in the history
We need to consider the API changes here more carefully.

This reverts commit da188a7.
and commit 8569f15.
  • Loading branch information
ry committed Feb 19, 2019
1 parent 37a6bca commit 57c9176
Show file tree
Hide file tree
Showing 11 changed files with 372 additions and 706 deletions.
19 changes: 5 additions & 14 deletions http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,13 @@ A framework for creating HTTP/HTTPS server.
## Example

```typescript
import { createServer } from "https://deno.land/x/http/server.ts";
import { encode } from "https://deno.land/x/strings/strings.ts";
import { serve } from "https://deno.land/x/http/server.ts";
const s = serve("0.0.0.0:8000");

async function main() {
const server = createServer();
server.handle("/", async (req, res) => {
await res.respond({
status: 200,
body: encode("ok")
});
});
server.handle(new RegExp("/foo/(?<id>.+)"), async (req, res) => {
const { id } = req.match.groups;
await res.respondJson({ id });
});
server.listen("127.0.0.1:8080");
for await (const req of s) {
req.respond({ body: new TextEncoder().encode("Hello World\n") });
}
}

main();
Expand Down
12 changes: 6 additions & 6 deletions http/file_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
listenAndServe,
ServerRequest,
setContentLength,
ServerResponse
Response
} from "./server.ts";
import { cwd, DenoError, ErrorKind, args, stat, readDir, open } from "deno";
import { extname } from "../fs/path.ts";
Expand Down Expand Up @@ -195,14 +195,14 @@ async function serveFallback(req: ServerRequest, e: Error) {
}
}

function serverLog(req: ServerRequest, res: ServerResponse) {
function serverLog(req: ServerRequest, res: Response) {
const d = new Date().toISOString();
const dateFmt = `[${d.slice(0, 10)} ${d.slice(11, 19)}]`;
const s = `${dateFmt} "${req.method} ${req.url} ${req.proto}" ${res.status}`;
console.log(s);
}

function setCORS(res: ServerResponse) {
function setCORS(res: Response) {
if (!res.headers) {
res.headers = new Headers();
}
Expand All @@ -213,11 +213,11 @@ function setCORS(res: ServerResponse) {
);
}

listenAndServe(addr, async (req, res) => {
listenAndServe(addr, async req => {
const fileName = req.url.replace(/\/$/, "");
const filePath = currentDir + fileName;

let response: ServerResponse;
let response: Response;

try {
const fileInfo = await stat(filePath);
Expand All @@ -235,7 +235,7 @@ listenAndServe(addr, async (req, res) => {
setCORS(response);
}
serverLog(req, response);
res.respond(response);
req.respond(response);
}
});

Expand Down
11 changes: 3 additions & 8 deletions http/http_bench.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import * as deno from "deno";
import { serve } from "./server.ts";
import { serve } from "./mod.ts";

const addr = deno.args[1] || "127.0.0.1:4500";
const server = serve(addr);

const body = new TextEncoder().encode("Hello World");

async function main(): Promise<void> {
try {
for await (const request of server) {
await request.responder.respond({ status: 200, body });
}
} catch (e) {
console.log(e.stack);
console.error(e);
for await (const request of server) {
await request.respond({ status: 200, body });
}
}

Expand Down
78 changes: 0 additions & 78 deletions http/readers.ts

This file was deleted.

12 changes: 0 additions & 12 deletions http/readers_test.ts

This file was deleted.

Loading

0 comments on commit 57c9176

Please sign in to comment.