-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for custom error documents and provide an example
- Loading branch information
1 parent
fda1476
commit 1a17597
Showing
3 changed files
with
94 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
return function (app, options) | ||
return function (req, res) | ||
app(req, function (code, headers, body) | ||
local errordocument = options[code] | ||
if errordocument then | ||
errordocument(code, headers, body, res) | ||
else | ||
res(code, headers, body) | ||
end | ||
end) | ||
end | ||
end |
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,36 @@ | ||
local errorhandlers = {} | ||
|
||
function errorhandlers.text(text) | ||
return function (code, headers, body, res) | ||
res(code, headers, text) | ||
end | ||
end | ||
|
||
function errorhandlers.file(path) | ||
return function (code, headers, body, res) | ||
local file = io.open(path) | ||
body = file:read("*a") | ||
res(code, headers, body) | ||
end | ||
end | ||
|
||
function errorhandlers.execute(path) | ||
return function (code, headers, body, res) | ||
local chunk = loadfile(path) | ||
local success, err = pcall(chunk, code, headers, body, res) | ||
if not success then | ||
error("Failed to execute error document handler: " .. err) | ||
res(code, headers, body) | ||
end | ||
end | ||
end | ||
|
||
function errorhandlers.redirect(url) | ||
return function (code, headers, body, res) | ||
code = 302 | ||
headers["Location"] = url | ||
res(code, headers, {}) | ||
end | ||
end | ||
|
||
return errorhandlers |
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,46 @@ | ||
local p = require('utils').prettyPrint | ||
local socketHandler = require('web').socketHandler | ||
local createServer = require('uv').createServer | ||
local errhdl = require('errorhandlers') | ||
|
||
local host = os.getenv("IP") or "0.0.0.0" | ||
local port = os.getenv("PORT") or 8080 | ||
|
||
local state = 1 | ||
local app = function (req, res) | ||
local code = 200 | ||
if state > 3 then | ||
code = 404 | ||
end | ||
|
||
res(code, { | ||
["Content-Type"] = "text/plain" | ||
}, {"Hello ", "World ", tostring(state), "\n"}) | ||
|
||
if req.url.path == "/" then | ||
state = state + 1 | ||
end | ||
end | ||
|
||
app = require('error-document')(app, { | ||
[404] = errhdl.text("TEST 404"), | ||
}) | ||
|
||
app = require('autoheaders')(app) | ||
|
||
app = require('log')(app) | ||
|
||
p{app=app} | ||
|
||
app({ | ||
method = "GET", | ||
url = { path = "/" }, | ||
headers = {} | ||
}, p) | ||
|
||
createServer(host, port, socketHandler(app)) | ||
print("http server listening at http://localhost:8080/") | ||
|
||
require('luv').run() | ||
|
||
print("done.") |