Class: Gitem::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/gitem/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, port = 8000) ⇒ Server

Returns a new instance of Server.



7
8
9
10
11
# File 'lib/gitem/server.rb', line 7

def initialize(root, port = 8000)
  @root = root
  @port = port
  @url = "http://localhost:#{port}"
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



5
6
7
# File 'lib/gitem/server.rb', line 5

def url
  @url
end

Instance Method Details

#startObject



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/gitem/server.rb', line 13

def start
  puts "🌐 Server running at #{@url}"
  puts "   Press Ctrl+C to stop\n\n"
  server = WEBrick::HTTPServer.new(
    Port: @port, DocumentRoot: @root,
    Logger: WEBrick::Log.new($stderr, WEBrick::Log::WARN), AccessLog: []
  )
  trap("INT") { server.shutdown }
  trap("TERM") { server.shutdown }
  server.start
end