refactor template endpoints to use ECR.embed for that extra 💪 performance 💪

This commit is contained in:
Jill 2023-01-04 12:20:45 +03:00
parent 6581d13a45
commit d25b006e57
4 changed files with 14 additions and 14 deletions

View File

@ -46,7 +46,7 @@ module CrystalGauntlet
DATABASE = DB.open(ENV["DATABASE_URL"]? || "sqlite3://./crystal-gauntlet.db")
@@endpoints = Hash(String, (HTTP::Server::Context -> String)).new
@@template_endpoints = Hash(String, (HTTP::Server::Context -> String)).new
@@template_endpoints = Hash(String, (HTTP::Server::Context -> Nil)).new
@@up_at = nil
@ -162,7 +162,7 @@ module CrystalGauntlet
if CrystalGauntlet.template_endpoints.has_key?(path)
func = CrystalGauntlet.template_endpoints[path]
begin
value = func.call(context)
func.call(context)
rescue err
LOG.error { "error while handling #{path.colorize(:white)}:" }
LOG.error { err.to_s }
@ -177,10 +177,6 @@ module CrystalGauntlet
end
context.response.content_type = "text/html"
context.response.respond_with_status(500, "Internal server error occurred, sorry about that")
else
LOG.debug { "-> " + value }
context.response.content_type = "text/html"
context.response.print value
end
else
call_next(context)

View File

@ -2,6 +2,7 @@ require "ecr"
include CrystalGauntlet
CrystalGauntlet.template_endpoints[""] = ->(context : HTTP::Server::Context): String {
ECR.render("./public/template/index.ecr")
CrystalGauntlet.template_endpoints[""] = ->(context : HTTP::Server::Context) {
context.response.content_type = "text/html"
ECR.embed("./public/template/index.ecr", context.response)
}

View File

@ -4,9 +4,10 @@ include CrystalGauntlet
levels_per_page = 10
CrystalGauntlet.template_endpoints["/tools/levels"] = ->(context : HTTP::Server::Context): String {
CrystalGauntlet.template_endpoints["/tools/levels"] = ->(context : HTTP::Server::Context) {
context.response.content_type = "text/html"
page = (context.request.query_params["page"]? || "0").to_i? || 0
total_levels = DATABASE.scalar("select count(*) from levels").as(Int64)
levels = DATABASE.query_all("select levels.id, name, users.username, levels.community_difficulty, levels.difficulty, levels.featured, levels.epic from levels left join users on levels.user_id = users.id order by levels.id desc limit #{levels_per_page} offset #{page * levels_per_page}", as: {Int32, String, String, Int32?, Int32?, Bool, Bool})
ECR.render("./public/template/levels.ecr")
ECR.embed("./public/template/levels.ecr", context.response)
}

View File

@ -17,7 +17,9 @@ def get_next_song_id() : Int32
end
end
CrystalGauntlet.template_endpoints["/tools/song_upload"] = ->(context : HTTP::Server::Context): String {
CrystalGauntlet.template_endpoints["/tools/song_upload"] = ->(context : HTTP::Server::Context) {
context.response.content_type = "text/html"
error = nil
song_id = nil
body = context.request.body
@ -28,11 +30,11 @@ CrystalGauntlet.template_endpoints["/tools/song_upload"] = ->(context : HTTP::Se
DATABASE.exec("insert into songs (id, url) values (?, ?)", song_id, params["url"])
rescue error
# todo: HELP HOW DO I DO THIS BUT BETTER
ECR.render("./public/template/song_upload.ecr")
ECR.embed("./public/template/song_upload.ecr", context.response)
else
ECR.render("./public/template/song_upload.ecr")
ECR.embed("./public/template/song_upload.ecr", context.response)
end
end
ECR.render("./public/template/song_upload.ecr")
ECR.embed("./public/template/song_upload.ecr", context.response)
}