Advertisement
Guest User

Untitled

a guest
May 26th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. require 'rubygems'
  2. require 'eventmachine'
  3. require 'evma_httpserver'
  4. require 'json'
  5.  
  6. INPUT = 'suggestions.txt'
  7.  
  8. class Handler < EventMachine::Connection
  9. include EventMachine::HttpServer
  10.  
  11. def process_http_request
  12. resp = EventMachine::DelegatedHttpResponse.new( self )
  13.  
  14. matches = @@suggestions.find_all{ |s| s.match(@http_request_uri[1..-1]) }[0..9]
  15.  
  16. resp.status = 200
  17. resp.content_type 'text/json'
  18. resp.content = matches.to_json
  19. resp.send_response
  20. end
  21. end
  22.  
  23. @@suggestions = []
  24. File.open(INPUT, 'r').each { |line| @@suggestions << line.strip }
  25. puts "#{@@suggestions.size} suggestions..."
  26.  
  27. EventMachine::run {
  28. EventMachine.epoll
  29. EventMachine::start_server("0.0.0.0", 8080, Handler)
  30. puts "Listening..."
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement