refactor endpoint system

context is now passed in instead of just the body

will allow for ip bans and similar in the future :slugclose:
This commit is contained in:
Jill 2023-01-03 10:02:50 +03:00
parent 0c6f4219da
commit e455a304b9
21 changed files with 46 additions and 46 deletions

View file

@ -41,7 +41,7 @@ module CrystalGauntlet
DATABASE = DB.open(ENV["DATABASE_URL"])
@@endpoints = Hash(String, (String -> String)).new
@@endpoints = Hash(String, (HTTP::Server::Context -> String)).new
def self.endpoints
@@endpoints
@ -89,7 +89,7 @@ module CrystalGauntlet
class GDHandler
include HTTP::Handler
def call(context)
def call(context : HTTP::Server::Context)
# expunge trailing slashes
path = context.request.path.chomp("/")
@ -100,9 +100,9 @@ module CrystalGauntlet
if CrystalGauntlet.endpoints.has_key?(path) && context.request.method == "POST" && body
func = CrystalGauntlet.endpoints[path]
begin
value = func.call(body.gets_to_end)
value = func.call(context)
rescue err
LOG.error { "error while handling #{path}:" }
LOG.error { "error while handling #{path.colorize(:white)}:" }
LOG.error { err.to_s }
is_relevant = true
err.backtrace.each do |str|

View file

@ -4,8 +4,8 @@ require "crypto/bcrypt/password"
include CrystalGauntlet
CrystalGauntlet.endpoints["/accounts/loginGJAccount.php"] = ->(body : String): String {
params = URI::Params.parse(body)
CrystalGauntlet.endpoints["/accounts/loginGJAccount.php"] = ->(context : HTTP::Server::Context): String {
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
LOG.debug { params.inspect }
username = params["userName"]

View file

@ -4,8 +4,8 @@ require "crypto/bcrypt/password"
include CrystalGauntlet
CrystalGauntlet.endpoints["/accounts/registerGJAccount.php"] = ->(body : String): String {
params = URI::Params.parse(body)
CrystalGauntlet.endpoints["/accounts/registerGJAccount.php"] = ->(context : HTTP::Server::Context): String {
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
LOG.debug { params.inspect }
if config_get("accounts.allow_registration").as(Bool | Nil) == false

View file

@ -2,8 +2,8 @@ require "uri"
include CrystalGauntlet
CrystalGauntlet.endpoints["/updateGJUserScore22.php"] = ->(body : String): String {
params = URI::Params.parse(body)
CrystalGauntlet.endpoints["/updateGJUserScore22.php"] = ->(context : HTTP::Server::Context): String {
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
LOG.debug { params.inspect }
user_id, account_id = Accounts.auth(params)

View file

@ -2,8 +2,8 @@ require "uri"
include CrystalGauntlet
CrystalGauntlet.endpoints["/uploadGJComment21.php"] = ->(body : String): String {
params = URI::Params.parse(body)
CrystalGauntlet.endpoints["/uploadGJComment21.php"] = ->(context : HTTP::Server::Context): String {
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
LOG.debug { params.inspect }
user_id, account_id = Accounts.auth(params)

View file

@ -2,8 +2,8 @@ require "uri"
include CrystalGauntlet
CrystalGauntlet.endpoints["/deleteGJLevelUser20.php"] = ->(body : String): String {
params = URI::Params.parse(body)
CrystalGauntlet.endpoints["/deleteGJLevelUser20.php"] = ->(context : HTTP::Server::Context): String {
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
user_id, account_id = Accounts.auth(params)
if !(user_id && account_id)

View file

@ -3,8 +3,8 @@ require "base64"
include CrystalGauntlet
CrystalGauntlet.endpoints["/downloadGJLevel22.php"] = ->(body : String): String {
params = URI::Params.parse(body)
CrystalGauntlet.endpoints["/downloadGJLevel22.php"] = ->(context : HTTP::Server::Context): String {
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
LOG.debug { params.inspect }
response = [] of String

View file

@ -4,8 +4,8 @@ include CrystalGauntlet
comments_per_page = 10
CrystalGauntlet.endpoints["/getGJComments21.php"] = ->(body : String): String {
params = URI::Params.parse(body)
CrystalGauntlet.endpoints["/getGJComments21.php"] = ->(context : HTTP::Server::Context): String {
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
LOG.debug { params.inspect }
level_id = params["levelID"].to_i

View file

@ -6,8 +6,8 @@ include CrystalGauntlet
# things might break if you modify this
levels_per_page = 10
CrystalGauntlet.endpoints["/getGJLevels21.php"] = ->(body : String): String {
params = URI::Params.parse(body)
CrystalGauntlet.endpoints["/getGJLevels21.php"] = ->(context : HTTP::Server::Context): String {
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
LOG.debug { params.inspect }
can_see_unlisted = false

View file

@ -2,8 +2,8 @@ require "uri"
include CrystalGauntlet
CrystalGauntlet.endpoints["/getGJLevelScores211.php"] = ->(body : String): String {
params = URI::Params.parse(body)
CrystalGauntlet.endpoints["/getGJLevelScores211.php"] = ->(context : HTTP::Server::Context): String {
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
LOG.debug { params.inspect }
user_id, account_id = Accounts.auth(params)

View file

@ -2,8 +2,8 @@ require "uri"
include CrystalGauntlet
CrystalGauntlet.endpoints["/rateGJStars211.php"] = ->(body : String): String {
params = URI::Params.parse(body)
CrystalGauntlet.endpoints["/rateGJStars211.php"] = ->(context : HTTP::Server::Context): String {
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
LOG.debug { params.inspect }
level_id = params["levelID"].to_i
@ -43,8 +43,8 @@ CrystalGauntlet.endpoints["/rateGJStars211.php"] = ->(body : String): String {
return "1"
}
CrystalGauntlet.endpoints["/rateGJDemon21.php"] = ->(body : String): String {
params = URI::Params.parse(body)
CrystalGauntlet.endpoints["/rateGJDemon21.php"] = ->(context : HTTP::Server::Context): String {
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
LOG.debug { params.inspect }
level_id = params["levelID"].to_i

View file

@ -2,8 +2,8 @@ require "uri"
include CrystalGauntlet
CrystalGauntlet.endpoints["/uploadGJLevel21.php"] = ->(body : String): String {
params = URI::Params.parse(body)
CrystalGauntlet.endpoints["/uploadGJLevel21.php"] = ->(context : HTTP::Server::Context): String {
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
LOG.debug { params.inspect }
# todo: green user fixes? pretty please?

View file

@ -4,8 +4,8 @@ require "crypto/bcrypt/password"
include CrystalGauntlet
CrystalGauntlet.endpoints["/likeGJItem211.php"] = ->(body : String): String {
params = URI::Params.parse(body)
CrystalGauntlet.endpoints["/likeGJItem211.php"] = ->(context : HTTP::Server::Context): String {
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
LOG.debug { params.inspect }
if !params.has_key?("itemID")

View file

@ -6,8 +6,8 @@ include CrystalGauntlet
mappacks_per_page = 10
CrystalGauntlet.endpoints["/getGJMapPacks21.php"] = ->(body : String): String {
params = URI::Params.parse(body)
CrystalGauntlet.endpoints["/getGJMapPacks21.php"] = ->(context : HTTP::Server::Context): String {
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
LOG.debug { params.inspect }
page = params["page"].to_i32

View file

@ -4,8 +4,8 @@ require "digest/sha256"
include CrystalGauntlet
CrystalGauntlet.endpoints["/getGJSongInfo.php"] = ->(body : String): String {
params = URI::Params.parse(body)
CrystalGauntlet.endpoints["/getGJSongInfo.php"] = ->(context : HTTP::Server::Context): String {
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
LOG.debug { params.inspect }
song_id = params["songID"].to_i32

View file

@ -2,8 +2,8 @@ require "uri"
include CrystalGauntlet
CrystalGauntlet.endpoints["/uploadGJAccComment20.php"] = ->(body : String): String {
params = URI::Params.parse(body)
CrystalGauntlet.endpoints["/uploadGJAccComment20.php"] = ->(context : HTTP::Server::Context): String {
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
LOG.debug { params.inspect }
user_id, account_id = Accounts.auth(params)

View file

@ -2,8 +2,8 @@ require "uri"
include CrystalGauntlet
CrystalGauntlet.endpoints["/deleteGJAccComment20.php"] = ->(body : String): String {
params = URI::Params.parse(body)
CrystalGauntlet.endpoints["/deleteGJAccComment20.php"] = ->(context : HTTP::Server::Context): String {
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
LOG.debug { params.inspect }
user_id, account_id = Accounts.auth(params)

View file

@ -4,8 +4,8 @@ include CrystalGauntlet
comments_per_page = 10
CrystalGauntlet.endpoints["/getGJAccountComments20.php"] = ->(body : String): String {
params = URI::Params.parse(body)
CrystalGauntlet.endpoints["/getGJAccountComments20.php"] = ->(context : HTTP::Server::Context): String {
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
LOG.debug { params.inspect }
account_id = params["accountID"].to_i

View file

@ -2,8 +2,8 @@ require "uri"
include CrystalGauntlet
CrystalGauntlet.endpoints["/getGJUserInfo20.php"] = ->(body : String): String {
params = URI::Params.parse(body)
CrystalGauntlet.endpoints["/getGJUserInfo20.php"] = ->(context : HTTP::Server::Context): String {
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
LOG.debug { params.inspect }
DATABASE.query("select accounts.id, accounts.username, is_admin, messages_enabled, friend_requests_enabled, comments_enabled, youtube_url, twitter_url, twitch_url, accounts.created_at, users.id, stars, demons, coins, user_coins, diamonds, orbs, creator_points, icon_type, color1, color2, glow, cube, ship, ball, ufo, wave, robot, spider, explosion from accounts join users on accounts.id = users.account_id where accounts.id = ?", params["targetAccountID"]) do |rs|

View file

@ -4,8 +4,8 @@ require "crypto/bcrypt/password"
include CrystalGauntlet
CrystalGauntlet.endpoints["/getGJUsers20.php"] = ->(body : String): String {
params = URI::Params.parse(body)
CrystalGauntlet.endpoints["/getGJUsers20.php"] = ->(context : HTTP::Server::Context): String {
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
LOG.debug { params.inspect }
page = params["page"].to_i

View file

@ -2,8 +2,8 @@ require "uri"
include CrystalGauntlet
CrystalGauntlet.endpoints["/updateGJUserScore22.php"] = ->(body : String): String {
params = URI::Params.parse(body)
CrystalGauntlet.endpoints["/updateGJUserScore22.php"] = ->(context : HTTP::Server::Context): String {
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
LOG.debug { params.inspect }
user_id, account_id = Accounts.auth(params)