Class: FaaStRuby::Server

Inherits:
Sinatra::Base
  • Object
show all
Includes:
Logger::Requests
Defined in:
lib/faastruby/server/app.rb

Instance Method Summary collapse

Methods included from Logger::Requests

#puts, rougify, #rougify, #tag

Instance Method Details

#is_a_function?(name) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/faastruby/server/app.rb', line 79

def is_a_function?(name)
  File.file?("#{FaaStRuby::ProjectConfig.functions_dir}/#{name}/faastruby.yml")
end

#log_request_message(function_name, request, request_uuid, query_params, body, context) ⇒ Object



49
50
51
# File 'lib/faastruby/server/app.rb', line 49

def log_request_message(function_name, request, request_uuid, query_params, body, context)
  puts "[#{function_name}] <- [REQUEST: #{request.request_method} \"#{request.fullpath}\"] request_id=\"#{request_uuid}\" body=\"#{body}\" query_params=#{query_params} headers=#{headers}"
end

#log_response_message(function_name, time, request_uuid, response, print_body) ⇒ Object



53
54
55
# File 'lib/faastruby/server/app.rb', line 53

def log_response_message(function_name, time, request_uuid, response, print_body)
  puts "[#{function_name}] -> [RESPONSE: #{time}ms] request_id=\"#{request_uuid}\" status=#{response.status} body=#{print_body.inspect} headers=#{response.headers}"
end

#parse_body(body, content_type, method) ⇒ Object



83
84
85
86
87
88
# File 'lib/faastruby/server/app.rb', line 83

def parse_body(body, content_type, method)
  return nil if method == 'GET'
  return {} if body.nil? && method != 'GET'
  return Oj.load(body) if content_type == 'application/json'
  return body
end

#parse_headers(env) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/faastruby/server/app.rb', line 99

def parse_headers(env)
  Hash[*env.select {|k,v| k.start_with? 'HTTP_'}
    .collect {|k,v| [k.sub(/^HTTP_/, ''), v]}
    .collect {|k,v| [k.split('_').collect{|a|k == 'DNT' ? k : k.capitalize}.join('-'), v]}
    .sort
    .flatten]
end

#parse_query(query_string) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/faastruby/server/app.rb', line 90

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) ⇒ Object



64
65
66
67
# File 'lib/faastruby/server/app.rb', line 64

def parse_response(response)
  return [Base64.urlsafe_decode64(response.body), "Base64(#{response.body})"] if response.binary?
  return [response.body, "#{response.body}"]
end

#resolve_function_name(splat) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/faastruby/server/app.rb', line 69

def resolve_function_name(splat)
  if splat == ''
    return FaaStRuby::ProjectConfig.root_to
  end
  if !is_a_function?(splat)
    return FaaStRuby::ProjectConfig.catch_all
  end
  return splat
end

#set_response_headers(response, request_uuid, original_request_id = nil, time) ⇒ Object



57
58
59
60
61
62
# File 'lib/faastruby/server/app.rb', line 57

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