Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@clyfe
Created August 9, 2011 11:14
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 clyfe/1133786 to your computer and use it in GitHub Desktop.
Save clyfe/1133786 to your computer and use it in GitHub Desktop.
Jetty7 jruby-rack embeded server
# Put this in Rails.root and the specified jars in the Rails.root/jars folder
#
# # run
# > jruby run.rb
#
require 'rubygems'
require 'java'
require 'jruby-rack'
# log to stdout for now
java.lang.System.setProperty(
"org.eclipse.jetty.util.log.class",
"org.eclipse.jetty.util.log.StdErrLog"
)
# jetty_jars/jetty-continuation-7.4.5.v20110725.jar
# jetty_jars/jetty-http-7.4.5.v20110725.jar
# jetty_jars/jetty-io-7.4.5.v20110725.jar
# jetty_jars/jetty-security-7.4.5.v20110725.jar
# jetty_jars/jetty-server-7.4.5.v20110725.jar
# jetty_jars/jetty-servlet-7.4.5.v20110725.jar
# jetty_jars/jetty-util-7.4.5.v20110725.jar
# jetty_jars/servlet-api-2.5.jar
jars_dir = File.join(File.dirname(__FILE__), 'jetty_jars')
for jar in Dir["#{jars_dir}/*"]
require jar
end
class HttpServer
include_class 'org.eclipse.jetty.server.Server'
include_class 'org.eclipse.jetty.servlet.ServletContextHandler'
include_class 'org.eclipse.jetty.servlet.ServletHolder'
include_class 'org.eclipse.jetty.server.nio.SelectChannelConnector'
include_class 'org.eclipse.jetty.util.thread.QueuedThreadPool'
include_class 'org.eclipse.jetty.servlet.DefaultServlet'
DEFAULT_OPTIONS = {
:port => 3000,
:host => 'localhost',
:root => File.join(File.dirname(__FILE__), '..'),
:public => '/public',
:rackup => File.join(File.dirname(__FILE__), '..', 'config.ru'),
:min_threads => 5,
:max_threads => 50,
:min_runtimes => 1,
:max_runtimes => 1
}
def run(options = DEFAULT_OPTIONS)
@server = Server.new
thread_pool = QueuedThreadPool.new
thread_pool.min_threads = options[:min_threads]
thread_pool.max_threads = options[:max_threads]
@server.set_thread_pool(thread_pool)
connector = SelectChannelConnector.new
connector.setPort options[:port]
connector.setHost options[:host]
@server.addConnector(connector)
context = ServletContextHandler.new(nil, "/",
ServletContextHandler::NO_SESSIONS|ServletContextHandler::NO_SECURITY)
context.add_filter("org.jruby.rack.RackFilter", "/*", 0)
context.set_resource_base(options[:root])
context.add_event_listener(org.jruby.rack.RackServletContextListener.new)
init_params = {
'org.eclipse.jetty.servlet.Default.relativeResourceBase' => options[:public],
'rackup' => IO.read(options[:rackup]),
'jruby.min.runtimes' => options[:min_runtimes].to_s,
'jruby.max.runtimes' => options[:max_runtimes].to_s
}
for name, value in init_params
context.set_init_parameter(name, value)
end
context.add_servlet(ServletHolder.new(DefaultServlet.new), "/")
@server.set_handler(context)
puts "Listening on #{options[:host]}:#{options[:port]}"
@server.start
trap("SIGINT") { @server.stop and exit }
@server.join
end
end
server = HttpServer.new
server.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment