Move handlers into Lua Server Pages

This commit is contained in:
Brandon Dyck 2024-08-05 22:46:43 -06:00
parent 3296db9f80
commit 9f2eeb0a50
13 changed files with 49 additions and 54 deletions

View File

@ -2,6 +2,5 @@ Use pattern matching (https://github.com/silentbicycle/tamale) for parsed sexps
Write schema management package Write schema management package
Unceremoniously dump a rudimentary schema management UI on index Unceremoniously dump a rudimentary schema management UI on index
Set up page routes Set up page routes
Figure out how Lua Server Pages work
Try keeping shared state in a zipped file Try keeping shared state in a zipped file
Make DB creation explicit, rather than automatic when opening a nonexistent file. Make DB creation explicit, rather than automatic when opening a nonexistent file.

View File

@ -3,7 +3,7 @@ local migrate = require "migrate"
local DB_FILENAME = "peachy.db" local DB_FILENAME = "peachy.db"
sqlite3 = require "lsqlite3" sqlite3 = require "lsqlite3"
local function OpenDB(req) function OpenDB(req)
local appID <const> = utf8.codepoint("🍑") local appID <const> = utf8.codepoint("🍑")
local filename = req.session.filename local filename = req.session.filename
if not filename then if not filename then
@ -31,53 +31,9 @@ db:exec("PRAGMA foreign_keys")
fm = require "fullmoon" fm = require "fullmoon"
schema = require "schema" schema = require "schema"
fm.sessionOptions.secret = false 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() fm.run()

View File

@ -43,7 +43,7 @@ end
local function migrate(db) local function migrate(db)
local migrations = {} local migrations = {}
local seqnums = {} local seqnums = {}
local MIGRATION_PATH = "/migrations/" local MIGRATION_PATH = "/.migrations/"
local paths = GetZipPaths(MIGRATION_PATH) local paths = GetZipPaths(MIGRATION_PATH)
for _, p in ipairs(paths) do for _, p in ipairs(paths) do
-- check that sequence number doesn't already exist -- check that sequence number doesn't already exist

View File

@ -3,7 +3,7 @@
{% for _, dir in ipairs(directories) do %} {% for _, dir in ipairs(directories) do %}
<details <details
hx-trigger="toggle once" hx-trigger="toggle once"
hx-get="/dirlist?dirpath={%& dir.path %}" hx-get="/dirlist.lua?dirpath={%& dir.path %}"
hx-target="find .details-children"> hx-target="find .details-children">
<summary>{%& dir.name %}</summary> <summary>{%& dir.name %}</summary>
<div class="details-children"></div> <div class="details-children"></div>

View File

@ -1,4 +1,4 @@
{% function block.pagebody() %} {% function block.pagebody() %}
<a href="/open">Open…</a> <a href="/open.lua">Open…</a>
{% end %} {% end %}
{% render('layout') %} {% render('layout') %}

View File

@ -1,13 +1,14 @@
{% block.styles = { "/static/css/filepicker.css" } %} {% block.styles = { "/static/css/filepicker.css" } %}
{% print("hello template") %}
{% function block.pagebody() %} {% function block.pagebody() %}
<p>Please select a database file to load.</p> <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 id="filename-input" name="filename" value="{%& filename %}">
<input type="submit"> <input type="submit">
</form> </form>
<div <div
class="filepicker card" class="filepicker card"
hx-get="/dirlist?dirpath=/" hx-get="/dirlist.lua?dirpath=/"
hx-trigger="load" hx-trigger="load"
_="on change(target)[target.type=='radio'] set #filename-input.value to target.value"> _="on change(target)[target.type=='radio'] set #filename-input.value to target.value">
</div> </div>

22
assets/dirlist.lua Normal file
View 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
View File

@ -0,0 +1 @@
fm.render("home")

9
assets/open.lua Normal file
View 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
View 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