diff --git a/README.md b/README.md index 269568c..dc80181 100644 --- a/README.md +++ b/README.md @@ -178,6 +178,33 @@ order to use this helper (adding `require "json"` to your app should suffice). The lower level `write` method writes a string to the response object. It is used internally by `text` and `json`. +Running the server +------------------ + +If `App` is an instance of `Toro`, then you can start the server +by calling `App.run`. You can pass any options you would use with +the `HTTP::Server` constructor from the standard library. + +For example, you can start the server on port 80: + +```crystal +App.run(80) +``` + +Or you can further configure server by using a block. The following +example shows how to configure SSL certificates: + +```crystal +App.run(443) do |server| + ssl = OpenSSL::SSL::Context::Server.new + ssl.private_key = "path/to/private_key" + ssl.certificate_chain = "path/to/certificate_chain" + server.tls = ssl +end +``` + +Refer to Crystal's documentation for more options. + Status codes ------------ diff --git a/src/toro.cr b/src/toro.cr index 82199f1..3056e8b 100644 --- a/src/toro.cr +++ b/src/toro.cr @@ -31,8 +31,12 @@ module Toro new(context, path).call end - def self.run(port = 8080) - server = HTTP::Server.new(port) do |context| + def self.run(*args) + run(*args) {} + end + + def self.run(*args, &block) + server = HTTP::Server.new(*args) do |context| call(context) end @@ -41,7 +45,9 @@ module Toro exit end - puts "#{name} - Listening on port #{port}" + yield server + + puts "#{name} - Listening on port #{server.port}" server.listen end