forked from radare/luvit-Connect
/
server.lua
100 lines (86 loc) · 1.95 KB
/
server.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
-- luvit-connect middleware framework --
-- author: pancake<nopcode>
-- license: MIT
local os = require('os')
local Url = require('url')
local HTTP = require("http")
local table = require ("table")
local App = require ("./app")
local server = {}
function table.copy(t, deep, seen)
seen = seen or {}
if t == nil then return nil end
if seen[t] then return seen[t] end
local nt = {}
for k, v in pairs(t) do
if deep and type(v) == 'table' then
nt[k] = table.copy(v, deep, seen)
else
nt[k] = v
end
end
setmetatable(nt, table.copy(getmetatable(t), deep, seen))
seen[t] = nt
return nt
end
function server.createServer()
local s = table.copy(server)
s.app = table.copy(App)
s.query = {}
s.handlers = {}
return s
end
function server.favicon(s)
return s
end
function server.static(s, a, b)
if not b then b = a ; a = "" end
return s
end
function server.logger()
local h = function (app)
local ts = os.date ("%Y-%m-%d %H:%M:%S")
print (ts.." "..app.req.method.." : "..app.req.url)
end
return h
end
function server.use(s, h)
table.insert (s.handlers, h)
return s
end
function server.listen(s, port, host)
if not host then host = "0.0.0.0" end
print ("luvit-Connect listening on port "..port)
HTTP.create_server (host, port, function (req, res)
local app = s.app
app.server = s
app.req = req
app.res = res
--- xxx not async or wtf
app.handled = false
for i=1, #s.handlers do
s.handlers[i] (app)
end
-- 404 --
if not app.handled then
local body = "<html><h1>404: resource not found</h1></html>"
res:write_head (404, {
["Content-Type"] = "text/html",
["Content-Length"] = #body
})
res:finish (body)
end
end)
end
function server.router(fn)
local x = function (app)
return fn (app)
-- optimize
--if app.method[app.req.method] then
-- print ("I CAN HAS A REG")
-- return fn (app, app.req, app.res)
--end
end
return x
end
return server