Skip to content

Commit

Permalink
Add ability to configure the server with a block
Browse files Browse the repository at this point in the history
  • Loading branch information
soveran committed Jan 13, 2017
1 parent 0d36fa5 commit b16354d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
27 changes: 27 additions & 0 deletions README.md
Expand Up @@ -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
------------

Expand Down
12 changes: 9 additions & 3 deletions src/toro.cr
Expand Up @@ -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

Expand All @@ -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

Expand Down

0 comments on commit b16354d

Please sign in to comment.