Skip to content

Instantly share code, notes, and snippets.

@asim
Created August 25, 2010 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asim/550150 to your computer and use it in GitHub Desktop.
Save asim/550150 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'eventmachine'
require 'evma_httpserver'
require 'uri'
class Static < EM::Connection
include EM::HttpServer
def post_init
super
no_environment_strings
end
def process_http_request
@header_processing = false
filename = Dir.pwd + URI.parse(@http_request_uri).path
response = EM::DelegatedHttpResponse.new(self)
if File.exists?(filename)
response.status = 200
response.content = File.readlines(filename,'b')
response.send_response
else
response.status = 404
response.content = "404 Error\n"
response.send_response
end
end
end
EM.run {
EM.epoll
EM.start_server '0.0.0.0', 8081, Static
}
@mudge
Copy link

mudge commented Aug 26, 2010

One thing that differs between this and your node version is the fact that File.readlines is synchronous where fs.readFile is not. It looks like EventMachine doesn't have a straight-forward asynchronous way to read files but it might be worth trying EventMachine::FileStreamer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment