-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add feature flag with comma, update fixtures & readme
- Loading branch information
1 parent
9e27948
commit f62afd7
Showing
7 changed files
with
129 additions
and
49 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { | ||
green, | ||
cyan, | ||
bold, | ||
yellow, | ||
red, | ||
} from "https://deno.land/std@0.53.0/fmt/colors.ts"; | ||
|
||
import { Application, HttpError, send, Status } from "https://deno.land/x/oak/mod.ts"; | ||
|
||
const app = new Application(); | ||
|
||
// Error handler middleware | ||
app.use(async (context, next) => { | ||
try { | ||
await next(); | ||
} catch (e) { | ||
if (e instanceof HttpError) { | ||
context.response.status = e.status as any; | ||
if (e.expose) { | ||
context.response.body = `<!DOCTYPE html> | ||
<html> | ||
<body> | ||
<h1>${e.status} - ${e.message}</h1> | ||
</body> | ||
</html>`; | ||
} else { | ||
context.response.body = `<!DOCTYPE html> | ||
<html> | ||
<body> | ||
<h1>${e.status} - ${Status[e.status]}</h1> | ||
</body> | ||
</html>`; | ||
} | ||
} else if (e instanceof Error) { | ||
context.response.status = 500; | ||
context.response.body = `<!DOCTYPE html> | ||
<html> | ||
<body> | ||
<h1>500 - Internal Server Error</h1> | ||
</body> | ||
</html>`; | ||
console.log("Unhandled Error:", red(bold(e.message))); | ||
console.log(e.stack); | ||
} | ||
} | ||
}); | ||
|
||
// Logger | ||
app.use(async (context, next) => { | ||
await next(); | ||
const rt = context.response.headers.get("X-Response-Time"); | ||
console.log( | ||
`${green(context.request.method)} ${cyan(context.request.url.pathname)} - ${ | ||
bold( | ||
String(rt), | ||
) | ||
}`, | ||
); | ||
}); | ||
|
||
// Response Time | ||
app.use(async (context, next) => { | ||
const start = Date.now(); | ||
await next(); | ||
const ms = Date.now() - start; | ||
context.response.headers.set("X-Response-Time", `${ms}ms`); | ||
}); | ||
|
||
// Send static content | ||
app.use(async (context) => { | ||
await context.send({ | ||
root: `${Deno.cwd()}/fixtures/static`, | ||
index: "index.html", | ||
}); | ||
}); | ||
|
||
const options = { hostname: "127.0.0.1", port: 3000 }; | ||
console.log( | ||
bold("Start listening on ") + yellow(`${options.hostname}:${options.port}`), | ||
); | ||
await app.listen(options); |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>A static page</title> | ||
<link rel="stylesheet" href="/style.css"> | ||
</head> | ||
<body> | ||
<h1>Hello from a static page</h1> | ||
</body> | ||
</html> |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
body {color: #c0392b} |
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