Move handlers into Lua Server Pages
This commit is contained in:
parent
3296db9f80
commit
9f2eeb0a50
1
TODO.txt
1
TODO.txt
@ -2,6 +2,5 @@ Use pattern matching (https://github.com/silentbicycle/tamale) for parsed sexps
|
||||
Write schema management package
|
||||
Unceremoniously dump a rudimentary schema management UI on index
|
||||
Set up page routes
|
||||
Figure out how Lua Server Pages work
|
||||
Try keeping shared state in a zipped file
|
||||
Make DB creation explicit, rather than automatic when opening a nonexistent file.
|
@ -3,7 +3,7 @@ local migrate = require "migrate"
|
||||
local DB_FILENAME = "peachy.db"
|
||||
sqlite3 = require "lsqlite3"
|
||||
|
||||
local function OpenDB(req)
|
||||
function OpenDB(req)
|
||||
local appID <const> = utf8.codepoint("🍑")
|
||||
local filename = req.session.filename
|
||||
if not filename then
|
||||
@ -31,53 +31,9 @@ db:exec("PRAGMA foreign_keys")
|
||||
fm = require "fullmoon"
|
||||
schema = require "schema"
|
||||
fm.sessionOptions.secret = false
|
||||
fm.setTemplate({ "/tmpl/", tmpl = "fmt" })
|
||||
fm.setTemplate({ "/.tmpl/", tmpl = "fmt" })
|
||||
|
||||
fm.setRoute("/static/*", fm.servePath)
|
||||
fm.setRoute("/", fm.servePath("index.lua"))
|
||||
fm.setRoute("*", fm.servePath)
|
||||
|
||||
fm.setRoute(fm.POST "/open", function(r)
|
||||
local filename = r.params.filename
|
||||
if filename == "" then filename = nil end
|
||||
r.session.filename = filename
|
||||
return false
|
||||
end)
|
||||
fm.setRoute("/open", function(r)
|
||||
local files = {}
|
||||
local filename = r.session.filename or ""
|
||||
return fm.serveContent("opendb", { filename = filename, dir = files })
|
||||
end)
|
||||
fm.setRoute("/testdb", function(r)
|
||||
OpenDB(r)
|
||||
local name = tostring(math.random(100000))
|
||||
schema.new_node_type(r.db, name)
|
||||
for ntype in schema.get_node_types(r.db) do
|
||||
print(ntype)
|
||||
end
|
||||
return false
|
||||
end)
|
||||
fm.setRoute("/dirlist", function(r)
|
||||
local function isSpecial(name)
|
||||
return name == "." or name == ".." or string.sub(name, 1, 1) == "$"
|
||||
end
|
||||
local dirpath = r.params.dirpath
|
||||
local files, directories = {}, {}
|
||||
for name, kind in assert(unix.opendir(dirpath)) do
|
||||
if not isSpecial(name) then
|
||||
local file = { name = name, path = path.join(dirpath, name) }
|
||||
if kind == unix.DT_REG then
|
||||
table.insert(files, file)
|
||||
elseif kind == unix.DT_DIR then
|
||||
table.insert(directories, file)
|
||||
end
|
||||
end
|
||||
end
|
||||
local function compareName(f1, f2)
|
||||
return f1.name < f2.name
|
||||
end
|
||||
table.sort(directories, compareName)
|
||||
table.sort(files, compareName)
|
||||
return fm.serveContent("fragment/dirlist", { files = files, directories = directories })
|
||||
end)
|
||||
|
||||
fm.setRoute("/", fm.serveContent "home")
|
||||
fm.run()
|
||||
|
@ -43,7 +43,7 @@ end
|
||||
local function migrate(db)
|
||||
local migrations = {}
|
||||
local seqnums = {}
|
||||
local MIGRATION_PATH = "/migrations/"
|
||||
local MIGRATION_PATH = "/.migrations/"
|
||||
local paths = GetZipPaths(MIGRATION_PATH)
|
||||
for _, p in ipairs(paths) do
|
||||
-- check that sequence number doesn't already exist
|
||||
|
@ -3,7 +3,7 @@
|
||||
{% for _, dir in ipairs(directories) do %}
|
||||
<details
|
||||
hx-trigger="toggle once"
|
||||
hx-get="/dirlist?dirpath={%& dir.path %}"
|
||||
hx-get="/dirlist.lua?dirpath={%& dir.path %}"
|
||||
hx-target="find .details-children">
|
||||
<summary>{%& dir.name %}</summary>
|
||||
<div class="details-children"></div>
|
@ -1,4 +1,4 @@
|
||||
{% function block.pagebody() %}
|
||||
<a href="/open">Open…</a>
|
||||
<a href="/open.lua">Open…</a>
|
||||
{% end %}
|
||||
{% render('layout') %}
|
@ -1,13 +1,14 @@
|
||||
{% block.styles = { "/static/css/filepicker.css" } %}
|
||||
{% print("hello template") %}
|
||||
{% function block.pagebody() %}
|
||||
<p>Please select a database file to load.</p>
|
||||
<form action="/open" method=POST>
|
||||
<form action="/open.lua" method=POST>
|
||||
<input id="filename-input" name="filename" value="{%& filename %}">
|
||||
<input type="submit">
|
||||
</form>
|
||||
<div
|
||||
class="filepicker card"
|
||||
hx-get="/dirlist?dirpath=/"
|
||||
hx-get="/dirlist.lua?dirpath=/"
|
||||
hx-trigger="load"
|
||||
_="on change(target)[target.type=='radio'] set #filename-input.value to target.value">
|
||||
</div>
|
22
assets/dirlist.lua
Normal file
22
assets/dirlist.lua
Normal file
@ -0,0 +1,22 @@
|
||||
local r = fm.getRequest()
|
||||
local function isSpecial(name)
|
||||
return name == "." or name == ".." or string.sub(name, 1, 1) == "$"
|
||||
end
|
||||
local dirpath = r.params.dirpath
|
||||
local files, directories = {}, {}
|
||||
for name, kind in assert(unix.opendir(dirpath)) do
|
||||
if not isSpecial(name) then
|
||||
local file = { name = name, path = path.join(dirpath, name) }
|
||||
if kind == unix.DT_REG then
|
||||
table.insert(files, file)
|
||||
elseif kind == unix.DT_DIR then
|
||||
table.insert(directories, file)
|
||||
end
|
||||
end
|
||||
end
|
||||
local function compareName(f1, f2)
|
||||
return f1.name < f2.name
|
||||
end
|
||||
table.sort(directories, compareName)
|
||||
table.sort(files, compareName)
|
||||
fm.render("fragment/dirlist", { files = files, directories = directories })
|
1
assets/index.lua
Normal file
1
assets/index.lua
Normal file
@ -0,0 +1 @@
|
||||
fm.render("home")
|
9
assets/open.lua
Normal file
9
assets/open.lua
Normal file
@ -0,0 +1,9 @@
|
||||
local r = fm.getRequest()
|
||||
if GetMethod() == "POST" then
|
||||
local filename = r.params.filename
|
||||
if filename == "" then filename = nil end
|
||||
r.session.filename = filename
|
||||
end
|
||||
|
||||
local filename = r.session.filename or ""
|
||||
fm.render("opendb", { filename = filename })
|
7
assets/testdb.lua
Normal file
7
assets/testdb.lua
Normal file
@ -0,0 +1,7 @@
|
||||
local r = fm.getRequest()
|
||||
OpenDB(r)
|
||||
local name = tostring(math.random(100000))
|
||||
schema.new_node_type(r.db, name)
|
||||
for ntype in schema.get_node_types(r.db) do
|
||||
Write("<p>"..ntype.."</p>")
|
||||
end
|
Loading…
Reference in New Issue
Block a user