mirror of
				https://github.com/iv-org/invidious.git
				synced 2025-11-03 22:21:55 +00:00 
			
		
		
		
	Kemal: remove APIHandler middleware
This commit is contained in:
		@@ -217,7 +217,6 @@ public_folder "assets"
 | 
			
		||||
 | 
			
		||||
Kemal.config.powered_by_header = false
 | 
			
		||||
add_handler FilteredCompressHandler.new
 | 
			
		||||
add_handler APIHandler.new
 | 
			
		||||
add_handler AuthHandler.new
 | 
			
		||||
add_handler DenyFrame.new
 | 
			
		||||
add_context_storage_type(Array(String))
 | 
			
		||||
 
 | 
			
		||||
@@ -134,74 +134,6 @@ class AuthHandler < Kemal::Handler
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
class APIHandler < Kemal::Handler
 | 
			
		||||
  {% for method in %w(GET POST PUT HEAD DELETE PATCH OPTIONS) %}
 | 
			
		||||
  only ["/api/v1/*"], {{method}}
 | 
			
		||||
  {% end %}
 | 
			
		||||
  exclude ["/api/v1/auth/notifications"], "GET"
 | 
			
		||||
  exclude ["/api/v1/auth/notifications"], "POST"
 | 
			
		||||
 | 
			
		||||
  def call(env)
 | 
			
		||||
    return call_next env unless only_match? env
 | 
			
		||||
 | 
			
		||||
    env.response.headers["Access-Control-Allow-Origin"] = "*"
 | 
			
		||||
 | 
			
		||||
    # Since /api/v1/notifications is an event-stream, we don't want
 | 
			
		||||
    # to wrap the response
 | 
			
		||||
    return call_next env if exclude_match? env
 | 
			
		||||
 | 
			
		||||
    # Here we swap out the socket IO so we can modify the response as needed
 | 
			
		||||
    output = env.response.output
 | 
			
		||||
    env.response.output = IO::Memory.new
 | 
			
		||||
 | 
			
		||||
    begin
 | 
			
		||||
      call_next env
 | 
			
		||||
 | 
			
		||||
      env.response.output.rewind
 | 
			
		||||
 | 
			
		||||
      if env.response.output.as(IO::Memory).size != 0 &&
 | 
			
		||||
         env.response.headers.includes_word?("Content-Type", "application/json")
 | 
			
		||||
        response = JSON.parse(env.response.output)
 | 
			
		||||
 | 
			
		||||
        if fields_text = env.params.query["fields"]?
 | 
			
		||||
          begin
 | 
			
		||||
            JSONFilter.filter(response, fields_text)
 | 
			
		||||
          rescue ex
 | 
			
		||||
            env.response.status_code = 400
 | 
			
		||||
            response = {"error" => ex.message}
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        if env.params.query["pretty"]?.try &.== "1"
 | 
			
		||||
          response = response.to_pretty_json
 | 
			
		||||
        else
 | 
			
		||||
          response = response.to_json
 | 
			
		||||
        end
 | 
			
		||||
      else
 | 
			
		||||
        response = env.response.output.gets_to_end
 | 
			
		||||
      end
 | 
			
		||||
    rescue ex
 | 
			
		||||
      env.response.content_type = "application/json" if env.response.headers.includes_word?("Content-Type", "text/html")
 | 
			
		||||
      env.response.status_code = 500
 | 
			
		||||
 | 
			
		||||
      if env.response.headers.includes_word?("Content-Type", "application/json")
 | 
			
		||||
        response = {"error" => ex.message || "Unspecified error"}
 | 
			
		||||
 | 
			
		||||
        if env.params.query["pretty"]?.try &.== "1"
 | 
			
		||||
          response = response.to_pretty_json
 | 
			
		||||
        else
 | 
			
		||||
          response = response.to_json
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    ensure
 | 
			
		||||
      env.response.output = output
 | 
			
		||||
      env.response.print response
 | 
			
		||||
 | 
			
		||||
      env.response.flush
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
class DenyFrame < Kemal::Handler
 | 
			
		||||
  exclude ["/embed/*"]
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user