mirror of
				https://gitea.invidious.io/iv-org/shard-kemal.git
				synced 2024-08-15 00:53:36 +00:00 
			
		
		
		
	Add 404
This commit is contained in:
		
							parent
							
								
									9a5934811d
								
							
						
					
					
						commit
						58013ba005
					
				
					 4 changed files with 33 additions and 1 deletions
				
			
		|  | @ -5,3 +5,5 @@ crystal: | ||||||
| matrix: | matrix: | ||||||
|   allow_failures: |   allow_failures: | ||||||
|     - crystal: nightly |     - crystal: nightly | ||||||
|  | os: | ||||||
|  |   - osx | ||||||
|  |  | ||||||
|  | @ -102,4 +102,11 @@ describe "Kemal::Handler" do | ||||||
|     response = kemal.call(request) |     response = kemal.call(request) | ||||||
|     response.body.should eq("Skills ruby,crystal") |     response.body.should eq("Skills ruby,crystal") | ||||||
|   end |   end | ||||||
|  | 
 | ||||||
|  |   it "renders 404 on not found" do | ||||||
|  |     kemal = Kemal::Handler.new | ||||||
|  |     request = HTTP::Request.new("GET", "/?message=world") | ||||||
|  |     response = kemal.call(request) | ||||||
|  |     response.body.should eq("hello world") | ||||||
|  |   end | ||||||
| end | end | ||||||
|  |  | ||||||
							
								
								
									
										
											BIN
										
									
								
								src/images/404.png
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								src/images/404.png
									
										
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| After Width: | Height: | Size: 20 KiB | 
|  | @ -6,6 +6,7 @@ class Kemal::Handler < HTTP::Handler | ||||||
| 
 | 
 | ||||||
|   def initialize |   def initialize | ||||||
|     @routes = [] of Route |     @routes = [] of Route | ||||||
|  |     @match = false | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   def call(request) |   def call(request) | ||||||
|  | @ -20,7 +21,7 @@ class Kemal::Handler < HTTP::Handler | ||||||
|   def process_request(request) |   def process_request(request) | ||||||
|     @routes.each do |route| |     @routes.each do |route| | ||||||
|       match = route.match?(request) |       match = route.match?(request) | ||||||
|       if match |       if @match = match | ||||||
|         params = Kemal::ParamParser.new(route, request).parse |         params = Kemal::ParamParser.new(route, request).parse | ||||||
|         context = Context.new(request, params) |         context = Context.new(request, params) | ||||||
|         begin |         begin | ||||||
|  | @ -31,6 +32,28 @@ class Kemal::Handler < HTTP::Handler | ||||||
|         end |         end | ||||||
|       end |       end | ||||||
|     end |     end | ||||||
|  |     unless @match | ||||||
|  |       return HTTP::Response.new(404, not_found) | ||||||
|  |     end | ||||||
|     nil |     nil | ||||||
|   end |   end | ||||||
|  | 
 | ||||||
|  |   def not_found | ||||||
|  |     <<-HTML | ||||||
|  |         <!DOCTYPE html> | ||||||
|  |         <html> | ||||||
|  |         <head> | ||||||
|  |           <style type="text/css"> | ||||||
|  |           body { text-align:center;font-family:helvetica,arial;font-size:22px; | ||||||
|  |             color:#888;margin:20px} | ||||||
|  |           #c {margin:0 auto;width:500px;text-align:left} | ||||||
|  |           </style> | ||||||
|  |         </head> | ||||||
|  |         <body> | ||||||
|  |           <h2>Kemal doesn't know this way.</h2> | ||||||
|  |           <img src="/images/404.png"> | ||||||
|  |         </body> | ||||||
|  |         </html> | ||||||
|  |     HTML | ||||||
|  |   end | ||||||
| end | end | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue