napkin/main.lua

80 lines
2.1 KiB
Lua
Raw Normal View History

2025-02-19 18:10:37 -03:00
local weblit = require('weblit')
2025-02-27 13:10:42 -03:00
local json = require("json")
local radon = require("./radon")
2025-02-27 13:10:42 -03:00
local foppy = require("./foppy")
local config = require("./config")
local authors = require("./authors")
local utils = require("./utils")
2025-02-27 13:10:42 -03:00
local error_handler = require("template.error_handler")
2025-02-27 13:10:42 -03:00
local templater = function(template)
return function(req, res)
local func = dofile("template/" .. template .. ".lua")
res.code = 200
res.body = radon.compile(func, {
request = req,
response = res
2025-02-27 13:10:42 -03:00
})
res.headers["Content-Type"] = "text/html"
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)
.use(utils.cookies)
2025-02-19 18:10:37 -03:00
.route(
{ path = "/static/:path:" },
weblit.static("static")
)
.route(
{ path = "/media/:path:" },
weblit.static("database/media")
)
-- TODO: CLONE ALL OF THIS AS A REST API EVENTUALLY,
.route({ path = "/signup" }, templater("signup"))
.route({ path = "/signup2" }, templater("signup2"))
.route({ path = "/login" }, templater("login"))
.route({ path = "/login2" }, templater("login2"))
.route({ path = "/logout" }, templater("logout"))
2025-03-02 10:45:22 -03:00
.route({ path = "/new-blog" }, templater("new-blog"))
.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
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
return error_handler({
request = req, response = res
}, "post_not_found", "/")
end)
2025-02-27 13:10:42 -03:00
.route({ path = "/" }, templater("index"))
2025-02-19 18:10:37 -03:00
-- Start the server
.start()