2025-02-19 18:10:37 -03:00
|
|
|
local weblit = require('weblit')
|
2025-03-06 19:48:52 -03:00
|
|
|
local cookie = require('weblit-cookie')
|
2025-03-08 00:07:37 -03:00
|
|
|
local multipart = require('weblit-multipart')
|
|
|
|
local timer = require("timer")
|
|
|
|
local radon = require("radon")
|
|
|
|
|
2025-02-27 13:10:42 -03:00
|
|
|
local foppy = require("./foppy")
|
2025-03-08 00:07:37 -03:00
|
|
|
local database = require("./database")
|
|
|
|
local errors = require("./errors")
|
2025-03-01 01:12:00 -03:00
|
|
|
local utils = require("./utils")
|
2025-02-27 13:10:42 -03:00
|
|
|
|
2025-03-02 00:41:20 -03:00
|
|
|
|
2025-03-03 00:27:45 -03:00
|
|
|
local templater = function(template, rate)
|
2025-02-27 13:10:42 -03:00
|
|
|
return function(req, res)
|
2025-03-03 00:27:45 -03:00
|
|
|
if rate then
|
|
|
|
local coro = coroutine.running()
|
|
|
|
timer.setTimeout(rate, function()
|
|
|
|
if coroutine.status(coro) ~= "dead" then
|
|
|
|
coroutine.resume(coro)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2025-02-27 13:10:42 -03:00
|
|
|
local func = dofile("template/" .. template .. ".lua")
|
|
|
|
|
|
|
|
res.code = 200
|
|
|
|
res.body = radon.compile(func, {
|
|
|
|
request = req,
|
2025-03-01 01:12:00 -03:00
|
|
|
response = res
|
2025-02-27 13:10:42 -03:00
|
|
|
})
|
|
|
|
res.headers["Content-Type"] = "text/html"
|
2025-03-03 00:27:45 -03:00
|
|
|
|
|
|
|
if rate then
|
|
|
|
coroutine.yield()
|
|
|
|
end
|
2025-02-27 13:10:42 -03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2025-02-19 18:10:37 -03:00
|
|
|
weblit.app
|
2025-02-27 13:10:42 -03:00
|
|
|
.bind({ host = "0.0.0.0", port = 1337 })
|
2025-02-19 18:10:37 -03:00
|
|
|
|
|
|
|
-- Configure weblit server
|
|
|
|
.use(weblit.logger)
|
|
|
|
.use(weblit.autoHeaders)
|
2025-03-06 19:48:52 -03:00
|
|
|
.use(utils.forms)
|
|
|
|
.use(cookie)
|
2025-02-19 18:10:37 -03:00
|
|
|
|
2025-02-26 14:39:07 -03:00
|
|
|
.route(
|
2025-03-08 00:07:37 -03:00
|
|
|
{ path = "/static/:path" },
|
2025-02-26 14:39:07 -03:00
|
|
|
weblit.static("static")
|
|
|
|
)
|
|
|
|
|
2025-03-01 01:12:00 -03:00
|
|
|
.route(
|
2025-03-06 19:34:12 -03:00
|
|
|
{ path = "/media/:path:", method = "GET" },
|
2025-03-01 01:12:00 -03:00
|
|
|
weblit.static("database/media")
|
|
|
|
)
|
|
|
|
|
2025-03-08 00:07:37 -03:00
|
|
|
.route(
|
|
|
|
{ path = "/media", method = "POST" },
|
|
|
|
multipart { writeTo = "database/media" }
|
|
|
|
)
|
|
|
|
|
2025-03-01 01:12:00 -03:00
|
|
|
-- TODO: CLONE ALL OF THIS AS A REST API EVENTUALLY,
|
|
|
|
.route({ path = "/signup" }, templater("signup"))
|
2025-03-03 00:27:45 -03:00
|
|
|
.route({ path = "/signup2" }, templater("signup2", 3000))
|
2025-03-01 01:12:00 -03:00
|
|
|
.route({ path = "/login" }, templater("login"))
|
2025-03-03 00:27:45 -03:00
|
|
|
.route({ path = "/login2" }, templater("login2", 3000))
|
|
|
|
.route({ path = "/logout" }, templater("logout", 3000))
|
2025-03-01 01:12:00 -03:00
|
|
|
|
2025-03-02 10:45:22 -03:00
|
|
|
.route({ path = "/new-blog" }, templater("new-blog"))
|
2025-03-03 00:27:45 -03:00
|
|
|
.route({ path = "/new-blog2" }, templater("new-blog2", 2000))
|
2025-03-02 10:45:22 -03:00
|
|
|
|
2025-03-01 01:12:00 -03:00
|
|
|
.route({ path = "/@:blog" }, templater("blog"))
|
|
|
|
.route({ path = "/@:blog/:post" }, templater("post"))
|
|
|
|
.route({ path = "/p/:path" }, function(req, res)
|
|
|
|
local path = req.params.path
|
|
|
|
if #path == 0 then
|
|
|
|
res.code = 301
|
|
|
|
res.headers["Location"] = "/"
|
|
|
|
return
|
|
|
|
end
|
2025-02-26 14:39:07 -03:00
|
|
|
|
2025-03-01 01:12:00 -03:00
|
|
|
local redirects = foppy.open("database/redirects.json")
|
|
|
|
assert(redirects)
|
|
|
|
|
|
|
|
local redirect = redirects[path]
|
|
|
|
if redirect then
|
|
|
|
res.code = 301
|
|
|
|
res.headers["Location"] = redirect
|
2025-02-27 13:10:42 -03:00
|
|
|
return
|
|
|
|
end
|
2025-02-26 14:39:07 -03:00
|
|
|
|
2025-03-02 00:41:20 -03:00
|
|
|
return error_handler({
|
2025-03-03 00:27:45 -03:00
|
|
|
request = req,
|
|
|
|
response = res
|
2025-03-02 00:41:20 -03:00
|
|
|
}, "post_not_found", "/")
|
2025-02-26 14:39:07 -03:00
|
|
|
end)
|
|
|
|
|
2025-02-27 13:10:42 -03:00
|
|
|
.route({ path = "/" }, templater("index"))
|
2025-03-03 00:27:45 -03:00
|
|
|
.route({ path = "/rules" }, templater("rules"))
|
2025-02-19 18:10:37 -03:00
|
|
|
|
|
|
|
-- Start the server
|
|
|
|
.start()
|