Class: Fingerer::Server

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

Class Method Summary collapse

Class Method Details

.start(options = {}) ⇒ Object



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
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fingerer.rb', line 12

def self.start(options = {})
  options[:listen] ||= '0.0.0.0'
  options[:port] ||= 79

  Debug.info("Booting Fingerer")

  server = ::TCPServer.new(options[:listen], options[:port])

  Debug.info("Listening on tcp://#{options[:listen]}:#{options[:port]}");
  Debug.info("Ctrl-C to shutdown server\n");

  loop do
    Thread.start(server.accept) do |client|
      # start timer
      start_time = Time.now
      remote_ip = client.peeraddr[-1]
      username = client.gets.strip

      Debug.info("Started request for \"#{username}\" from #{remote_ip}")

      begin
        user = ::Octokit.user(username)

        client.puts(response(user))
      rescue Octokit::NotFound
        client.puts("No such user \"#{username}\"")
      rescue => e
        client.puts("Something went wrong")
        Debug.error(e.message)
      end

      client.close

      # end timer
      end_time = Time.now

      Debug.info("Completed request for \"#{username}\" from #{remote_ip} in #{(end_time.to_ms - start_time.to_ms)}ms")
    end
  end
end