forked from dangermccann/dcpu16-ide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.rb
54 lines (46 loc) · 1.26 KB
/
application.rb
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
require 'sinatra'
require 'aws-sdk'
require 'json'
set :public_folder, File.dirname(__FILE__)
get '/' do
send_file 'index.html'
end
post '/program' do
program = params[:program]
program_id = params[:program_id]
if !program.nil? && !program_id.nil?
programs().items.put(
:program_id => program_id,
:program => params[:program],
:hardware => params[:hardware]
)
return "OK"
else
return [400, "You did not specify a program or a program_id"]
end
end
get '/program/:id' do
return programs().items[params[:id]].attributes[:program]
end
get '/program.json/:id' do
program = programs().items[params[:id]]
return {
:program => program.attributes[:program],
:program_id => program.attributes[:program_id],
:hardware => program.attributes[:hardware]
}.to_json
end
def programs
dynamo_db = AWS::DynamoDB.new(
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'])
programs = dynamo_db.tables['dcpu16_programs']
programs.hash_key = [:program_id, :string]
return programs
end
if ENV['AWS_ACCESS_KEY_ID'].nil?
$stderr.puts "You forgot to set AWS_ACCESS_KEY_ID!"
end
if ENV['AWS_SECRET_ACCESS_KEY'].nil?
$stderr.puts "You forgot to set AWS_SECRET_ACCESS_KEY!"
end