Class: ReverseTunnel::Server::ApiServer

Inherits:
EM::Connection
  • Object
show all
Includes:
EM::HttpServer
Defined in:
lib/reverse-tunnel/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server) ⇒ ApiServer

Returns a new instance of ApiServer.



12
13
14
# File 'lib/reverse-tunnel/server.rb', line 12

def initialize(server)
  @server = server
end

Instance Attribute Details

#serverObject

Returns the value of attribute server.



10
11
12
# File 'lib/reverse-tunnel/server.rb', line 10

def server
  @server
end

Instance Method Details

#post_initObject



16
17
18
19
# File 'lib/reverse-tunnel/server.rb', line 16

def post_init
  super
  no_environment_strings
end

#process_http_requestObject



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
# File 'lib/reverse-tunnel/server.rb', line 21

def process_http_request
  # the http request details are available via the following instance variables:
  #   @http_protocol
  #   @http_request_method
  #   @http_cookie
  #   @http_if_none_match
  #   @http_content_type
  #   @http_path_info
  #   @http_request_uri
  #   @http_query_string
  #   @http_post_content
  #   @http_headers

  ReverseTunnel.logger.debug { "Process http request #{@http_request_uri}" }

  response = EM::DelegatedHttpResponse.new(self)
  response.status = 200
  response.content_type 'application/json'

  begin

    case @http_request_uri 
    when %r{^/tunnels(.json)?$}
      case @http_request_method
      when "GET"
        response.content = server.tunnels.to_json
      when "POST"
        params = @http_post_content ? JSON.parse(@http_post_content) : {}
        tunnel = server.tunnels.create params
        response.content = tunnel.to_json
      end
    when %r{^/tunnels/([0-9A-F]+)(.json)?$}
      tunnel_id = $1
      tunnel = server.tunnels.find(tunnel_id)

      if tunnel
        case @http_request_method
        when "GET"
          response.content = tunnel.to_json
        when "DELETE"
          tunnel = server.tunnels.destroy(tunnel_id)
          response.content = tunnel.to_json
        end
      end
    else 
    end
  rescue => e
    ReverseTunnel.logger.error "Error in http request processing: #{e}"
    response.status = 500
  end

  if response.content.nil?
    response.status = 404
  end

  response.send_response
end