mirror of
				https://gitea.invidious.io/iv-org/shard-kemal.git
				synced 2024-08-15 00:53:36 +00:00 
			
		
		
		
	Middleware ordering (#236)
Improve and correct request middleware Request -> Middleware -> Filter -> Route
This commit is contained in:
		
							parent
							
								
									1b27f5c011
								
							
						
					
					
						commit
						922d6de4d1
					
				
					 6 changed files with 79 additions and 24 deletions
				
			
		|  | @ -29,6 +29,9 @@ module Kemal | |||
|       @error_handler = nil | ||||
|       @always_rescue = true | ||||
|       @server = uninitialized HTTP::Server | ||||
|       @router_included = false | ||||
|       @custom_handler_position = 4 | ||||
|       @default_handlers_setup = false | ||||
|     end | ||||
| 
 | ||||
|     def logger | ||||
|  | @ -43,15 +46,30 @@ module Kemal | |||
|       ssl ? "https" : "http" | ||||
|     end | ||||
| 
 | ||||
|     def clear | ||||
|       @router_included = false | ||||
|       @custom_handler_position = 4 | ||||
|       @default_handlers_setup = false | ||||
|       HANDLERS.clear | ||||
|     end | ||||
| 
 | ||||
|     def handlers | ||||
|       HANDLERS | ||||
|     end | ||||
| 
 | ||||
|     def add_handler(handler : HTTP::Handler) | ||||
|       HANDLERS << handler | ||||
|       setup | ||||
|       HANDLERS.insert @custom_handler_position, handler | ||||
|       @custom_handler_position = @custom_handler_position + 1 | ||||
|     end | ||||
| 
 | ||||
|     def add_filter_handler(handler : HTTP::Handler) | ||||
|       setup | ||||
|       HANDLERS.insert HANDLERS.size - 1, handler | ||||
|     end | ||||
| 
 | ||||
|     def add_ws_handler(handler : HTTP::WebSocketHandler) | ||||
|       setup | ||||
|       HANDLERS << handler | ||||
|     end | ||||
| 
 | ||||
|  | @ -67,10 +85,15 @@ module Kemal | |||
|     end | ||||
| 
 | ||||
|     def setup | ||||
|       setup_init_handler | ||||
|       setup_log_handler | ||||
|       setup_error_handler | ||||
|       setup_static_file_handler | ||||
|       unless @default_handlers_setup && @router_included | ||||
|         setup_init_handler | ||||
|         setup_log_handler | ||||
|         setup_error_handler | ||||
|         setup_static_file_handler | ||||
|         @default_handlers_setup = true | ||||
|         @router_included = true | ||||
|         HANDLERS.insert(HANDLERS.size, Kemal::RouteHandler::INSTANCE) | ||||
|       end | ||||
|     end | ||||
| 
 | ||||
|     private def setup_init_handler | ||||
|  |  | |||
|  | @ -7,7 +7,7 @@ module Kemal::Middleware | |||
|     # This middleware is lazily instantiated and added to the handlers as soon as a call to `after_X` or `before_X` is made. | ||||
|     def initialize | ||||
|       @tree = Radix::Tree(Array(Kemal::Middleware::Block)).new | ||||
|       Kemal.config.add_handler(self) | ||||
|       Kemal.config.add_filter_handler(self) | ||||
|     end | ||||
| 
 | ||||
|     # The call order of the filters is before_all -> before_x -> X -> after_x -> after_all | ||||
|  |  | |||
|  | @ -8,7 +8,9 @@ module Kemal | |||
|     @method : String | ||||
| 
 | ||||
|     def initialize(@method, @path : String, &handler : HTTP::Server::Context -> _) | ||||
|       @handler = ->(context : HTTP::Server::Context) { handler.call(context).to_s } | ||||
|       @handler = ->(context : HTTP::Server::Context) do | ||||
|         handler.call(context).to_s | ||||
|       end | ||||
|     end | ||||
|   end | ||||
| end | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue