mirror of
				https://gitea.invidious.io/iv-org/shard-kemal.git
				synced 2024-08-15 00:53:36 +00:00 
			
		
		
		
	Tidy up Kemal.run
This commit is contained in:
		
							parent
							
								
									bc661d58ae
								
							
						
					
					
						commit
						621342170a
					
				
					 1 changed files with 29 additions and 23 deletions
				
			
		
							
								
								
									
										52
									
								
								src/kemal.cr
									
										
									
									
									
								
							
							
						
						
									
										52
									
								
								src/kemal.cr
									
										
									
									
									
								
							|  | @ -31,30 +31,10 @@ module Kemal | |||
|     config.setup | ||||
|     config.port = port if port | ||||
| 
 | ||||
|     unless Kemal.config.error_handlers.has_key?(404) | ||||
|       error 404 do | ||||
|         render_404 | ||||
|       end | ||||
|     end | ||||
| 
 | ||||
|     # Test environment doesn't need to have signal trap, built-in images, and logging. | ||||
|     unless config.env == "test" | ||||
|       Signal::INT.trap do | ||||
|         log "Kemal is going to take a rest!" if config.shutdown_message | ||||
|         Kemal.stop | ||||
|         exit | ||||
|       end | ||||
| 
 | ||||
|       # This route serves the built-in images for not_found and exceptions. | ||||
|       get "/__kemal__/404.png" do |env| | ||||
|         file_path = File.expand_path("lib/kemal/images/404.png", Dir.current) | ||||
| 
 | ||||
|         if File.exists? file_path | ||||
|           send_file env, file_path | ||||
|         else | ||||
|           halt env, 404 | ||||
|         end | ||||
|       end | ||||
|     if config.env != "test" | ||||
|       setup_404 | ||||
|       setup_trap_signal | ||||
|     end | ||||
| 
 | ||||
|     server = config.server ||= HTTP::Server.new(config.handlers) | ||||
|  | @ -96,4 +76,30 @@ module Kemal | |||
|       raise "Kemal is already stopped." | ||||
|     end | ||||
|   end | ||||
| 
 | ||||
|   private def self.setup_404 | ||||
|     unless Kemal.config.error_handlers.has_key?(404) | ||||
|       get "/__kemal__/404.png" do |env| | ||||
|         file_path = File.expand_path("lib/kemal/images/404.png", Dir.current) | ||||
| 
 | ||||
|         if File.exists? file_path | ||||
|           send_file env, file_path | ||||
|         else | ||||
|           halt env, 404 | ||||
|         end | ||||
|       end | ||||
| 
 | ||||
|       error 404 do | ||||
|         render_404 | ||||
|       end | ||||
|     end | ||||
|   end | ||||
| 
 | ||||
|   private def self.setup_trap_signal | ||||
|     Signal::INT.trap do | ||||
|       log "Kemal is going to take a rest!" if Kemal.config.shutdown_message | ||||
|       Kemal.stop | ||||
|       exit | ||||
|     end | ||||
|   end | ||||
| end | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue