Created
August 25, 2010 19:50
-
-
Save asim/550150 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
One thing that differs between this and your node version is the fact that
File.readlines
is synchronous wherefs.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.