refactor endpoint system
context is now passed in instead of just the body
will allow for ip bans and similar in the future ![:slugclose:](/assets/img/emoji/slugclose.png)
This commit is contained in:
parent
0c6f4219da
commit
e455a304b9
|
@ -41,7 +41,7 @@ module CrystalGauntlet
|
||||||
|
|
||||||
DATABASE = DB.open(ENV["DATABASE_URL"])
|
DATABASE = DB.open(ENV["DATABASE_URL"])
|
||||||
|
|
||||||
@@endpoints = Hash(String, (String -> String)).new
|
@@endpoints = Hash(String, (HTTP::Server::Context -> String)).new
|
||||||
|
|
||||||
def self.endpoints
|
def self.endpoints
|
||||||
@@endpoints
|
@@endpoints
|
||||||
|
@ -89,7 +89,7 @@ module CrystalGauntlet
|
||||||
class GDHandler
|
class GDHandler
|
||||||
include HTTP::Handler
|
include HTTP::Handler
|
||||||
|
|
||||||
def call(context)
|
def call(context : HTTP::Server::Context)
|
||||||
# expunge trailing slashes
|
# expunge trailing slashes
|
||||||
path = context.request.path.chomp("/")
|
path = context.request.path.chomp("/")
|
||||||
|
|
||||||
|
@ -100,9 +100,9 @@ module CrystalGauntlet
|
||||||
if CrystalGauntlet.endpoints.has_key?(path) && context.request.method == "POST" && body
|
if CrystalGauntlet.endpoints.has_key?(path) && context.request.method == "POST" && body
|
||||||
func = CrystalGauntlet.endpoints[path]
|
func = CrystalGauntlet.endpoints[path]
|
||||||
begin
|
begin
|
||||||
value = func.call(body.gets_to_end)
|
value = func.call(context)
|
||||||
rescue err
|
rescue err
|
||||||
LOG.error { "error while handling #{path}:" }
|
LOG.error { "error while handling #{path.colorize(:white)}:" }
|
||||||
LOG.error { err.to_s }
|
LOG.error { err.to_s }
|
||||||
is_relevant = true
|
is_relevant = true
|
||||||
err.backtrace.each do |str|
|
err.backtrace.each do |str|
|
||||||
|
|
|
@ -4,8 +4,8 @@ require "crypto/bcrypt/password"
|
||||||
|
|
||||||
include CrystalGauntlet
|
include CrystalGauntlet
|
||||||
|
|
||||||
CrystalGauntlet.endpoints["/accounts/loginGJAccount.php"] = ->(body : String): String {
|
CrystalGauntlet.endpoints["/accounts/loginGJAccount.php"] = ->(context : HTTP::Server::Context): String {
|
||||||
params = URI::Params.parse(body)
|
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
|
||||||
LOG.debug { params.inspect }
|
LOG.debug { params.inspect }
|
||||||
|
|
||||||
username = params["userName"]
|
username = params["userName"]
|
||||||
|
|
|
@ -4,8 +4,8 @@ require "crypto/bcrypt/password"
|
||||||
|
|
||||||
include CrystalGauntlet
|
include CrystalGauntlet
|
||||||
|
|
||||||
CrystalGauntlet.endpoints["/accounts/registerGJAccount.php"] = ->(body : String): String {
|
CrystalGauntlet.endpoints["/accounts/registerGJAccount.php"] = ->(context : HTTP::Server::Context): String {
|
||||||
params = URI::Params.parse(body)
|
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
|
||||||
LOG.debug { params.inspect }
|
LOG.debug { params.inspect }
|
||||||
|
|
||||||
if config_get("accounts.allow_registration").as(Bool | Nil) == false
|
if config_get("accounts.allow_registration").as(Bool | Nil) == false
|
||||||
|
|
|
@ -2,8 +2,8 @@ require "uri"
|
||||||
|
|
||||||
include CrystalGauntlet
|
include CrystalGauntlet
|
||||||
|
|
||||||
CrystalGauntlet.endpoints["/updateGJUserScore22.php"] = ->(body : String): String {
|
CrystalGauntlet.endpoints["/updateGJUserScore22.php"] = ->(context : HTTP::Server::Context): String {
|
||||||
params = URI::Params.parse(body)
|
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
|
||||||
LOG.debug { params.inspect }
|
LOG.debug { params.inspect }
|
||||||
|
|
||||||
user_id, account_id = Accounts.auth(params)
|
user_id, account_id = Accounts.auth(params)
|
||||||
|
|
|
@ -2,8 +2,8 @@ require "uri"
|
||||||
|
|
||||||
include CrystalGauntlet
|
include CrystalGauntlet
|
||||||
|
|
||||||
CrystalGauntlet.endpoints["/uploadGJComment21.php"] = ->(body : String): String {
|
CrystalGauntlet.endpoints["/uploadGJComment21.php"] = ->(context : HTTP::Server::Context): String {
|
||||||
params = URI::Params.parse(body)
|
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
|
||||||
LOG.debug { params.inspect }
|
LOG.debug { params.inspect }
|
||||||
|
|
||||||
user_id, account_id = Accounts.auth(params)
|
user_id, account_id = Accounts.auth(params)
|
||||||
|
|
|
@ -2,8 +2,8 @@ require "uri"
|
||||||
|
|
||||||
include CrystalGauntlet
|
include CrystalGauntlet
|
||||||
|
|
||||||
CrystalGauntlet.endpoints["/deleteGJLevelUser20.php"] = ->(body : String): String {
|
CrystalGauntlet.endpoints["/deleteGJLevelUser20.php"] = ->(context : HTTP::Server::Context): String {
|
||||||
params = URI::Params.parse(body)
|
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
|
||||||
|
|
||||||
user_id, account_id = Accounts.auth(params)
|
user_id, account_id = Accounts.auth(params)
|
||||||
if !(user_id && account_id)
|
if !(user_id && account_id)
|
||||||
|
|
|
@ -3,8 +3,8 @@ require "base64"
|
||||||
|
|
||||||
include CrystalGauntlet
|
include CrystalGauntlet
|
||||||
|
|
||||||
CrystalGauntlet.endpoints["/downloadGJLevel22.php"] = ->(body : String): String {
|
CrystalGauntlet.endpoints["/downloadGJLevel22.php"] = ->(context : HTTP::Server::Context): String {
|
||||||
params = URI::Params.parse(body)
|
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
|
||||||
LOG.debug { params.inspect }
|
LOG.debug { params.inspect }
|
||||||
|
|
||||||
response = [] of String
|
response = [] of String
|
||||||
|
|
|
@ -4,8 +4,8 @@ include CrystalGauntlet
|
||||||
|
|
||||||
comments_per_page = 10
|
comments_per_page = 10
|
||||||
|
|
||||||
CrystalGauntlet.endpoints["/getGJComments21.php"] = ->(body : String): String {
|
CrystalGauntlet.endpoints["/getGJComments21.php"] = ->(context : HTTP::Server::Context): String {
|
||||||
params = URI::Params.parse(body)
|
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
|
||||||
LOG.debug { params.inspect }
|
LOG.debug { params.inspect }
|
||||||
|
|
||||||
level_id = params["levelID"].to_i
|
level_id = params["levelID"].to_i
|
||||||
|
|
|
@ -6,8 +6,8 @@ include CrystalGauntlet
|
||||||
# things might break if you modify this
|
# things might break if you modify this
|
||||||
levels_per_page = 10
|
levels_per_page = 10
|
||||||
|
|
||||||
CrystalGauntlet.endpoints["/getGJLevels21.php"] = ->(body : String): String {
|
CrystalGauntlet.endpoints["/getGJLevels21.php"] = ->(context : HTTP::Server::Context): String {
|
||||||
params = URI::Params.parse(body)
|
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
|
||||||
LOG.debug { params.inspect }
|
LOG.debug { params.inspect }
|
||||||
|
|
||||||
can_see_unlisted = false
|
can_see_unlisted = false
|
||||||
|
|
|
@ -2,8 +2,8 @@ require "uri"
|
||||||
|
|
||||||
include CrystalGauntlet
|
include CrystalGauntlet
|
||||||
|
|
||||||
CrystalGauntlet.endpoints["/getGJLevelScores211.php"] = ->(body : String): String {
|
CrystalGauntlet.endpoints["/getGJLevelScores211.php"] = ->(context : HTTP::Server::Context): String {
|
||||||
params = URI::Params.parse(body)
|
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
|
||||||
LOG.debug { params.inspect }
|
LOG.debug { params.inspect }
|
||||||
|
|
||||||
user_id, account_id = Accounts.auth(params)
|
user_id, account_id = Accounts.auth(params)
|
||||||
|
|
|
@ -2,8 +2,8 @@ require "uri"
|
||||||
|
|
||||||
include CrystalGauntlet
|
include CrystalGauntlet
|
||||||
|
|
||||||
CrystalGauntlet.endpoints["/rateGJStars211.php"] = ->(body : String): String {
|
CrystalGauntlet.endpoints["/rateGJStars211.php"] = ->(context : HTTP::Server::Context): String {
|
||||||
params = URI::Params.parse(body)
|
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
|
||||||
LOG.debug { params.inspect }
|
LOG.debug { params.inspect }
|
||||||
|
|
||||||
level_id = params["levelID"].to_i
|
level_id = params["levelID"].to_i
|
||||||
|
@ -43,8 +43,8 @@ CrystalGauntlet.endpoints["/rateGJStars211.php"] = ->(body : String): String {
|
||||||
return "1"
|
return "1"
|
||||||
}
|
}
|
||||||
|
|
||||||
CrystalGauntlet.endpoints["/rateGJDemon21.php"] = ->(body : String): String {
|
CrystalGauntlet.endpoints["/rateGJDemon21.php"] = ->(context : HTTP::Server::Context): String {
|
||||||
params = URI::Params.parse(body)
|
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
|
||||||
LOG.debug { params.inspect }
|
LOG.debug { params.inspect }
|
||||||
|
|
||||||
level_id = params["levelID"].to_i
|
level_id = params["levelID"].to_i
|
||||||
|
|
|
@ -2,8 +2,8 @@ require "uri"
|
||||||
|
|
||||||
include CrystalGauntlet
|
include CrystalGauntlet
|
||||||
|
|
||||||
CrystalGauntlet.endpoints["/uploadGJLevel21.php"] = ->(body : String): String {
|
CrystalGauntlet.endpoints["/uploadGJLevel21.php"] = ->(context : HTTP::Server::Context): String {
|
||||||
params = URI::Params.parse(body)
|
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
|
||||||
LOG.debug { params.inspect }
|
LOG.debug { params.inspect }
|
||||||
|
|
||||||
# todo: green user fixes? pretty please?
|
# todo: green user fixes? pretty please?
|
||||||
|
|
|
@ -4,8 +4,8 @@ require "crypto/bcrypt/password"
|
||||||
|
|
||||||
include CrystalGauntlet
|
include CrystalGauntlet
|
||||||
|
|
||||||
CrystalGauntlet.endpoints["/likeGJItem211.php"] = ->(body : String): String {
|
CrystalGauntlet.endpoints["/likeGJItem211.php"] = ->(context : HTTP::Server::Context): String {
|
||||||
params = URI::Params.parse(body)
|
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
|
||||||
LOG.debug { params.inspect }
|
LOG.debug { params.inspect }
|
||||||
|
|
||||||
if !params.has_key?("itemID")
|
if !params.has_key?("itemID")
|
||||||
|
|
|
@ -6,8 +6,8 @@ include CrystalGauntlet
|
||||||
|
|
||||||
mappacks_per_page = 10
|
mappacks_per_page = 10
|
||||||
|
|
||||||
CrystalGauntlet.endpoints["/getGJMapPacks21.php"] = ->(body : String): String {
|
CrystalGauntlet.endpoints["/getGJMapPacks21.php"] = ->(context : HTTP::Server::Context): String {
|
||||||
params = URI::Params.parse(body)
|
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
|
||||||
LOG.debug { params.inspect }
|
LOG.debug { params.inspect }
|
||||||
|
|
||||||
page = params["page"].to_i32
|
page = params["page"].to_i32
|
||||||
|
|
|
@ -4,8 +4,8 @@ require "digest/sha256"
|
||||||
|
|
||||||
include CrystalGauntlet
|
include CrystalGauntlet
|
||||||
|
|
||||||
CrystalGauntlet.endpoints["/getGJSongInfo.php"] = ->(body : String): String {
|
CrystalGauntlet.endpoints["/getGJSongInfo.php"] = ->(context : HTTP::Server::Context): String {
|
||||||
params = URI::Params.parse(body)
|
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
|
||||||
LOG.debug { params.inspect }
|
LOG.debug { params.inspect }
|
||||||
|
|
||||||
song_id = params["songID"].to_i32
|
song_id = params["songID"].to_i32
|
||||||
|
|
|
@ -2,8 +2,8 @@ require "uri"
|
||||||
|
|
||||||
include CrystalGauntlet
|
include CrystalGauntlet
|
||||||
|
|
||||||
CrystalGauntlet.endpoints["/uploadGJAccComment20.php"] = ->(body : String): String {
|
CrystalGauntlet.endpoints["/uploadGJAccComment20.php"] = ->(context : HTTP::Server::Context): String {
|
||||||
params = URI::Params.parse(body)
|
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
|
||||||
LOG.debug { params.inspect }
|
LOG.debug { params.inspect }
|
||||||
|
|
||||||
user_id, account_id = Accounts.auth(params)
|
user_id, account_id = Accounts.auth(params)
|
||||||
|
|
|
@ -2,8 +2,8 @@ require "uri"
|
||||||
|
|
||||||
include CrystalGauntlet
|
include CrystalGauntlet
|
||||||
|
|
||||||
CrystalGauntlet.endpoints["/deleteGJAccComment20.php"] = ->(body : String): String {
|
CrystalGauntlet.endpoints["/deleteGJAccComment20.php"] = ->(context : HTTP::Server::Context): String {
|
||||||
params = URI::Params.parse(body)
|
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
|
||||||
LOG.debug { params.inspect }
|
LOG.debug { params.inspect }
|
||||||
|
|
||||||
user_id, account_id = Accounts.auth(params)
|
user_id, account_id = Accounts.auth(params)
|
||||||
|
|
|
@ -4,8 +4,8 @@ include CrystalGauntlet
|
||||||
|
|
||||||
comments_per_page = 10
|
comments_per_page = 10
|
||||||
|
|
||||||
CrystalGauntlet.endpoints["/getGJAccountComments20.php"] = ->(body : String): String {
|
CrystalGauntlet.endpoints["/getGJAccountComments20.php"] = ->(context : HTTP::Server::Context): String {
|
||||||
params = URI::Params.parse(body)
|
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
|
||||||
LOG.debug { params.inspect }
|
LOG.debug { params.inspect }
|
||||||
|
|
||||||
account_id = params["accountID"].to_i
|
account_id = params["accountID"].to_i
|
||||||
|
|
|
@ -2,8 +2,8 @@ require "uri"
|
||||||
|
|
||||||
include CrystalGauntlet
|
include CrystalGauntlet
|
||||||
|
|
||||||
CrystalGauntlet.endpoints["/getGJUserInfo20.php"] = ->(body : String): String {
|
CrystalGauntlet.endpoints["/getGJUserInfo20.php"] = ->(context : HTTP::Server::Context): String {
|
||||||
params = URI::Params.parse(body)
|
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
|
||||||
LOG.debug { params.inspect }
|
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|
|
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|
|
||||||
|
|
|
@ -4,8 +4,8 @@ require "crypto/bcrypt/password"
|
||||||
|
|
||||||
include CrystalGauntlet
|
include CrystalGauntlet
|
||||||
|
|
||||||
CrystalGauntlet.endpoints["/getGJUsers20.php"] = ->(body : String): String {
|
CrystalGauntlet.endpoints["/getGJUsers20.php"] = ->(context : HTTP::Server::Context): String {
|
||||||
params = URI::Params.parse(body)
|
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
|
||||||
LOG.debug { params.inspect }
|
LOG.debug { params.inspect }
|
||||||
|
|
||||||
page = params["page"].to_i
|
page = params["page"].to_i
|
||||||
|
|
|
@ -2,8 +2,8 @@ require "uri"
|
||||||
|
|
||||||
include CrystalGauntlet
|
include CrystalGauntlet
|
||||||
|
|
||||||
CrystalGauntlet.endpoints["/updateGJUserScore22.php"] = ->(body : String): String {
|
CrystalGauntlet.endpoints["/updateGJUserScore22.php"] = ->(context : HTTP::Server::Context): String {
|
||||||
params = URI::Params.parse(body)
|
params = URI::Params.parse(context.request.body.not_nil!.gets_to_end)
|
||||||
LOG.debug { params.inspect }
|
LOG.debug { params.inspect }
|
||||||
|
|
||||||
user_id, account_id = Accounts.auth(params)
|
user_id, account_id = Accounts.auth(params)
|
||||||
|
|
Loading…
Reference in a new issue