Class: Ikura::Server

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

Constant Summary collapse

TEMPLATE_PATH =
File.join(__dir__, "templates", "ikura.html.erb")
IKURA_POINTS =
[
  [50, 50], [35, 50], [65, 50], [20, 50], [80, 50],
  [50, 15], [35, 22], [65, 22], [20, 35], [80, 35],
  [50, 8], [42, 12], [58, 12], [28, 18], [72, 18],
  [12, 28], [88, 28], [50, 85], [35, 78], [65, 78],
  [20, 65], [80, 65], [10, 50], [90, 50], [72, 62],
  [50, 30], [50, 70], [28, 38], [72, 38], [28, 62],
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port: 8080) ⇒ Server

Returns a new instance of Server.



24
25
26
27
# File 'lib/ikura/server.rb', line 24

def initialize(port: 8080)
  @port = port
  @ikura_count = 0
end

Class Method Details

.start(port: 8080) ⇒ Object



11
12
13
# File 'lib/ikura/server.rb', line 11

def self.start(port: 8080)
  new(port:).run
end

Instance Method Details

#runObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ikura/server.rb', line 29

def run
  server = TCPServer.new(@port)
  $stdout.sync = true
  url = "http://localhost:#{@port}"
  puts "🍣  #{terminal_link(url)}"
  puts "    (Ctrl+C to stop)\n\n"

  loop do
    client = server.accept
    req = parse_request(client)
    next unless req

    puts "#{req[:method]} #{req[:path]}"
    handle(client, req)
    client.close
  rescue => e
    STDERR.puts "Error: #{e}"
    client&.close
  end
end