Class: Conveyor::App
- Inherits:
-
Object
- Object
- Conveyor::App
- Defined in:
- lib/conveyor/server.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
- #create_new_channel(channel_name) ⇒ Object
- #delete(env, m) ⇒ Object
- #get(env) ⇒ Object
- #i(msg) ⇒ Object
-
#initialize(data_directory, *options) ⇒ App
constructor
A new instance of App.
- #path_match(env, pattern) ⇒ Object
- #post(env, m) ⇒ Object
- #put(env, m) ⇒ Object
Constructor Details
#initialize(data_directory, *options) ⇒ App
Returns a new instance of App.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/conveyor/server.rb', line 10 def initialize(data_directory, *) = .inject(){|(k, v), m| m[k] = v; m} @data_directory = data_directory @log_directory = [:log_directory] @unsafe_mode = [:unsafe_mode] # allows deleting of channels. REALLY UNSAFE! if @log_directory @logger = Logger.new File.join(@log_directory, 'conveyor.log') else @logger = Logger.new '/dev/null' end @channels = {} Dir.entries(@data_directory).each do |e| if !['.', '..'].include?(e) && File.directory?(File.join(@data_directory, e)) && Channel.valid_channel_name?(e) @channels[e] = Channel.new(File.join(@data_directory, e)) end end @requests = 0 end |
Instance Method Details
#call(env) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/conveyor/server.rb', line 149 def call(env) @requests += 1 if env['REQUEST_METHOD'] == 'PUT' && m = path_match(env, %r{/channels/(.*)}) put(env, m) elsif env['REQUEST_METHOD'] == 'POST' && m = path_match(env, %r{/channels/(.*)}) post(env, m) elsif @unsafe_mode && env['REQUEST_METHOD'] == 'DELETE' && m = path_match(env, %r{/channels/(.*)}) delete(env, m) elsif env['REQUEST_METHOD'] == 'GET' get(env) else [404, {}, ''] end end |
#create_new_channel(channel_name) ⇒ Object
36 37 38 |
# File 'lib/conveyor/server.rb', line 36 def create_new_channel channel_name @channels[channel_name] = Conveyor::Channel.new(File.join(@data_directory, channel_name)) end |
#delete(env, m) ⇒ Object
141 142 143 144 145 146 147 |
# File 'lib/conveyor/server.rb', line 141 def delete env, m if @channels.key?(m.captures[0]) @channels[m.captures[0]].delete! @channels.delete(m.captures[0]) [200, {}, "Channel deleted."] end end |
#get(env) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/conveyor/server.rb', line 89 def get env headers = content = nil if m = path_match(env, %r{/channels/(.*)/(\d+)}) if @channels.key?(m.captures[0]) headers, content = @channels[m.captures[0]].get(m.captures[1].to_i) end elsif m = path_match(env, %r{/channels/(.*)}) if @channels.key?(m.captures[0]) params = Mongrel::HttpRequest.query_parse(env['QUERY_STRING']) if params.key? 'next' if params.key? 'group' if params.key? 'n' list = @channels[m.captures[0]].get_next_n_by_group(params['n'].to_i, params['group']) else headers, content = @channels[m.captures[0]].get_next_by_group(params['group']) end else if params.key? 'n' list = @channels[m.captures[0]].get_next_n(params['n'].to_i).map do |i| {:data => i[1], :hash => i[0][:hash], :id => i[0][:id]} end else headers, content = @channels[m.captures[0]].get_next end end else return [200, {}, @channels[m.captures[0]].status.to_json] end end else return [200, {}, "fake!"] end if headers && content i "#{env["REMOTE_ADDR"]} GET #{env["REQUEST_PATH"]} 200 #{headers[:id]} #{headers[:length]} #{headers[:hash]}" return [ 200, { 'Content-Location' => "/channels/#{m.captures[0]}/#{headers[:id]}", 'Content-MD5' => headers[:hash], 'Content-Type' => 'application/octet-stream', 'Last-Modified' => Time.at(headers[:time]).gmtime.to_s, }, content ] elsif list return [200, {}, list.to_json] end return [404, {}, ''] end |
#i(msg) ⇒ Object
40 41 42 |
# File 'lib/conveyor/server.rb', line 40 def i msg @logger.info msg end |
#path_match(env, pattern) ⇒ Object
32 33 34 |
# File 'lib/conveyor/server.rb', line 32 def path_match env, pattern env["REQUEST_PATH"].match(pattern) end |
#post(env, m) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/conveyor/server.rb', line 60 def post env, m if @channels.key?(m.captures[0]) params = Mongrel::HttpRequest.query_parse(env['QUERY_STRING']) if params.key?('rewind_id') if params['group'] @channels[m.captures[0]].rewind(:id => params['rewind_id'], :group => params['group']).to_i # TODO make sure this is an integer [200, {}, "iterator rewound to #{params['rewind_id']}"] else @channels[m.captures[0]].rewind(:id => params['rewind_id']).to_i # TODO make sure this is an integer [200, {}, "iterator rewound to #{params['rewind_id']}"] end else if env.key?('HTTP_DATE') && d = Time.parse(env['HTTP_DATE']) id = @channels[m.captures[0]].post(env['rack.input'].read) i "#{env["REMOTE_ADDR"]} POST #{env["REQUEST_PATH"]} 202" [202, {"Location" => "/channels/#{m.captures[0]}/#{id}"}, ""] else i "#{env["REMOTE_ADDR"]} POST #{env["REQUEST_PATH"]} 400" [400, {}, "A valid Date header is required for all POSTs."] end end elsif Channel.valid_channel_name?(m.captures[0]) create_new_channel(m.captures[0]) post(env, m) else [404, {}, ''] end end |
#put(env, m) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/conveyor/server.rb', line 44 def put env, m if Channel.valid_channel_name?(m.captures[0]) if !@channels.key?(m.captures[0]) create_new_channel m.captures[0] i "#{env["REMOTE_ADDR"]} PUT #{env["REQUEST_PATH"]} 201" [201, {}, "created channel #{m.captures[0]}"] else i "#{env["REMOTE_ADDR"]} PUT #{env["REQUEST_PATH"]}" [202, {}, "channel already exists. didn't do anything"] end else i "#{env["REMOTE_ADDR"]} GET #{env["REQUEST_PATH"]} 406" [406, {}, "invalid channel name. must match #{Channel::NAME_PATTERN}"] end end |