|
| 1 | +# |
| 2 | +# Author:: Daniel DeLeo (<dan@opscode.com>) |
| 3 | +# Copyright:: Copyright (c) 2010 Opscode, Inc. |
| 4 | +# License:: Apache License, Version 2.0 |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | + |
| 19 | +require 'rubygems' |
| 20 | +require 'rack' |
| 21 | +require 'thin' |
| 22 | +require 'singleton' |
| 23 | +require 'json' |
| 24 | +require 'open-uri' |
| 25 | + |
| 26 | +module TinyServer |
| 27 | + |
| 28 | + class Server < Rack::Server |
| 29 | + |
| 30 | + attr_writer :app |
| 31 | + |
| 32 | + def self.run(options=nil, &block) |
| 33 | + tiny_app = new(options) |
| 34 | + app_code = Rack::Builder.new(&block).to_app |
| 35 | + tiny_app.app = app_code |
| 36 | + tiny_app.start |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + class Manager |
| 41 | + |
| 42 | + DEFAULT_OPTIONS = {:server => 'thin', :Port => 9000, :Host => 'localhost', :environment => :none} |
| 43 | + |
| 44 | + def initialize(options=nil) |
| 45 | + @options = options ? DEFAULT_OPTIONS.merge(options) : DEFAULT_OPTIONS |
| 46 | + @creator = caller.first |
| 47 | + end |
| 48 | + |
| 49 | + def start |
| 50 | + @server_thread = Thread.new do |
| 51 | + @server = Server.run(@options) do |
| 52 | + run API.instance |
| 53 | + end |
| 54 | + end |
| 55 | + block_until_started |
| 56 | + end |
| 57 | + |
| 58 | + def url |
| 59 | + "http://localhost:#{@options[:Port]}" |
| 60 | + end |
| 61 | + |
| 62 | + def block_until_started |
| 63 | + 20.times do |
| 64 | + return true if started? |
| 65 | + end |
| 66 | + raise "TinyServer failed to boot :/" |
| 67 | + end |
| 68 | + |
| 69 | + def started? |
| 70 | + open(url) |
| 71 | + true |
| 72 | + rescue OpenURI::HTTPError |
| 73 | + true |
| 74 | + rescue Errno::ECONNREFUSED => e |
| 75 | + sleep 0.1 |
| 76 | + false |
| 77 | + end |
| 78 | + |
| 79 | + def stop |
| 80 | + # yes, this is terrible. |
| 81 | + @server_thread.kill |
| 82 | + @server_thread.join |
| 83 | + @server_thread = nil |
| 84 | + end |
| 85 | + |
| 86 | + end |
| 87 | + |
| 88 | + class API |
| 89 | + include Singleton |
| 90 | + |
| 91 | + GET = "GET" |
| 92 | + PUT = "PUT" |
| 93 | + POST = "POST" |
| 94 | + DELETE = "DELETE" |
| 95 | + |
| 96 | + attr_reader :api_data |
| 97 | + |
| 98 | + def initialize |
| 99 | + clear |
| 100 | + end |
| 101 | + |
| 102 | + def clear |
| 103 | + @api_data = {GET => [], PUT => [], POST => [], DELETE => []} |
| 104 | + end |
| 105 | + |
| 106 | + def get(path, response_code, data=nil, &block) |
| 107 | + @api_data[GET] << [path, Response.new(response_code,data, &block)] |
| 108 | + end |
| 109 | + |
| 110 | + def put(path, response_code, data=nil, &block) |
| 111 | + @api_data[PUT] << [path, Response.new(response_code,data, &block)] |
| 112 | + end |
| 113 | + |
| 114 | + def post(path, response_code, data=nil, &block) |
| 115 | + @api_data[POST] << [path, Response.new(response_code,data, &block)] |
| 116 | + end |
| 117 | + |
| 118 | + def delete(path, response_code, data=nil, &block) |
| 119 | + @api_data[DELETE] << [path, Response.new(response_code,data, &block)] |
| 120 | + end |
| 121 | + |
| 122 | + def call(env) |
| 123 | + if response = response_for_request(env) |
| 124 | + response.call |
| 125 | + else |
| 126 | + debug_info = {:message => "no data matches the request for #{env['REQUEST_URI']}", |
| 127 | + :available_data => @api_data, :request => env} |
| 128 | + [404, {'Content-Type' => 'application/json'}, debug_info.to_json] |
| 129 | + end |
| 130 | + end |
| 131 | + |
| 132 | + def response_for_request(env) |
| 133 | + if route_tuple = @api_data[env["REQUEST_METHOD"]].find { |route, response| route === env["REQUEST_URI"] } |
| 134 | + route_tuple[1] |
| 135 | + end |
| 136 | + end |
| 137 | + end |
| 138 | + |
| 139 | + class Response |
| 140 | + HEADERS = {'Content-Type' => 'application/json'} |
| 141 | + |
| 142 | + def initialize(response_code=200,data=nil, &block) |
| 143 | + @response_code, @data = response_code, data |
| 144 | + @block = block_given? ? block : nil |
| 145 | + end |
| 146 | + |
| 147 | + def call |
| 148 | + data = @data || @block.call |
| 149 | + [@response_code, HEADERS, data] |
| 150 | + end |
| 151 | + end |
| 152 | + |
| 153 | +end |
0 commit comments