Skip to content

Commit

Permalink
feat: Prism::Server doesn't have host and port arguments anymore
Browse files Browse the repository at this point in the history
BREAKING CHANGE: This is to comply with fresh standard `HTTP::Server` implementation.

Also added `Prism::Server` specs.

Related to #31
  • Loading branch information
vladfaust committed Aug 16, 2018
1 parent 051d72d commit 3da5e22
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ logger = Logger.new(STDOUT)
log_handler = Prism::LogHandler.new(logger)
handlers = [log_handler, router]
server = Prism::Server.new(handlers, "localhost", 5000, true, logger)
server = Prism::Server.new(handlers, logger)
server.bind_tcp(5000)
server.listen
# INFO -- : Prism::Server is listening on http://localhost:5000...
# INFO -- : Prism::Server is listening on http://127.0.0.1:5000...
```

## Websockets example
Expand Down
26 changes: 26 additions & 0 deletions spec/server_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "./spec_helper"
require "../src/prism/server"

describe Prism::Server do
router = Prism::Router.new do
get "/" do |env|
env.response.print("Hello Prism!")
end
end

server = Prism::Server.new([router])

it "works" do
spawn do
server.bind_tcp(5042)
server.listen
end

sleep(0.5)

response = HTTP::Client.get("http://localhost:5042")
response.body.should eq "Hello Prism!"

server.close
end
end
22 changes: 11 additions & 11 deletions src/prism/server.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ module Prism
#
# log_handler = Prism::LogHandler.new(Logger.new(STDOUT))
#
# server = Prism::Server.new([log_handler, router], "0.0.0.0", 5000)
# server = Prism::Server.new([log_handler, router])
# server.bind_tcp(5000)
# server.listen
#
# # INFO -- : Prism::Server is listening on http://0.0.0.0:5000
# # INFO -- : Prism::Server is listening on http://localhost:5000
# # INFO -- : GET /? 200 61μs
# # INFO -- : GET /foo? 404 166μs
# # INFO -- : Prism::Server is shutting down!
Expand All @@ -32,36 +33,35 @@ module Prism
class Server
def initialize(
handlers : Array(HTTP::Handler),
@host = "0.0.0.0",
@port = 5000,
reuse_port = false,
@logger = ::Logger.new(STDOUT)
)
@tcp_server = HTTP::Server.new(handlers) do |context|
@server = HTTP::Server.new(handlers) do |context|
if action = context.request.action
action.call(context)
else
context.response.status_code = 404
context.response.print("Not Found: #{context.request.path}")
end
end

@tcp_server.bind_tcp(@host, @port)
end

def listen
# It's simpler than handling "not binded" case here
@logger.info(
"Prism::Server is listening on " +
"http://#{@host}:#{@port}".colorize(:light_gray).mode(:bold).to_s
)
"http://#{addresses.first}".colorize(:light_gray).mode(:bold).to_s +
"..."
) if addresses.any?

Signal::INT.trap do
puts "\n"
@logger.info("Prism::Server is shutting down!")
exit
end

@tcp_server.listen
@server.listen
end

forward_missing_to @server
end
end

0 comments on commit 3da5e22

Please sign in to comment.