napkin/main.lua

229 lines
5.8 KiB
Lua

local weblit = require('weblit')
local cookie = require('weblit-cookie')
--local multipart = require('weblit-multipart')
local timer = require("timer")
local radon = require("radon")
local foppy = require("./foppy")
local database = require("./database")
local errors = require("./errors")
local utils = require("./utils")
local blogs = require("blogs")
local authors = require("authors")
local templater = function(template, rate)
return function(req, res)
if rate then
local coro = coroutine.running()
timer.setTimeout(rate, function()
if coroutine.status(coro) ~= "dead" then
coroutine.resume(coro)
end
end)
end
local func = dofile("template/" .. template .. ".lua")
res.code = 200
res.body = radon.compile(func, {
request = req,
response = res
})
res.headers["Content-Type"] = "text/html"
if rate then
coroutine.yield()
end
end
end
weblit.app
.bind({ host = "0.0.0.0", port = 1337 })
-- Configure weblit server
.use(weblit.logger)
.use(weblit.autoHeaders)
.use(utils.forms)
.use(cookie)
.route(
{ path = "/static/:path" },
weblit.static("static")
)
.route(
{ path = "/media/:path:", method = "GET" },
weblit.static("database/media")
)
--.route(
-- { path = "/media", method = "POST" },
-- multipart { writeTo = "database/media" }
--)
-- TODO: CLONE ALL OF THIS AS A REST API EVENTUALLY,
.route({ path = "/signup" }, templater("signup"))
.route({ path = "/signup2" }, templater("signup2", 3000))
-- LOGIN ------------------------
.route({ path = "/login", method = "GET" }, templater("login"))
.route({ path = "/login" }, function(req, res)
local token = req.cookies.auth_token
local form = req.form
local data = {
request = req,
response = res
}
if (not token) and form and form.id and form.password then
if form.id:sub(1, 1) == "@" then
local blog = blogs.from_handle(form.id:sub(2))
if not blog then
return errors.handle(data, "blog_not_found", "/login")
end
form.id = blog.owner
end
token = authors.auth(form.id, form.password)
if not token then
return errors.handle(data, "wrong_credentials", "/login")
end
res.setCookie(
"auth_token", token, {
Path = "/", SameSite = "Strict"
}
)
req.cookies.auth_token = token
end
res.code = 302
res.headers["Location"] = "/"
return ""
end)
.route({ path = "/logout" }, function(req, res)
res.clearCookie("auth_token")
res.headers["Location"] = "/"
res.code = 302
end)
-- BLOGS ------------------------
.route({ path = "/new-blog" }, templater("new-blog"))
.route({ path = "/@" }, function(req, res)
local form = req.form
if not (form and form.handle) then
res.code = 302
res.headers["Location"] = "/"
return
end
local handle = form.handle
local data = {
request = req,
response = res
}
local prev = req.headers["referer"] or "/"
if #handle == 0 then
return errors.handle(data, "blog_handle_invalid", prev)
end
if req.method ~= "POST" then
local blog = blogs.from_handle(handle)
if not blog then
return errors.handle(data, "blog_not_found", prev)
end
end
res.code = 307
res.headers["Location"] = "/@" .. handle
end)
.route({ path = "/@:blog:", method = "GET" }, templater("blog"))
.route({ path = "/@:blog:", method = "POST" }, function(req, res)
local form = req.form
if not form then
res.headers["Location"] = "/"
res.code = 302
return
end
local data = {
request = req,
response = res
}
local token = req.cookies.auth_token
if not token then
return errors.handle(data, "not_logged_in", "/login")
end
local author_id = authors.id_from_token(token)
if not author_id then
return errors.handle(data, "invalid_token", "/login")
end
local blog = req.params.blog
if not (blog and utils.is_valid_handle(blog)) then
return errors.handle(data, "blog_handle_invalid", "/new-blog")
end
local ok, err = blogs.new_blog(
author_id,
blog,
form.title,
form.about
)
if not ok then
return errors.handle(data, err, "/new-blog")
end
res.code = 302
res.headers["Location"] = "/@" .. blog
end)
-- POSTS ------------------------
.route({ path = "/@:blog:/:post:" }, templater("post"))
.route({ path = "/p/:path:", method = "GET" }, function(req, res)
local path = req.params.path
if #path == 0 then
res.code = 301
res.headers["Location"] = "/"
return
end
local redirects = foppy.open("database/redirects.json")
assert(redirects)
local redirect = redirects[path]
if redirect then
res.code = 301
res.headers["Location"] = redirect
return
end
return errors.handle({
request = req,
response = res
}, "post_not_found", "/")
end)
.route({ path = "/" }, templater("index"))
.route({ path = "/rules" }, templater("rules"))
-- Start the server
.start()