-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathamatyr.lua
58 lines (47 loc) · 1.22 KB
/
amatyr.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
47
48
49
50
51
52
53
54
55
56
57
58
local redis = require "resty.redis"
local cjson = require "cjson"
local template = require "template"
local conf
-- use nginx $root variable for template dir, needs trailing slash
TEMPLATEDIR = ngx.var.root .. '/';
if not conf then
local f = assert(io.open(ngx.var.document_root .. "/etc/config.json", "r"))
local c = f:read("*all")
f:close()
conf = cjson.decode(c)
end
--
-- Index view
--
local function index()
local page = template.tload('index.html')
local context = { conf=conf }
ngx.print( page(context) )
end
--
-- Webcam view
--
local function cam()
local page = template.tload('cam.html')
local context = { conf=conf }
ngx.print( page(context) )
end
-- mapping patterns to views
local routes = {
['cam'] = cam,
['(.*)$'] = index,
}
-- Set the content type
ngx.header.content_type = 'text/html';
local BASE = '/'
-- iterate route patterns and find view
for pattern, view in pairs(routes) do
local uri = '^' .. BASE .. pattern
local match = ngx.re.match(ngx.var.uri, uri, "") -- regex mather in compile mode
if match then
exit = view(match) or ngx.HTTP_OK
ngx.exit( exit )
end
end
-- no match, return 404
ngx.exit( ngx.HTTP_NOT_FOUND )