mirror of
				https://gitea.invidious.io/iv-org/shard-kemal.git
				synced 2024-08-15 00:53:36 +00:00 
			
		
		
		
	More docs
This commit is contained in:
		
							parent
							
								
									7a772a9301
								
							
						
					
					
						commit
						596452f838
					
				
					 5 changed files with 57 additions and 54 deletions
				
			
		|  | @ -1,4 +1,7 @@ | ||||||
| require "http" | require "http" | ||||||
|  | require "json" | ||||||
|  | require "uri" | ||||||
|  | require "tempfile" | ||||||
| require "./kemal/*" | require "./kemal/*" | ||||||
| require "./kemal/helpers/*" | require "./kemal/helpers/*" | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -1,65 +1,70 @@ | ||||||
| class Kemal::Handler | module Kemal | ||||||
|   include HTTP::Handler |   # `Kemal::Handler` is a subclass of `HTTP::Handler`. | ||||||
|   @@only_routes_tree = Radix::Tree(String).new |   # It adds `only`, `only_match?`, `exclude`, `exclude_match?`. | ||||||
|   @@exclude_routes_tree = Radix::Tree(String).new |   # These methods are useful for custom handlers for conditional execution. | ||||||
|  |   class Handler | ||||||
|  |     include HTTP::Handler | ||||||
|  |     @@only_routes_tree = Radix::Tree(String).new | ||||||
|  |     @@exclude_routes_tree = Radix::Tree(String).new | ||||||
| 
 | 
 | ||||||
|   macro only(paths, method = "GET") |     macro only(paths, method = "GET") | ||||||
|     class_name = {{@type.name}} |     class_name = {{@type.name}} | ||||||
|     {{paths}}.each do |path| |     {{paths}}.each do |path| | ||||||
|       @@only_routes_tree.add "#{class_name}/#{{{method}}.downcase}#{path}", "/#{{{method}}.downcase}#{path}" |       @@only_routes_tree.add "#{class_name}/#{{{method}}.downcase}#{path}", "/#{{{method}}.downcase}#{path}" | ||||||
|     end |     end | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   macro exclude(paths, method = "GET") |     macro exclude(paths, method = "GET") | ||||||
|     class_name = {{@type.name}} |     class_name = {{@type.name}} | ||||||
|     {{paths}}.each do |path| |     {{paths}}.each do |path| | ||||||
|       @@exclude_routes_tree.add "#{class_name}/#{{{method}}.downcase}#{path}", "/#{{{method}}.downcase}#{path}" |       @@exclude_routes_tree.add "#{class_name}/#{{{method}}.downcase}#{path}", "/#{{{method}}.downcase}#{path}" | ||||||
|     end |     end | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   def call(env) |     def call(env) | ||||||
|     call_next(env) |       call_next(env) | ||||||
|   end |     end | ||||||
| 
 | 
 | ||||||
|   # Processes the path based on `only` paths which is a `Array(String)`. |     # Processes the path based on `only` paths which is a `Array(String)`. | ||||||
|   # If the path is not found on `only` conditions the handler will continue processing. |     # If the path is not found on `only` conditions the handler will continue processing. | ||||||
|   # If the path is found in `only` conditions it'll stop processing and will pass the request |     # If the path is found in `only` conditions it'll stop processing and will pass the request | ||||||
|   # to next handler. |     # to next handler. | ||||||
|   # |     # | ||||||
|   # However this is not done automatically. All handlers must inherit from `Kemal::Handler`. |     # However this is not done automatically. All handlers must inherit from `Kemal::Handler`. | ||||||
|   # |     # | ||||||
|   #     class OnlyHandler < Kemal::Handler |     #     class OnlyHandler < Kemal::Handler | ||||||
|   #       only ["/"] |     #       only ["/"] | ||||||
|   # |     # | ||||||
|   #       def call(env) |     #       def call(env) | ||||||
|   #         return call_next(env) unless only_match?(env) |     #         return call_next(env) unless only_match?(env) | ||||||
|   #         puts "If the path is / i will be doing some processing here." |     #         puts "If the path is / i will be doing some processing here." | ||||||
|   #       end |     #       end | ||||||
|   #     end |     #     end | ||||||
|   def only_match?(env) |     def only_match?(env) | ||||||
|     @@only_routes_tree.find(radix_path(env.request.method, env.request.path)).found? |       @@only_routes_tree.find(radix_path(env.request.method, env.request.path)).found? | ||||||
|   end |     end | ||||||
| 
 | 
 | ||||||
|   # Processes the path based on `exclude` paths which is a `Array(String)`. |     # Processes the path based on `exclude` paths which is a `Array(String)`. | ||||||
|   # If the path is not found on `exclude` conditions the handler will continue processing. |     # If the path is not found on `exclude` conditions the handler will continue processing. | ||||||
|   # If the path is found in `exclude` conditions it'll stop processing and will pass the request |     # If the path is found in `exclude` conditions it'll stop processing and will pass the request | ||||||
|   # to next handler. |     # to next handler. | ||||||
|   # |     # | ||||||
|   # However this is not done automatically. All handlers must inherit from `Kemal::Handler`. |     # However this is not done automatically. All handlers must inherit from `Kemal::Handler`. | ||||||
|   # |     # | ||||||
|   #     class ExcludeHandler < Kemal::Handler |     #     class ExcludeHandler < Kemal::Handler | ||||||
|   #       exclude ["/"] |     #       exclude ["/"] | ||||||
|   # |     # | ||||||
|   #       def call(env) |     #       def call(env) | ||||||
|   #         return call_next(env) if exclude_match?(env) |     #         return call_next(env) if exclude_match?(env) | ||||||
|   #         puts "If the path is not / i will be doing some processing here." |     #         puts "If the path is not / i will be doing some processing here." | ||||||
|   #       end |     #       end | ||||||
|   #     end |     #     end | ||||||
|   def exclude_match?(env) |     def exclude_match?(env) | ||||||
|     @@exclude_routes_tree.find(radix_path(env.request.method, env.request.path)).found? |       @@exclude_routes_tree.find(radix_path(env.request.method, env.request.path)).found? | ||||||
|   end |     end | ||||||
| 
 | 
 | ||||||
|   private def radix_path(method : String, path) |     private def radix_path(method : String, path) | ||||||
|     "#{self.class}/#{method.downcase}#{path}" |       "#{self.class}/#{method.downcase}#{path}" | ||||||
|  |     end | ||||||
|   end |   end | ||||||
| end | end | ||||||
|  |  | ||||||
|  | @ -1,6 +1,6 @@ | ||||||
| module Kemal | module Kemal | ||||||
|   # Kemal::InitHandler is the first handler thus initializes the context with default values such as |   # Kemal::InitHandler is the first handler thus initializes the context with default values. | ||||||
|   # Content-Type, X-Powered-By. |   # Such as *Content-Type*, *X-Powered-By* headers. | ||||||
|   class InitHandler |   class InitHandler | ||||||
|     include HTTP::Handler |     include HTTP::Handler | ||||||
|     INSTANCE = new |     INSTANCE = new | ||||||
|  |  | ||||||
|  | @ -1,7 +1,3 @@ | ||||||
| require "json" |  | ||||||
| require "uri" |  | ||||||
| require "tempfile" |  | ||||||
| 
 |  | ||||||
| module Kemal | module Kemal | ||||||
|   # ParamParser parses the request contents including query_params and body |   # ParamParser parses the request contents including query_params and body | ||||||
|   # and converts them into a params hash which you can within the environment |   # and converts them into a params hash which you can within the environment | ||||||
|  |  | ||||||
|  | @ -1,4 +1,3 @@ | ||||||
| # Opening HTTP::Request to add override_method property |  | ||||||
| class HTTP::Request | class HTTP::Request | ||||||
|   property override_method |   property override_method | ||||||
|   property url_params : Hash(String, String)? |   property url_params : Hash(String, String)? | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue