summaryrefslogtreecommitdiff
path: root/server-rack.rb
blob: d68d867af7c412a39989325f874fa2a57e4e12db (plain)
1
2
3
4
5
6
7
8
9
10
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env ruby
# Usage:
#
# $ ./script/server
require 'digest/sha1'
require 'rack'
require 'json'

class DataStorageServer
  MAX_BYTES=1024

  # You may initialize any variables you want to use across requests here
  def initialize
    @storage = {}
  end

  # Download an Object
  #
  # GET /data/{repository}/{objectID}
  # Response
  #
  # Status: 200 OK
  # {object data}
  # Objects that are not on the server will return a 404 Not Found.
  def get(path)
    oid = id_from(path)
    if @storage.key?(oid)
      ['200', {}, [@storage[oid]]]
    else
      ['404', {}, []]
    end
  end

  # Upload an Object
  #
  # Request
  # PUT /data/{repository}
  #
  # my data
  #
  # Response
  #
  # Status: 201 Created
  # {size, oid}
  # Blobs can have a max size of 1024 bytes.
  # If a blob exceeds this size then the blob is truncated
  # and the first 1024 bytes are stored.
  def put(io)
    data = io.read(MAX_BYTES)
    oid = Digest::SHA1.hexdigest(data)
    @storage[oid] = data
    ['201', {}, [JSON.generate({ size: data.size, oid: oid })]]
  end

  # Delete an Object
  #
  # Request
  # DELETE /data/{repository}/{oid}
  #
  # Response
  #
  # Status: 200 OK
  # If the object identifer is unknown
  # then a 404 status code is returned.
  def destroy(path)
    oid = id_from(path)
    if @storage.key?(oid)
      @storage.delete(oid)
      ['200', {}, []]
    else
      ['404', {}, []]
    end
  end

  def call(env)
    case env['REQUEST_METHOD']
    when 'GET'
      get(env['PATH_INFO'])
    when 'PUT'
      put(env['rack.input'])
    when 'DELETE'
      destroy(env['PATH_INFO'])
    else
      raise env.inspect
    end
  end

  private

  def id_from(path)
    path.split('/')[-1]
  end
end

# This starts the server if the script is invoked from the command line. No
# modifications needed here.
if __FILE__ == $0
  app = Rack::Builder.new do
    use Rack::Reloader
    run DataStorageServer.new
  end.to_app

  Rack::Server.start(app: app, Port: 8282)
end