mirror of
				https://gitea.invidious.io/iv-org/shard-kemal.git
				synced 2024-08-15 00:53:36 +00:00 
			
		
		
		
	Merge pull request #52 from f/master
Implement SSL handling, move option parser to another file to maintain
This commit is contained in:
		
						commit
						3e27e15e29
					
				
					 3 changed files with 56 additions and 13 deletions
				
			
		
							
								
								
									
										16
									
								
								src/kemal.cr
									
										
									
									
									
								
							
							
						
						
									
										16
									
								
								src/kemal.cr
									
										
									
									
									
								
							|  | @ -1,21 +1,11 @@ | ||||||
| require "option_parser" |  | ||||||
| require "./kemal/*" | require "./kemal/*" | ||||||
| require "./kemal/middleware/*" | require "./kemal/middleware/*" | ||||||
| 
 | 
 | ||||||
| at_exit do | at_exit do | ||||||
|   OptionParser.parse! do |opts| |   Kemal::CLI.new | ||||||
|     opts.on("-p ", "--port ", "port") do |opt_port| |  | ||||||
|       Kemal.config.port = opt_port.to_i |  | ||||||
|     end |  | ||||||
|     opts.on("-e ", "--environment ", "environment") do |env| |  | ||||||
|       Kemal.config.env = env |  | ||||||
|     end |  | ||||||
|     opts.on("-b", "--bind", "host binding") do |host_binding| |  | ||||||
|       Kemal.config.host_binding = host_binding |  | ||||||
|     end |  | ||||||
|   end |  | ||||||
| 
 | 
 | ||||||
|   config = Kemal.config |   config = Kemal.config | ||||||
|  | 
 | ||||||
|   logger = Kemal::Logger.new |   logger = Kemal::Logger.new | ||||||
|   config.add_handler logger |   config.add_handler logger | ||||||
|   config.add_handler Kemal::StaticFileHandler.new(config.public_folder) |   config.add_handler Kemal::StaticFileHandler.new(config.public_folder) | ||||||
|  | @ -37,7 +27,7 @@ at_exit do | ||||||
|     image = env.params["image"] |     image = env.params["image"] | ||||||
|     file_path = File.expand_path("libs/kemal/images/#{image}", Dir.current) |     file_path = File.expand_path("libs/kemal/images/#{image}", Dir.current) | ||||||
|     env.add_header "Content-Type", "application/octet-stream" |     env.add_header "Content-Type", "application/octet-stream" | ||||||
|     File.read(file_path) |     File.read(file_path) if File.exists? file_path | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   server.listen |   server.listen | ||||||
|  |  | ||||||
							
								
								
									
										35
									
								
								src/kemal/cli.cr
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								src/kemal/cli.cr
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,35 @@ | ||||||
|  | require "option_parser" | ||||||
|  | 
 | ||||||
|  | module Kemal | ||||||
|  |   class CLI | ||||||
|  |     def initialize | ||||||
|  |       ssl = nil | ||||||
|  |       OptionParser.parse! do |opts| | ||||||
|  |         opts.on("-b HOST", "--bind HOST", "HTTP host to bind (defaults to 0.0.0.0)") do |host_binding| | ||||||
|  |           Kemal.config.host_binding = host_binding | ||||||
|  |         end | ||||||
|  |         opts.on("-p PORT", "--port PORT", "HTTP port to listen connections (defaults to 3000)") do |opt_port| | ||||||
|  |           Kemal.config.port = opt_port.to_i | ||||||
|  |         end | ||||||
|  |         opts.on("-e ENV", "--environment ENV", "Running environment [development, production] (defaults to development). Set `production` to boost performance") do |env| | ||||||
|  |           Kemal.config.env = env | ||||||
|  |         end | ||||||
|  |         opts.on("-s", "--ssl", "Enables SSL") do | ||||||
|  |           ssl = Kemal::Middleware::SSL.new | ||||||
|  |         end | ||||||
|  |         opts.on("--ssl-key-file FILE", "SSL key file") do |key_file| | ||||||
|  |           ssl.not_nil!.set_key_file key_file | ||||||
|  |         end | ||||||
|  |         opts.on("--ssl-cert-file FILE", "SSL certificate file") do |cert_file| | ||||||
|  |           ssl.not_nil!.set_cert_file cert_file | ||||||
|  |         end | ||||||
|  |         opts.on("-h", "--help", "Shows this help") do | ||||||
|  |           puts opts | ||||||
|  |           exit 0 | ||||||
|  |         end | ||||||
|  |       end | ||||||
|  | 
 | ||||||
|  |       Kemal.config.ssl = ssl.not_nil!.context if ssl | ||||||
|  |     end | ||||||
|  |   end | ||||||
|  | end | ||||||
							
								
								
									
										18
									
								
								src/kemal/middleware/ssl.cr
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								src/kemal/middleware/ssl.cr
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,18 @@ | ||||||
|  | module Kemal::Middleware | ||||||
|  |   class SSL | ||||||
|  | 
 | ||||||
|  |     getter context | ||||||
|  | 
 | ||||||
|  |     def initialize | ||||||
|  |       @context = OpenSSL::SSL::Context.new | ||||||
|  |     end | ||||||
|  | 
 | ||||||
|  |     def set_key_file(key_file) | ||||||
|  |       @context.private_key = key_file | ||||||
|  |     end | ||||||
|  | 
 | ||||||
|  |     def set_cert_file(cert_file) | ||||||
|  |       @context.certificate_chain = cert_file | ||||||
|  |     end | ||||||
|  |   end | ||||||
|  | end | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue