Repository navigation
Expand file tree
/
Copy pathserver.lua
More file actions
358 lines (280 loc) · 8.89 KB
/
Copy pathserver.lua
File metadata and controls
358 lines (280 loc) · 8.89 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
local socket = require("socket")
local url = require('socket.url')
local json = require('cjson')
function _G.getDir()
local handle
local result
if os.getenv("OS") == "Windows_NT" then
handle = io.popen("cd")
else
handle = io.popen("pwd")
end
if handle then
result = handle:read("*a"):gsub("%s+", "")
handle:close()
else
result = "Failed to get directory"
end
return result
end
package.path = package.path .. ";" .. _G.getDir() .. "/?.lua"
-- package.path = package.path .. ";" .. _G.getDir() .. "/luax/?.lua"
package.path = package.path .. ";" .. _G.getDir() .. "/?/init.lua"
package.cpath = package.cpath .. ";" .. _G.getDir() .. "/?.dll"
-- expose to global usage
_G.h = require('luax')
local utility = require('utility')
local page = require('templates.page')
local todoEdit = require('templates.todo-edit')
local toggleMain = require('templates.toggle-main')
local todoItem = require('templates.todo-item')
local todoList = require('templates.todo-list')
local clearCompleted = require('templates.clear-completed')
local todoFooter = require('templates.todo-footer')
local todos = {}
local idCounter = #todos
local filters = {
{ url = "#/", name = "All", selected = true },
{ url = "#/active", name = "Active", selected = false },
{ url = "#/completed", name = "Completed", selected = false },
}
local function Page(client, headers)
local cookieString = headers["cookie"]
local sessionId = cookieString and cookieString:match("sessionId=([^;]+)")
local setCookie
if not sessionId then
local newCookieValue = utility.randomString(32)
setCookie = "sessionId="
.. newCookieValue
.. "; Expires="
.. os.date("%a, %d %b %Y %X GMT", os.time() + 6000)
.. "; HttpOnly"
-- reset todos, counter
todos = {}
idCounter = 0
end
local html = page.Page(todos, filters)
local customHeaders = nil
if setCookie then
customHeaders = {
["Set-Cookie"] = setCookie
}
end
utility.render(client, "200 OK", html, customHeaders)
end
local function SetHash(client, queryName)
local name = queryName or "All"
for _, filter in ipairs(filters) do
filter.selected = (filter.name == name)
end
utility.render(client, "200 OK")
end
local function TodoJSON(client, queryCount)
local count = queryCount == 'true'
if count then
utility.render(client, "200 OK", string.format(#todos), { ["Content-Type"] = "text/plain" })
else
utility.render(client, "200 OK", json.encode(todos), { ["Content-Type"] = "application/json" })
end
end
local function Footer(client)
local html = todoFooter.TodoFooter(todos, filters)
utility.render(client, "200 OK", html)
end
local function AddTodo(client, title)
local newTodo
if title and title ~= '' then
idCounter = idCounter + 1
newTodo = {
id = idCounter,
title = utility.unescape(title),
done = false,
editing = false
}
table.insert(todos, newTodo)
end
local html
if #todos == 1 then
html = todoList.TodoList(todos, filters)
else
local filterName = utility.selectedFilter(filters)
html = todoItem.TodoItem(newTodo, filterName)
end
utility.render(client, "200 OK", html)
end
local function ToggleMain(client)
local html = toggleMain.ToggleMain(todos)
utility.render(client, "200 OK", html)
end
local function UpdateCount(client)
local uncompletedCount = utility.countNotDone(todos)
local plural = ''
if uncompletedCount ~= 1 then
plural = 's'
end
local str = string.format('<strong>%s</strong> item%s left', uncompletedCount, plural)
utility.render(client, "200 OK", str)
end
local function ToggleTodo(client, query)
local id = tonumber(string.match(query.id, "%d+"))
local done = query.done == 'true'
local html
for _, todo in ipairs(todos) do
if todo.id == id then
todo.done = not done
local filterName = utility.selectedFilter(filters)
html = todoItem.TodoItem(todo, filterName)
break
end
end
utility.render(client, "200 OK", html)
end
local function ToggleAll(client)
local checked = utility.defChecked(todos)
utility.render(client, "200 OK", tostring(checked))
end
local function Completed(client)
local hasCompleted = utility.hasCompleteTask(todos)
local html
if hasCompleted then
html = clearCompleted.ClearCompleted(todos)
end
utility.render(client, "200 OK", html)
end
local function RemoveTodo(client, queryId)
local id = tonumber(string.match(queryId, "%d+"))
for index, todo in ipairs(todos) do
if todo.id == id then
table.remove(todos, index)
break
end
end
utility.render(client, "200 OK")
end
local function EditTodo(client, queryId)
local id = tonumber(string.match(queryId, "%d+"))
local html
for _, todo in ipairs(todos) do
if todo.id == id then
html = todoEdit.EditTodo({ id = id, title = todo.title, done = false, editing = true })
break
end
end
utility.render(client, "200 OK", html)
end
local function UpdateTodo(client, query)
local id = tonumber(string.match(query.id, "%d+"))
local title = query.title
local html
for index, todo in ipairs(todos) do
if todo.id == id then
if title and title ~= '' then
todo.title = utility.unescape(title)
local filterName = utility.selectedFilter(filters)
html = todoItem.TodoItem(todo, filterName)
else
table.remove(todos, index)
end
break
end
end
utility.render(client, "200 OK", html)
end
local function SwapJSON(client, queryAll)
local all = queryAll == 'true'
for _, todo in ipairs(todos) do
if all then
todo.done = true
else
todo.done = false
end
end
utility.render(client, "200 OK")
end
local function TodoItem(client, queryId)
local id = tonumber(string.match(queryId, "%d+"))
local html
for _, todo in ipairs(todos) do
if todo.id == id then
local filterName = utility.selectedFilter(filters)
html = todoItem.TodoItem(todo, filterName)
break
end
end
utility.render(client, "200 OK", html)
end
local function handler(client, request, headers)
if request then
local method, path = request:match("^(%w+)%s([^%s]+)%sHTTP")
local parsedUrl = url.parse(path)
local queryString = parsedUrl.query
if parsedUrl.path == "/" then
Page(client, headers)
elseif parsedUrl.path == "/set-hash" then
SetHash(client, utility.parseQuery(queryString, "name"))
elseif parsedUrl.path == "/todo-json" then
TodoJSON(client, utility.parseQuery(queryString, "count"))
elseif parsedUrl.path == "/footer" then
Footer(client)
elseif parsedUrl.path == "/add-todo" then
AddTodo(client, utility.parseQuery(queryString, "title"))
elseif parsedUrl.path == "/toggle-main" then
ToggleMain(client)
elseif parsedUrl.path == "/update-count" then
UpdateCount(client)
elseif parsedUrl.path == "/toggle-todo" then
ToggleTodo(client, utility.parseQuery(queryString))
elseif parsedUrl.path == "/toggle-all" then
ToggleAll(client)
elseif parsedUrl.path == "/completed" then
Completed(client)
elseif parsedUrl.path == "/remove-todo" then
RemoveTodo(client, utility.parseQuery(queryString, "id"))
elseif parsedUrl.path == "/edit-todo" then
EditTodo(client, utility.parseQuery(queryString, "id"))
elseif parsedUrl.path == "/update-todo" then
UpdateTodo(client, utility.parseQuery(queryString))
elseif parsedUrl.path == "/swap-json" then
SwapJSON(client, utility.parseQuery(queryString, "all"))
elseif parsedUrl.path == "/todo-item" then
TodoItem(client, utility.parseQuery(queryString, "id"))
elseif utility.mimes[parsedUrl.path:lower():match("[^.]*$")] and method == "GET" then
local file = parsedUrl.path
local contentType = utility.getType(file)
-- custom axe-core path
if file == '/node_modules/axe-core/axe.min.js' then
file = '/cypress-example-todomvc' .. file:gsub('^%.', '')
end
local content, err = utility.readStaticFile(file)
if not err then
utility.render(client, "200 OK", content, { ["Content-Type"] = contentType })
else
utility.render(client, "404 Not Found", "Not Found", { ["Content-Type"] = "text/plain" })
end
else
-- 404
utility.render(client, "404 Not Found", "Not Found", { ["Content-Type"] = "text/plain" })
end
end
end
-- non blocking io socket setup
local server = socket.tcp()
server:bind("*", 8888)
server:listen()
server:settimeout(0)
print('Lua Socket TCP Server running at http://127.0.0.1:8888/')
while true do
-- Check for new connections
local client = server:accept()
if client then
client:settimeout(10)
local headers = {}
local request, err = client:receive()
if not err then
utility.getHeaders(client, headers)
handler(client, request, headers)
end
-- done with client, close the object
client:close()
end
end