-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.rb
37 lines (30 loc) · 939 Bytes
/
server.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
require "socket"
require_relative "response"
require_relative "controller"
require_relative "app/controllers"
require_relative "request"
require_relative "router"
require_relative "config/routes"
class HttpServer
DEFAULT_PORT = 3456
def self.start
config = YAML.load_file("config/config.yml")
port = config["port"]
server = TCPServer.new(port || DEFAULT_PORT)
puts "Server running on http://localhost:#{port}", ""
loop do
Thread.new(server.accept) do |client|
request = Request.new(client).read
case Router.route(request)
in {controller:, action:}
end
response = controller.new(request).send(action)
client.puts response.to_s
puts "Responded with status code: #{response.status}"
puts "Response headers: #{response.headers}"
puts "" # newlines between requests in server console
client.close
end
end
end
end