generated from ExamProCo/terraform-beginner-bootcamp-2023
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.rb
247 lines (210 loc) · 7.39 KB
/
server.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
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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
require 'sinatra'
require 'json'
require 'pry'
require 'active_model'
# we will mock having a state or database for this development server
# by setting a global variable. You would never use a global variable
# in production server.
$home = {}
# This is a ruby class that includes validations from ActiveRecord.
# This will represent our Home resources as a ruby object.
class Home
# ActiveModel is part of Ruby on Rails.
# it is used as an ORM. It has a module within
# ActiveModel that provides validations.
# The production Terratowns server is rails and uses
# very similar and in most cases identical validation
# https://guides.rubyonrails.org/active_model_basics.html
# https://guides.rubyonrails.org/active_record_validations.html
include ActiveModel::Validations
# create some virtual attributes to stored on this object
# This will set a getter and setter
# eg.
# home = new Home()
# home.town = 'hello' # setter
# home.town() # getter
attr_accessor :town, :name, :description, :domain_name, :content_version
validates :town, presence: true, inclusion: { in: [
'melomaniac-mansion',
'cooker-cove',
'video-valley',
'the-nomad-pad',
'gamers-grotto'
] }
# visible to all users
validates :name, presence: true
# visible to all users
validates :description, presence: true
# we want to lock this down to only be from cloudfront
validates :domain_name,
format: { with: /\.cloudfront\.net\z/, message: "domain must be from .cloudfront.net" }
# uniqueness: true,
# content version has to be an integer
# we will make sure it an incremental version in the controller.
validates :content_version, numericality: { only_integer: true }
end
# We are extending a class from Sinatra::Base to
# turn this generic class to utilize the sinatra web-framework
class TerraTownsMockServer < Sinatra::Base
def error code, message
halt code, {'Content-Type' => 'application/json'}, {err: message}.to_json
end
def error_json json
halt code, {'Content-Type' => 'application/json'}, json
end
def ensure_correct_headings
unless request.env["CONTENT_TYPE"] == "application/json"
error 415, "expected Content_type header to be application/json"
end
unless request.env["HTTP_ACCEPT"] == "application/json"
error 406, "expected Accept header to be application/json"
end
end
# return a harcoded access token
def x_access_code
return '9b49b3fb-b8e9-483c-b703-97ba88eef8e0'
end
def x_user_uuid
return 'e328f4ab-b99f-421c-84c9-4ccea042c7d1'
end
def find_user_by_bearer_token
# https://swagger.io/docs/specification/authentication/bearer-authentication/
auth_header = request.env["HTTP_AUTHORIZATION"]
# Check if the Authorization header exists?
if auth_header.nil? || !auth_header.start_with?("Bearer ")
error 401, "a1000 Failed to authenicate, bearer token invalid and/or teacherseat_user_uuid invalid"
end
# Does the token match the one in our database?
# if we cant find it than return an error or if it doesn't match
# code = access_code = token
code = auth_header.split("Bearer ")[1]
if code != x_access_code
error 401, "a1001 Failed to authenicate, bearer token invalid and/or teacherseat_user_uuid invalid"
end
# was there a user_uuid in the body payload json?
if params['user_uuid'].nil?
error 401, "a1002 Failed to authenicate, bearer token invalid and/or teacherseat_user_uuid invalid"
end
# the code and the user_uuid should be matching for user
unless code == x_access_code && params['user_uuid'] == x_user_uuid
error 401, "a1003 Failed to authenicate, bearer token invalid and/or teacherseat_user_uuid invalid"
end
end
# CREATE
post '/api/u/:user_uuid/homes' do
ensure_correct_headings()
find_user_by_bearer_token()
# puts will print to the terminal similar to a print or console.log
puts "# create - POST /api/homes"
# a begin/resurce is a try/catch, if an error occurs, result it.
begin
# Sinatra does not automatically part json bodys as params
# like rails so we need to manuall parse it.
payload = JSON.parse(request.body.read)
rescue JSON::ParserError
halt 422, "Malformed JSON"
end
# assign the payload to variables
# to make easier to work with the code
name = payload["name"]
description = payload["description"]
domain_name = payload["domain_name"]
content_version = payload["content_version"]
town = payload["town"]
# printing the variables out to console to make it eaiser
# to see or debug what we have inputed into this endpoint
puts "name #{name}"
puts "description #{description}"
puts "domain_name #{domain_name}"
puts "content_version #{content_version}"
puts "town #{town}"
# Create a new Home model and set to attributes
home = Home.new
home.town = town
home.name = name
home.description = description
home.domain_name = domain_name
home.content_version = content_version
# ensure our validation checks pass otherwise
# return the errors
unless home.valid?
# return the errors message back json
error 422, home.errors.messages.to_json
end
# generating a uuid at random.
uuid = SecureRandom.uuid
puts "uuid #{uuid}"
# will mock our data to our mock databse
# which just a global variable
$home = {
uuid: uuid,
name: name,
town: town,
description: description,
domain_name: domain_name,
content_version: content_version
}
# will just return uuid
return { uuid: uuid }.to_json
end
# READ
get '/api/u/:user_uuid/homes/:uuid' do
ensure_correct_headings
find_user_by_bearer_token
puts "# read - GET /api/homes/:uuid"
# checks for house limit
content_type :json
# does the uuid for the home match the one in our mock database
if params[:uuid] == $home[:uuid]
return $home.to_json
else
error 404, "failed to find home with provided uuid and bearer token"
end
end
# UPDATE
# very similar to create action
put '/api/u/:user_uuid/homes/:uuid' do
ensure_correct_headings
find_user_by_bearer_token
puts "# update - PUT /api/homes/:uuid"
begin
# Parse JSON payload from the request body
payload = JSON.parse(request.body.read)
rescue JSON::ParserError
halt 422, "Malformed JSON"
end
# Validate payload data
name = payload["name"]
description = payload["description"]
content_version = payload["content_version"]
unless params[:uuid] == $home[:uuid]
error 404, "failed to find home with provided uuid and bearer token"
end
home = Home.new
home.town = $home[:town]
home.domain_name = $home[:domain_name]
home.name = name
home.description = description
home.content_version = content_version
unless home.valid?
error 422, home.errors.messages.to_json
end
return { uuid: params[:uuid] }.to_json
end
# DELETE
delete '/api/u/:user_uuid/homes/:uuid' do
ensure_correct_headings
find_user_by_bearer_token
puts "# delete - DELETE /api/homes/:uuid"
content_type :json
if params[:uuid] != $home[:uuid]
error 404, "failed to find home with provided uuid and bearer token"
end
# delete from mock database
uuid = $home[:uuid]
$home = {}
{ uuid: uuid }.to_json
end
end
# This is what will run the server.
TerraTownsMockServer.run!