pretty logs :)

This commit is contained in:
Jill 2023-01-02 13:59:37 +03:00
parent ae2901e859
commit c6b7fe3dfb
20 changed files with 77 additions and 37 deletions

View File

@ -1,3 +1,5 @@
DATABASE_URL=sqlite3://./crystalgauntlet.db
# can either be http:// or unix:// for a unix socket
LISTEN_ON=http://127.0.0.1:8080
LISTEN_ON=http://127.0.0.1:8080
# try DEBUG or TRACE
LOG_LEVEL=INFO

View File

@ -5,6 +5,7 @@ require "sqlite3"
require "migrate"
require "dotenv"
require "toml"
require "colorize"
require "./enums"
require "./lib/hash"
@ -20,6 +21,7 @@ module CrystalGauntlet
VERSION = "0.1.0"
CONFIG = TOML.parse(File.read("./config.toml"))
LOG = ::Log.for("crystal-gauntlet")
def config_get(key : String)
this = CONFIG
@ -42,6 +44,45 @@ module CrystalGauntlet
@@endpoints
end
def severity_color(severity : Log::Severity) : Colorize::Object
case severity
when .trace?
Colorize.with.dark_gray
when .debug?
Colorize.with.dark_gray
when .info?
Colorize.with.cyan
when .notice?
Colorize.with.cyan
when .warn?
Colorize.with.yellow
when .error?
Colorize.with.red
when .fatal?
Colorize.with.light_red
else
Colorize.with.white
end
end
struct CrystalGauntletFormat < Log::StaticFormatter
def run
Colorize.with.light_gray.dim.surround(@io) do
timestamp
end
string " "
severity_color(@entry.severity).surround(@io) do
@entry.severity.label.rjust(@io, 6)
end
string ": "
Colorize.with.white.surround(@io) do
source
end
string " - "
message
end
end
class GDHandler
include HTTP::Handler
@ -56,6 +97,7 @@ module CrystalGauntlet
if CrystalGauntlet.endpoints.has_key?(path) && body
func = CrystalGauntlet.endpoints[path]
value = func.call(body.gets_to_end)
LOG.debug { "-> " + value }
context.response.content_type = "text/plain"
context.response.print value
else
@ -81,10 +123,9 @@ module CrystalGauntlet
server.bind_unix(listen_on.to_s.sub("unix://",""))
end
# for debugging
#Songs.reupload("https://soundcloud.com/koraii/encroachingdark", 123456)
Log.setup_from_env(backend: Log::IOBackend.new(formatter: CrystalGauntletFormat))
puts "Listening on #{listen_on.to_s}"
LOG.notice { "Listening on #{listen_on.to_s}" }
server.listen
end
end

View File

@ -6,7 +6,7 @@ include CrystalGauntlet
CrystalGauntlet.endpoints["/accounts/registerGJAccount.php"] = ->(body : String): String {
params = URI::Params.parse(body)
puts params.inspect
LOG.debug { params.inspect }
if config_get("accounts.allow_registration").as(Bool | Nil) == false
return "-1"

View File

@ -4,7 +4,7 @@ include CrystalGauntlet
CrystalGauntlet.endpoints["/updateGJUserScore22.php"] = ->(body : String): String {
params = URI::Params.parse(body)
puts params.inspect
LOG.debug { params.inspect }
user_id, account_id = Accounts.auth(params)
if !(user_id && account_id)

View File

@ -4,7 +4,7 @@ include CrystalGauntlet
CrystalGauntlet.endpoints["/uploadGJComment21.php"] = ->(body : String): String {
params = URI::Params.parse(body)
puts params.inspect
LOG.debug { params.inspect }
user_id, account_id = Accounts.auth(params)
if !(user_id && account_id)

View File

@ -5,7 +5,7 @@ include CrystalGauntlet
CrystalGauntlet.endpoints["/downloadGJLevel22.php"] = ->(body : String): String {
params = URI::Params.parse(body)
puts params.inspect
LOG.debug { params.inspect }
response = ""

View File

@ -8,7 +8,7 @@ levels_per_page = 10
CrystalGauntlet.endpoints["/getGJLevels21.php"] = ->(body : String): String {
params = URI::Params.parse(body)
puts params.inspect
LOG.debug { params.inspect }
# where [...]
queryParams = ["unlisted = 0"] # don't leave the default empty!!
@ -69,7 +69,6 @@ CrystalGauntlet.endpoints["/getGJLevels21.php"] = ->(body : String): String {
when "-1"
queryParams << "difficulty is null and community_difficulty is null" # NA
when "-2"
puts "demon :)"
case params["demonFilter"]?
when "1"
queryParams << "demon_difficulty = #{DemonDifficulty::Easy.value}"
@ -135,7 +134,7 @@ CrystalGauntlet.endpoints["/getGJLevels21.php"] = ->(body : String): String {
# todo: switch join users to left join to avoid losing levels to the shadow realm after a user vanishes
query_base = "from levels join users on levels.user_id = users.id left join map_pack_links on map_pack_links.level_id = levels.id #{where_str} order by #{order}"
puts query_base
LOG.debug { "query: #{query_base}" }
level_count = DATABASE.scalar("select count(*) #{query_base}").as(Int64)
@ -216,7 +215,7 @@ CrystalGauntlet.endpoints["/getGJLevels21.php"] = ->(body : String): String {
searchMeta = "#{level_count}:#{page_offset}:#{levels_per_page}"
res = [results.join("|"), users.join("|"), songs.join("~:~"), searchMeta, CrystalGauntlet::Hashes.gen_multi(hash_data)].join("#")
puts res
LOG.debug { res }
res
}

View File

@ -4,7 +4,7 @@ include CrystalGauntlet
CrystalGauntlet.endpoints["/getGJLevelScores211.php"] = ->(body : String): String {
params = URI::Params.parse(body)
puts params.inspect
LOG.debug { params.inspect }
account_id = Accounts.get_account_id_from_params(params)
if !account_id || !Accounts.verify_gjp(account_id, params["gjp"])
@ -23,9 +23,7 @@ CrystalGauntlet.endpoints["/getGJLevelScores211.php"] = ->(body : String): Strin
attempts = params["s1"].to_i - 8354
clicks = params["s2"].to_i - 3991
time = params["s3"].to_i - 4085
# todo: fix
progress = String.new(XorCrypt.encrypt_string(GDBase64.decode_string(params["s6"]), "41274"))
puts progress
coins = params["s9"].to_i - 5819
if coins > 3 || coins < 0
return "-1"

View File

@ -4,7 +4,7 @@ include CrystalGauntlet
CrystalGauntlet.endpoints["/rateGJStars211.php"] = ->(body : String): String {
params = URI::Params.parse(body)
puts params.inspect
LOG.debug { params.inspect }
level_id = params["levelID"].to_i
stars = params["stars"].to_i
@ -45,7 +45,7 @@ CrystalGauntlet.endpoints["/rateGJStars211.php"] = ->(body : String): String {
CrystalGauntlet.endpoints["/rateGJDemon21.php"] = ->(body : String): String {
params = URI::Params.parse(body)
puts params.inspect
LOG.debug { params.inspect }
level_id = params["levelID"].to_i
rating = params["rating"].to_i

View File

@ -4,7 +4,7 @@ include CrystalGauntlet
CrystalGauntlet.endpoints["/uploadGJLevel21.php"] = ->(body : String): String {
params = URI::Params.parse(body)
puts params.inspect
LOG.debug { params.inspect }
# todo: green user fixes? pretty please?
user_id, account_id = Accounts.auth(params)

View File

@ -6,7 +6,7 @@ include CrystalGauntlet
CrystalGauntlet.endpoints["/likeGJItem211.php"] = ->(body : String): String {
params = URI::Params.parse(body)
puts params.inspect
LOG.debug { params.inspect }
if !params.has_key?("itemID")
return "-1"

View File

@ -6,12 +6,12 @@ include CrystalGauntlet
CrystalGauntlet.endpoints["/getGJMapPacks21.php"] = ->(body : String): String {
params = URI::Params.parse(body)
puts params.inspect
LOG.debug { params.inspect }
page = params["page"].to_i32
properties = Hash(Int32, Hash(String, String)).new
levels = Hash(Int32, Array(Int32)).new
DATABASE.query("select map_packs.id, map_packs.name, map_packs.stars, map_packs.coins, map_packs.difficulty, map_packs.col1, map_packs.col2, map_pack_links.level_id from map_packs join map_pack_links on map_packs.id = map_pack_links.mappack_id order by map_pack_links.idx limit 10 offset #{page * 10}") do |rs|
rs.each do
id = rs.read(Int32)
@ -28,9 +28,9 @@ CrystalGauntlet.endpoints["/getGJMapPacks21.php"] = ->(body : String): String {
end
if !properties.has_key?(id)
properties[id] = {
"name" => name,
"stars" => stars.to_s,
properties[id] = {
"name" => name,
"stars" => stars.to_s,
"coins" => coins.to_s,
"difficulty" => difficulty.to_s,
"col1" => col1,

View File

@ -12,7 +12,7 @@ end
CrystalGauntlet.endpoints["/getGJSongInfo.php"] = ->(body : String): String {
params = URI::Params.parse(body)
puts params.inspect
LOG.debug { params.inspect }
song_id = params["songID"].to_i32

View File

@ -4,7 +4,7 @@ include CrystalGauntlet
CrystalGauntlet.endpoints["/uploadGJAccComment20.php"] = ->(body : String): String {
params = URI::Params.parse(body)
puts params.inspect
LOG.debug { params.inspect }
user_id, account_id = Accounts.auth(params)
if !(user_id && account_id)

View File

@ -4,7 +4,7 @@ include CrystalGauntlet
CrystalGauntlet.endpoints["/deleteGJAccComment20.php"] = ->(body : String): String {
params = URI::Params.parse(body)
puts params.inspect
LOG.debug { params.inspect }
user_id, account_id = Accounts.auth(params)
if !(user_id && account_id)

View File

@ -6,7 +6,7 @@ comments_per_page = 10
CrystalGauntlet.endpoints["/getGJAccountComments20.php"] = ->(body : String): String {
params = URI::Params.parse(body)
puts params.inspect
LOG.debug { params.inspect }
account_id = params["accountID"].to_i

View File

@ -6,7 +6,7 @@ include CrystalGauntlet
CrystalGauntlet.endpoints["/getGJUserInfo20.php"] = ->(body : String): String {
params = URI::Params.parse(body)
puts 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|
if rs.move_next

View File

@ -6,7 +6,7 @@ include CrystalGauntlet
CrystalGauntlet.endpoints["/getGJUsers20.php"] = ->(body : String): String {
params = URI::Params.parse(body)
puts params.inspect
LOG.debug { params.inspect }
page = params["page"].to_i
results = [] of String
@ -49,6 +49,6 @@ CrystalGauntlet.endpoints["/getGJUsers20.php"] = ->(body : String): String {
amount = DATABASE.scalar("select count(*) from users where id = ? or username like ?", params["str"], username)
response = [results.join("|"), "#{amount}:#{page * 10}:10"].join("#")
response
}

View File

@ -6,7 +6,7 @@ include CrystalGauntlet
CrystalGauntlet.endpoints["/updateGJUserScore22.php"] = ->(body : String): String {
params = URI::Params.parse(body)
puts params.inspect
LOG.debug { params.inspect }
user_id, account_id = Accounts.auth(params)
if !(user_id && account_id)

View File

@ -68,7 +68,7 @@ module CrystalGauntlet::Songs
# will raise errors
def fetch_song_metadata(url : String) : SongMetadata
puts "getting metadata for #{url}"
LOG.info { "getting metadata for #{url}" }
output = IO::Memory.new
# todo: ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️ LOOK OUT FOR SHELL INJECTION BULLSHIT!!!!!!!!!!!!!!!!!!
@ -112,7 +112,7 @@ module CrystalGauntlet::Songs
# returns nil if song should be disabled
# throws if something failed
def fetch_song(song_id : Int32, get_download = false) : Tuple(String, Int32, String, Int32 | Nil, String | Nil) | Nil
puts "fetching #{song_id}"
LOG.debug { "fetching #{song_id}" }
if !config_get("songs.allow_custom_songs").as?(Bool)
return nil
end
@ -151,8 +151,8 @@ module CrystalGauntlet::Songs
begin
metadata = fetch_song_metadata(url.not_nil!)
rescue err
puts "ran into error fetching metadata: #{err}; disabling song"
puts err.inspect
LOG.warn { "ran into error fetching metadata: #{err}; disabling song" }
LOG.warn { err.inspect }
if song_exists
DATABASE.exec("update songs set disabled=1 where id = ?", song_id)
else
@ -177,7 +177,7 @@ module CrystalGauntlet::Songs
end
end
puts metadata.inspect
LOG.debug { metadata.inspect }
# do checks to make sure this is a valid song
max_duration = config_get("songs.sources.max_duration").as?(Int64)