Class: Conveyor::App

Inherits:
Object
  • Object
show all
Defined in:
lib/conveyor/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(data_directory, *options) ⇒ App

Returns a new instance of App.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/conveyor/server.rb', line 11

def initialize(data_directory, *options)
  options = options.inject(){|(k, v), m| m[k] = v; m}
  @data_directory = data_directory
  @log_directory  = options[:log_directory]
  @unsafe_mode    = options[:unsafe_mode] # allows deleting of channels. REALLY UNSAFE!
  @verbose        = options[:verbose]

  if @log_directory
    @logger = Logger.new File.join(@log_directory, 'conveyor.log')
  else
    @logger = Logger.new '/dev/null'
  end

  t0 = Time.now
  i "reading data"

  @channels = {}
  Dir.entries(@data_directory).each do |e|
    if !['.', '..'].include?(e) && File.directory?(File.join(@data_directory, e)) && Channel.valid_channel_name?(e)
      i "initializing channel '#{e}'"
      @channels[e] = Channel.new(File.join(@data_directory, e))
    end
  end

  i "done reading data (took #{Time.now - t0} sec.)"

  @requests = 0
end

Instance Method Details

#call(env) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/conveyor/server.rb', line 169

def call(env)
  if @verbose
    pp env
  end
  @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
    i "#{env["REMOTE_ADDR"]} #{env["REQUEST_METHOD"]} #{env["REQUEST_PATH"]} 404"
    [404, {}, '']
  end
end

#create_new_channel(channel_name) ⇒ Object



44
45
46
# File 'lib/conveyor/server.rb', line 44

def create_new_channel channel_name
  @channels[channel_name] = Conveyor::Channel.new(File.join(@data_directory, channel_name))
end

#delete(env, m) ⇒ Object



161
162
163
164
165
166
167
# File 'lib/conveyor/server.rb', line 161

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



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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/conveyor/server.rb', line 106

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
      elsif params.key? 'after'
        headers, content = @channels[m.captures[0]].get_nearest_after_timestamp(params['after'].to_i)
      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

  i "#{env["REMOTE_ADDR"]} #{env["REQUEST_METHOD"]} #{env["REQUEST_PATH"]} 404"
  return [404, {}, '']
end

#i(msg) ⇒ Object



48
49
50
# File 'lib/conveyor/server.rb', line 48

def i msg
  @logger.info msg
end

#path_match(env, pattern) ⇒ Object



40
41
42
# File 'lib/conveyor/server.rb', line 40

def path_match env, pattern
  env["REQUEST_PATH"].match(pattern)
end

#post(env, m) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/conveyor/server.rb', line 68

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
    elsif params.key?('rewind_time')
      if params['group']
        @channels[m.captures[0]].rewind(:time => params['rewind_time'].to_i, :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(:time => params['rewind_time'].to_i) # TODO make sure this is an integer
        [200, {}, "iterator rewound to #{params['rewind_time']}"]
      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
    i "#{env["REMOTE_ADDR"]} #{env["REQUEST_METHOD"]} #{env["REQUEST_PATH"]} 404"
    [404, {}, '']
  end
end

#put(env, m) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/conveyor/server.rb', line 52

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"
      [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