class Gotta::Serve::App

Public Instance Methods

body_content(request_body) click to toggle source
# File lib/gotta/serve/app.rb, line 158
def body_content(request_body)
  return {} if (content = request_body.read).nil?

  content
end
form_content(request_env) click to toggle source
# File lib/gotta/serve/app.rb, line 152
def form_content(request_env)
  return {} if (content = request_env['rack.request.form_hash']).nil?

  content.to_json
end
log_request_message(component_name, request, request_uuid, query_params, body, request_headers) click to toggle source
# File lib/gotta/serve/app.rb, line 94
def log_request_message(component_name,
                        request,
                        request_uuid,
                        query_params,
                        body,
                        request_headers)

  puts "[#{component_name}] <- [REQUEST: #{request.request_method} \"#{request.fullpath}\"] request_id=\"#{request_uuid}\" body=#{body.inspect} query_params=#{query_params} headers=#{request_headers}"
end
log_response_message(component_name, time, request_uuid, response, print_body) click to toggle source
# File lib/gotta/serve/app.rb, line 104
def log_response_message(component_name,
                         time,
                         request_uuid,
                         response,
                         print_body)

  puts "[#{component_name}] -> [RESPONSE: #{time}ms] request_id=\"#{request_uuid}\" status=#{response.status} body=#{print_body.inspect} headers=#{response.headers}"
end
parse_body(request, content_type, method) click to toggle source
# File lib/gotta/serve/app.rb, line 145
def parse_body(request, content_type, method)
  return nil if method == 'GET'
  return form_content(request.env) if content_type == 'application/x-www-form-urlencoded'

  body_content(request.body)
end
parse_headers(env) click to toggle source
# File lib/gotta/serve/app.rb, line 173
def parse_headers(env)
  result = {}
  env.select { |e| e.match(/^HTTP_/) }.each do |k, v|
    newkey = k.sub(/^HTTP_/, '').split('_').map(&:capitalize).join('-')
    result[newkey] = v
  end
  result['Content-Type'] = env['CONTENT_TYPE']
  result['Request-Method'] = env['REQUEST_METHOD']
  result['Content-Length'] = env['CONTENT_LENGTH']
  result['Remote-Addr'] = env['REMOTE_ADDR']
  result
end
parse_query(query_string) click to toggle source
# File lib/gotta/serve/app.rb, line 164
def parse_query(query_string)
  hash = {}
  query_string.split('&').each do |param|
    key, value = param.split('=')
    hash[key] = value
  end
  hash
end
parse_response(response) click to toggle source
# File lib/gotta/serve/app.rb, line 124
def parse_response(response)
  return [Base64.urlsafe_decode64(response.body), "Base64(#{response.body})"] if response.binary?

  [response.body, response.body]
end
resolve_component_name(splat) click to toggle source
# File lib/gotta/serve/app.rb, line 130
def resolve_component_name(splat)
  return Gotta::Project.config.root_to if splat == ''
  return Gotta::Project.config.catch_all unless File.directory?(name)

  # TODO: 'catch_all' should render when the handler file
  # cannot be found.
  # Maybe this could happen when the handler file can't
  # be found (trigger by gotta-mod)

  # if !is_a_component?(splat)
  #   return Gotta::Project.config.catch_all
  # end
  splat
end
set_request_headers(env) click to toggle source
# File lib/gotta/serve/app.rb, line 87
def set_request_headers(env)
  request_headers = parse_headers(env)
  request_headers['X-Request-Id'] = SecureRandom.uuid
  request_headers['Request-Method'] = request.request_method
  [request_headers, request_headers['X-Original-Request-Id']]
end
set_response_headers(response, request_uuid, original_request_id = nil, time) click to toggle source
# File lib/gotta/serve/app.rb, line 113
def set_response_headers(response,
                         request_uuid,
                         original_request_id = nil,
                         time)

  response.headers['X-Request-Id'] = request_uuid
  response.headers['X-Original-Request-Id'] = original_request_id if original_request_id
  response.headers['X-Execution-time'] = "#{time}ms"
  response.headers
end