forked from LarsFin/Making-a-secure-web-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.rb
40 lines (36 loc) · 1016 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
38
39
40
ENV['DB_ENV'] ||= 'development'
require 'socket'
require_relative 'lib/server/request'
require 'pp'
require_relative 'lib/db/db-connect'
require_relative 'lib/server/middleware'
class Server
attr_reader :middleware
def initialize(port = 3000, app_class)
@server = TCPServer.new(port)
@middleware = Middleware.new(app_class)
end
def run
puts 'Booting up the server..'
puts 'Server booted!'
while(true)
Thread.start(@server.accept) do |socket|
begin
request = Request.new(socket.recv(4096))
# pp request
request.generate_hashes
# resource = request.get_location
res = middleware.get_response(request)
socket.print(res.build)
socket.close
rescue Exception => error
puts 'Error: ' + error.to_s
puts error.backtrace
socket.print middleware.error.build
socket.close
end
end
end
end
end
Server.new(App).run if $PROGRAM_NAME == 'server.rb'