diff --git a/TODO.txt b/TODO.txt index 98ea0df..1d968ef 100644 --- a/TODO.txt +++ b/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. \ No newline at end of file diff --git a/assets/.init.lua b/assets/.init.lua index 50355e1..0b5456b 100644 --- a/assets/.init.lua +++ b/assets/.init.lua @@ -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 = 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() diff --git a/assets/.lua/migrate.lua b/assets/.lua/migrate.lua index ee8fd42..e787884 100644 --- a/assets/.lua/migrate.lua +++ b/assets/.lua/migrate.lua @@ -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 diff --git a/assets/migrations/01 create node.sql b/assets/.migrations/01 create node.sql similarity index 100% rename from assets/migrations/01 create node.sql rename to assets/.migrations/01 create node.sql diff --git a/assets/migrations/2 create node_type.sql b/assets/.migrations/2 create node_type.sql similarity index 100% rename from assets/migrations/2 create node_type.sql rename to assets/.migrations/2 create node_type.sql diff --git a/assets/tmpl/fragment/dirlist.tmpl b/assets/.tmpl/fragment/dirlist.tmpl similarity index 91% rename from assets/tmpl/fragment/dirlist.tmpl rename to assets/.tmpl/fragment/dirlist.tmpl index 5c37a55..3db5246 100644 --- a/assets/tmpl/fragment/dirlist.tmpl +++ b/assets/.tmpl/fragment/dirlist.tmpl @@ -3,7 +3,7 @@ {% for _, dir in ipairs(directories) do %}
{%& dir.name %}
diff --git a/assets/tmpl/home.tmpl b/assets/.tmpl/home.tmpl similarity index 66% rename from assets/tmpl/home.tmpl rename to assets/.tmpl/home.tmpl index 08b77fd..afa707f 100644 --- a/assets/tmpl/home.tmpl +++ b/assets/.tmpl/home.tmpl @@ -1,4 +1,4 @@ {% function block.pagebody() %} -Open… +Open… {% end %} {% render('layout') %} \ No newline at end of file diff --git a/assets/tmpl/layout.tmpl b/assets/.tmpl/layout.tmpl similarity index 100% rename from assets/tmpl/layout.tmpl rename to assets/.tmpl/layout.tmpl diff --git a/assets/tmpl/opendb.tmpl b/assets/.tmpl/opendb.tmpl similarity index 80% rename from assets/tmpl/opendb.tmpl rename to assets/.tmpl/opendb.tmpl index 16d1d07..1ea1671 100644 --- a/assets/tmpl/opendb.tmpl +++ b/assets/.tmpl/opendb.tmpl @@ -1,13 +1,14 @@ {% block.styles = { "/static/css/filepicker.css" } %} +{% print("hello template") %} {% function block.pagebody() %}

Please select a database file to load.

-
+
diff --git a/assets/dirlist.lua b/assets/dirlist.lua new file mode 100644 index 0000000..7ca7c41 --- /dev/null +++ b/assets/dirlist.lua @@ -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 }) \ No newline at end of file diff --git a/assets/index.lua b/assets/index.lua new file mode 100644 index 0000000..30bf0ca --- /dev/null +++ b/assets/index.lua @@ -0,0 +1 @@ +fm.render("home") \ No newline at end of file diff --git a/assets/open.lua b/assets/open.lua new file mode 100644 index 0000000..1daff92 --- /dev/null +++ b/assets/open.lua @@ -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 }) diff --git a/assets/testdb.lua b/assets/testdb.lua new file mode 100644 index 0000000..21a415a --- /dev/null +++ b/assets/testdb.lua @@ -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("

"..ntype.."

") +end