Permalink
Browse files

example of setting up custom server

  • Loading branch information...
1 parent f0e3da4 commit 0083a6aa33dc7aa33eabf45f468d09b69e8bf8c3 @dj2 dj2 committed Feb 20, 2011
Showing with 54 additions and 0 deletions.
  1. +54 −0 examples/custom_server.rb
View
54 examples/custom_server.rb
@@ -0,0 +1,54 @@
+#!/usr/bin/env ruby
+$:<< '../lib' << 'lib'
+
+require 'logger'
+require 'goliath'
+
+# Our custom Goliath API
+class HelloWorld < Goliath::API
+ def response(env)
+ [200, {}, "hello world!"]
+ end
+end
+
+class Bonjour < Goliath::API
+ def response(env)
+ [200, {}, "bonjour!"]
+ end
+end
+
+
+router = Rack::Builder.new do
+ map '/version' do
+ run Proc.new {|env| [200, {"Content-Type" => "text/html"}, ["Version 0.1"]] }
+ end
+
+ map "/hello_world" do
+ run HelloWorld.new
+ end
+
+ map "/bonjour" do
+ run Bonjour.new
+ end
+
+ map "/" do
+ run Proc.new {|env| [404, {"Content-Type" => "text/html"}, ["Try /version /hello_world or /bonjour"]] }
+ end
+
+end
+
+Goliath::Application.options_parser.parse!(ARGV)
+options = Goliath::Application.options
+
+# We have to start our own server
+server = Goliath::Server.new(options[:address], options[:port])
+server.logger = Logger.new(STDOUT)
+server.app = router
+puts "Starting server: #{server.address}:#{server.port}"
+server.start
+
+
+at_exit do
+ puts "Ciao!"
+ exit
+end

0 comments on commit 0083a6a

Please sign in to comment.