-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
executable file
·66 lines (51 loc) · 1.45 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env speakeasy
web_path = table.remove(arg, 2) or ""
if torchbear.os == "android" then
default_path = "/data/data/com.termux/files/usr/share/static-file-webserver/"
elseif torchbear.os == "linux" then
default_path = "/usr/share/static-file-webserver/"
end
if not fs.exists(default_path) then
_log.error("Path does not exist")
end
_log.info("Initialize web server")
if not fs.exists(web_path) then
web_path = default_path .. "/static/"
end
templates_path = default_path .. "/templates/"
-- Handler function
return function (request)
_log.info("Handle request")
if not fs.exists(web_path .. request.path) then
return {
headers = {
["content-type"] = "application/json",
},
body = json.from_table({
message = "No such file or directory",
status = 404
})
}
end
if fs.is_file(web_path .. request.path) then
local _mime = mime.guess_mime_type(request.path)
return {
headers = {
["content-type"] = _mime,
},
body = fs.read_file(web_path .. request.path)
}
else
local contents = fs.read_dir(web_path .. request.path) or {}
-- Remember to use a glob when using tera to render
local _tera = tera.new(templates_path .. "/*")
return {
headers = {
["content-type"] = "text/html",
},
body = _tera:render("index.html", {
contents = contents,
})
}
end
end