-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
46 lines (37 loc) · 1.1 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
local fs = require('fs')
local __dirname = module.dir
function favicon (path, options)
local icon -- caching the icon
if not options then options = {} end
path = path or __dirname .. '/favicon.ico'
maxAge = options.maxAge or (1000 * 60 * 60 * 24) -- 1 day
return function (req, res, follow)
if ('/favicon.ico' ~= req.url) then
return follow()
end
if ('GET' ~= req.method and 'HEAD' ~= req.method) then
local status = 'OPTIONS' == req.method and 200 or 405
res:writeHead(status, {['Allow'] = 'GET, HEAD, OPTIONS'})
return res:finish()
end
if (icon) then
res:writeHead(304, icon.headers)
res:finish(icon.body)
else
fs.readFile(path, function (err, buf)
if (err) then follow(err) end
icon = {
body = buf,
headers = {
['Content-Type'] = 'image/x-icon',
['Content-Length'] = #buf,
['Cache-Control'] = 'public, max-age=' .. (maxAge / 1000)
}
}
res:writeHead(200, icon.headers)
res:finish(buf)
end)
end
end
end
return favicon