Add static file handler
This commit is contained in:
		
							parent
							
								
									12c6af7d79
								
							
						
					
					
						commit
						21a3c6ad3c
					
				
					 4 changed files with 74 additions and 41 deletions
				
			
		|  | @ -17,8 +17,8 @@ at_exit do | |||
|   config = Kemal.config | ||||
|   logger = Kemal::Logger.new | ||||
|   config.add_handler logger | ||||
|   config.add_handler Kemal::StaticHandler.new("./public") | ||||
|   config.add_handler Kemal::Handler::INSTANCE | ||||
|   config.add_handler HTTP::StaticFileHandler.new("./public") | ||||
| 
 | ||||
|   server = HTTP::Server.new(config.port, config.handlers) | ||||
|   server.ssl = config.ssl | ||||
|  |  | |||
|  | @ -1,6 +1,7 @@ | |||
| require "http/server" | ||||
| require "uri" | ||||
| 
 | ||||
| # Handles the routes | ||||
| class Kemal::Handler < HTTP::Handler | ||||
|   INSTANCE = new | ||||
| 
 | ||||
|  | @ -37,44 +38,4 @@ class Kemal::Handler < HTTP::Handler | |||
|     # Render 404 unless a route matches | ||||
|     return render_404 | ||||
|   end | ||||
| 
 | ||||
|   def render_404 | ||||
|     template = <<-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="/__kemal__/404.png"> | ||||
|         </body> | ||||
|         </html> | ||||
|     HTML | ||||
|     HTTP::Response.new(404, template) | ||||
|   end | ||||
| 
 | ||||
|   def render_500(ex) | ||||
|     template = <<-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 has encountered an error. (500)</h2> | ||||
|           <p>#{ex}</p> | ||||
|         </body> | ||||
|         </html> | ||||
|     HTML | ||||
|     HTTP::Response.error("text/html", template) | ||||
|   end | ||||
| end | ||||
|  |  | |||
							
								
								
									
										32
									
								
								src/kemal/static_handler.cr
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								src/kemal/static_handler.cr
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,32 @@ | |||
| require "http/server" | ||||
| require "ecr/macros" | ||||
| 
 | ||||
| class Kemal::StaticHandler < HTTP::Handler | ||||
| 
 | ||||
|   def initialize(@publicdir) | ||||
|   end | ||||
| 
 | ||||
|   def call(request) | ||||
|     request_path = request.path.not_nil! | ||||
| 
 | ||||
|     # Call next handler for processing | ||||
|     return call_next(request) unless request_path.starts_with? "/public" | ||||
|     file = request_path.split("/public/")[1] if request_path.starts_with?("/public") | ||||
|     file_path = File.expand_path("public/#{file}", Dir.working_directory) | ||||
|     if File.exists?(file_path) | ||||
|       HTTP::Response.new(200, File.read(file_path), HTTP::Headers{"Content-Type": mime_type(file_path)}) | ||||
|     else | ||||
|       call_next(request) | ||||
|     end | ||||
|   end | ||||
| 
 | ||||
|   private def mime_type(path) | ||||
|     case File.extname(path) | ||||
|     when ".txt"          then "text/plain" | ||||
|     when ".htm", ".html" then "text/html" | ||||
|     when ".css"          then "text/css" | ||||
|     when ".js"           then "application/javascript" | ||||
|     else                      "application/octet-stream" | ||||
|     end | ||||
|   end | ||||
| end | ||||
|  | @ -17,3 +17,43 @@ macro render(filename, layout) | |||
|   content = render {{filename}} | ||||
|   render {{layout}} | ||||
| end | ||||
| 
 | ||||
| def render_404 | ||||
|   template = <<-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="/__kemal__/404.png"> | ||||
|       </body> | ||||
|       </html> | ||||
|   HTML | ||||
|   HTTP::Response.new(404, template) | ||||
| end | ||||
| 
 | ||||
| def render_500(ex) | ||||
|   template = <<-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 has encountered an error. (500)</h2> | ||||
|         <p>#{ex}</p> | ||||
|       </body> | ||||
|       </html> | ||||
|   HTML | ||||
|   HTTP::Response.error("text/html", template) | ||||
| end | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue