84 lines
2.3 KiB
Lua
84 lines
2.3 KiB
Lua
local migrate = require "migrate"
|
|
|
|
local DB_FILENAME = "peachy.db"
|
|
sqlite3 = require "lsqlite3"
|
|
|
|
local function OpenDB(req)
|
|
local appID <const> = utf8.codepoint("🍑")
|
|
local filename = req.session.filename
|
|
if not filename then
|
|
error "no file selected"
|
|
end
|
|
local db = sqlite3.open(filename, sqlite3.OPEN_READWRITE)
|
|
if not db then
|
|
db, errcode, errmsg = sqlite3.open(filename)
|
|
if errcode then error(errmsg) end
|
|
errcode = db:exec(string.format("PRAGMA application_id=%d", appID))
|
|
if errcode ~= sqlite3.OK then error(db:errmsg()) end
|
|
end
|
|
local currentAppID
|
|
for id in db:urows("PRAGMA application_id") do currentAppID = id end
|
|
if currentAppID ~= appID then error("not a Peachy database") end
|
|
schema.setup(db)
|
|
req.db = db
|
|
end
|
|
|
|
local db = sqlite3.open(DB_FILENAME)
|
|
db:exec("PRAGMA journal_mode=WAL")
|
|
db:exec("PRAGMA foreign_keys")
|
|
-- migrate.migrate(db)
|
|
|
|
fm = require "fullmoon"
|
|
schema = require "schema"
|
|
fm.sessionOptions.secret = false
|
|
fm.setTemplate({ "/tmpl/", tmpl = "fmt" })
|
|
|
|
fm.setRoute("/static/*", 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()
|