diff --git a/README.md b/README.md new file mode 100644 index 0000000..c4a78d2 --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +### TODO + +- Use Redis to cache replies and add a new key to report for how much time the JSON is going to be stored in cache (**Already started, but I need to fix it!!**) +- Add other endpoints from api.ivr.fi +- CAPTURE MORE THAN 1 PARAM `?login=fuck,fuck` + +### NOT SO IMPORTANT TODO + +- Rate limiting (can be done in the reverse proxy side) +- Better shitcode +- STOP USING KEMAL FOR SIMPLE THINGS! + + +### Suggestions + +3:45 RyanPotat: add team \ No newline at end of file diff --git a/shard.lock b/shard.lock index 0805cb9..1fac801 100644 --- a/shard.lock +++ b/shard.lock @@ -12,7 +12,15 @@ shards: git: https://github.com/kemalcr/kemal.git version: 1.5.0 + pool: + git: https://github.com/ysbaddaden/pool.git + version: 0.2.4 + radix: git: https://github.com/luislavena/radix.git version: 0.4.1 + redis: + git: https://github.com/stefanwille/crystal-redis.git + version: 2.9.1 + diff --git a/shard.yml b/shard.yml index 3974d41..5175ae6 100644 --- a/shard.yml +++ b/shard.yml @@ -8,6 +8,8 @@ targets: dependencies: kemal: github: kemalcr/kemal + redis: + github: stefanwille/crystal-redis # authors: diff --git a/src/api/users.cr b/src/api/users.cr index e2d9921..80f9f4f 100644 --- a/src/api/users.cr +++ b/src/api/users.cr @@ -17,9 +17,9 @@ module Users def self.checkBanned(gql) # puts "checkBanned: #{gql}" # puts (typeof(gql)) - # if gql["userResultByLogin"] + # if gql["userResultByLogin"] # return false - # if gql["userResultByLogin"]["result"]? + # if gql["userResultByLogin"]["result"]? # return true # else # return false @@ -42,17 +42,17 @@ module Users # return false # end # return false - if GqlAPI.urb().includes? "login" + if GqlAPI.urb.includes? "login" if gql["userResultByLogin"]["reason"]? - return true + return gql["userResultByLogin"]["reason"] else - return false + return nil end - elsif GqlAPI.urb().includes? "id" + elsif GqlAPI.urb.includes? "id" if gql["userResultByID"]["reason"]? - return true + return gql["userResultByID"]["reason"] else - return false + return nil end end end @@ -80,18 +80,38 @@ module Users # spawn do # a.send fetchGQL(id.to_s) # end + login = params["login"]? + id = params["id"]? + + if login && (json = REDIS_DB.get(login)) + puts (JSON.parse(json)) + return json + elsif id && (json = REDIS_DB.get(id)) + return json + end + + # puts REDIS_DB.get(params["login"]?) + # puts REDIS_DB.get(params["id"]?) + # if (json = (REDIS_DB.get(params["login"]?.to_s))) || (json = (REDIS_DB.get(params["id"]?.to_s))) + # if ((json = REDIS_DB.get(id)) + # puts json + # return json + # end gql = GqlAPI.query(params) + panels = gql["user"]["panels"] banned = checkBanned(gql) - # puts banned - # puts ("urb #{GqlAPI.urb()}") - # pp gql + # puts(banned) json_data = [ { - "banned" => banned, - "reason" => nil, + "cache" => { + "isCached" => false, + "expireTime" => nil : Int32, + }, + "banned" => false, + "reason" => banned, "displayName" => gql["user"]["displayName"], "login" => gql["user"]["login"], "id" => gql["user"]["id"], @@ -121,11 +141,15 @@ module Users "panels" => panels, }, ] + if banned != nil + json_data[0]["banned"] = true + end - # if json_data[0]["banned"].nil? - # puts "test" - # json_data[0]["reason"] = gql["userResultByID"]["reason"] - # end + if id + REDIS_DB.set(id, json_data.to_json, ex: 5) + else + REDIS_DB.set(login.to_s, json_data.to_json, ex: 5) + end return json_data.to_json end diff --git a/src/config.cr b/src/config.cr index 62adc21..fca5d05 100644 --- a/src/config.cr +++ b/src/config.cr @@ -9,6 +9,9 @@ class Config property gqlClientID : String? property apiEndpoint : String? property gqlEndpoint : String? + property redis_url : String? + property redis_socket : String? + property port : Int32 = 8080 def self.load config_file = "config/config.yml" @@ -25,16 +28,6 @@ class Config exit(1) end - if config.apiEndpoint.to_s.empty? - puts "Config: 'apiEndpoint' is required/can't be empty" - exit(1) - end - - if config.gqlEndpoint.to_s.empty? - puts "Config: 'apiEndpoint' is required/can't be empty" - exit(1) - end - return config end end diff --git a/src/main.cr b/src/main.cr index 3da8964..2dc0fe2 100644 --- a/src/main.cr +++ b/src/main.cr @@ -5,10 +5,14 @@ require "uri" require "./config" require "./api/*" require "./twitchapi/*" +require "redis" CONFIG = Config.load -# puts("helixOAuth: #{CONFIG.helixOAuth}") -# puts("helixClientID: #{CONFIG.helixClientID}") +Kemal.config.port = CONFIG.port +REDIS_DB = Redis::PooledClient.new(unixsocket: CONFIG.redis_socket || nil, url: CONFIG.redis_url || nil) +if REDIS_DB.ping + puts "Connected to redis" +end before_all "/twitch/*" do |env| env.response.content_type = "application/json" @@ -46,4 +50,8 @@ get "/twitch/user" do |env| end end +{% if flag?(:release) || flag?(:production) %} + Kemal.config.env = "production" if !ENV.has_key?("KEMAL_ENV") +{% end %} + Kemal.run diff --git a/src/twitchapi/helix.cr b/src/twitchapi/helix.cr index fa07e7e..5fe3918 100644 --- a/src/twitchapi/helix.cr +++ b/src/twitchapi/helix.cr @@ -14,7 +14,7 @@ module HelixAPI response = HTTP::Client.get(endpoint, headers: @@headers) - puts response.body + # puts response.body if response.success? return (response.body) else