From a67a8508cf8adb8e0a7ec2a1a3fdf62661bafa8a Mon Sep 17 00:00:00 2001 From: Sdogruyol Date: Tue, 8 Dec 2015 22:15:13 +0200 Subject: [PATCH] Add worker support (experimental) --- src/kemal.cr | 13 +++++++++++-- src/kemal/config.cr | 2 ++ src/kemal/route.cr | 1 - src/kemal/view.cr | 2 +- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/kemal.cr b/src/kemal.cr index 717adcf..37bd940 100644 --- a/src/kemal.cr +++ b/src/kemal.cr @@ -9,6 +9,9 @@ at_exit do opts.on("-e ", "--environment ", "environment") do |env| Kemal.config.env = env end + opts.on("-w VALUE", "--workers", "workers") do |workers| + Kemal.config.workers = workers.to_i + end end config = Kemal.config @@ -17,7 +20,7 @@ at_exit do config.add_handler Kemal::Handler::INSTANCE config.add_handler HTTP::StaticFileHandler.new("./public") - server = HTTP::Server.new("0.0.0.0", config.port, config.handlers) + server = HTTP::Server.new(config.port, config.handlers) server.ssl = config.ssl logger.write "[#{config.env}] Kemal is ready to lead at #{config.scheme}://0.0.0.0:#{config.port}\n" @@ -37,5 +40,11 @@ at_exit do File.read(file_path) end - server.listen + workers = Kemal.config.workers + if workers > 1 + logger.write "Kemal is starting with #{workers} workers!" + server.listen_fork workers: workers + else + server.listen + end end diff --git a/src/kemal/config.cr b/src/kemal/config.cr index 8307525..69b8b37 100644 --- a/src/kemal/config.cr +++ b/src/kemal/config.cr @@ -5,10 +5,12 @@ module Kemal property ssl property port property env + property workers def initialize @port = 3000 @env = "development" unless @env + @workers = 1 end def scheme diff --git a/src/kemal/route.cr b/src/kemal/route.cr index de983ca..bcac004 100644 --- a/src/kemal/route.cr +++ b/src/kemal/route.cr @@ -11,7 +11,6 @@ class Kemal::Route def match?(request) check_for_method_override!(request) - return nil unless request.override_method == @method components = request.path.not_nil!.split "/" return nil unless components.size == @components.size diff --git a/src/kemal/view.cr b/src/kemal/view.cr index 813cf98..2475675 100644 --- a/src/kemal/view.cr +++ b/src/kemal/view.cr @@ -1,6 +1,6 @@ # Kemal render uses built-in ECR to render methods. -## Usage +# # Usage # get '/' do # render 'hello.ecr' # end