Class: Gloo::WebSvr::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/gloo/web_svr/handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(engine, obj) ⇒ Handler

Set up the web server.



22
23
24
25
26
# File 'lib/gloo/web_svr/handler.rb', line 22

def initialize( engine, obj )
  @engine = engine
  @log = @engine.log
  @server_obj = obj
end

Instance Attribute Details

#server_objObject (readonly)

Returns the value of attribute server_obj.



12
13
14
# File 'lib/gloo/web_svr/handler.rb', line 12

def server_obj
  @server_obj
end

Instance Method Details

#file_error_resultObject

Get a file not found error result.



114
115
116
117
# File 'lib/gloo/web_svr/handler.rb', line 114

def file_error_result
  return Gloo::WebSvr::Response.text_response( @engine, 
    "File not found!", Gloo::WebSvr::ResponseCode::NOT_FOUND )
end

#handle(request) ⇒ Object

Process the request and return a result.



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
# File 'lib/gloo/web_svr/handler.rb', line 36

def handle request
  @request = request
  page_obj = nil
  route_params = nil

  page, id, route_params = @server_obj.router.page_for_route( @request.path, @request.method )
  @engine.log.debug "Found Page: #{page&.name}" if page

  request.request_params.id = id
  request.request_params.route_params = route_params
  request.request_params.log_id_keys

  if page
    if page.is_a? Gloo::Objs::FileHandle
      result = handle_file page
    else
      result = handle_page page
      page_obj = page
    end
  else
    result = server_error_result
  end

  return result, page_obj
end

#handle_file(file) ⇒ Object

Handle a request for a static file such as an image.



83
84
85
86
87
88
89
90
91
# File 'lib/gloo/web_svr/handler.rb', line 83

def handle_file file
  pn = @server_obj.asset.path_for_file file

  # Check to make sure it is a valid file
  # return error if it is not
  return file_error_result unless File.exist? pn

  return @server_obj.asset.render_file pn
end

#handle_page(page) ⇒ Object

Handle request for a page. Render the page, with possible redirect.



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/gloo/web_svr/handler.rb', line 66

def handle_page page
  result = page.render @request
  if redirect_hard_set?
    result = server_redirect_result
    @engine.running_app.obj.redirect_hard = nil
  elsif redirect_set?
    page = @engine.running_app.obj.redirect
    @log.debug "Redirecting to: #{page.pn}"
    @engine.running_app.obj.redirect = nil
    result = page.render
  end
  return result
end

#redirect_hard_set?Boolean

Is there a redirect page set in the running app?

Returns:

  • (Boolean)


135
136
137
138
# File 'lib/gloo/web_svr/handler.rb', line 135

def redirect_hard_set?
  return false unless @engine.app_running?
  return @engine.running_app.obj.redirect_hard
end

#redirect_set?Boolean

Is there a redirect page set in the running app?

Returns:

  • (Boolean)


127
128
129
130
# File 'lib/gloo/web_svr/handler.rb', line 127

def redirect_set?
  return false unless @engine.app_running?
  return @engine.running_app.obj.redirect
end

#server_error_resultObject

Return a server error result. Use the app’s error if there is one, otherwise a generic message.



102
103
104
105
106
107
108
109
# File 'lib/gloo/web_svr/handler.rb', line 102

def server_error_result
  err_page = @server_obj.err_page
  return err_page.render if err_page

  # Last resort, just return a generic error message.
  return Gloo::WebSvr::Response.text_response( @engine, 
    "Server error!", Gloo::WebSvr::ResponseCode::SERVER_ERR )
end

#server_redirect_resultObject

Return a redirect result.



143
144
145
146
147
# File 'lib/gloo/web_svr/handler.rb', line 143

def server_redirect_result
  target = @engine.running_app.obj.redirect_hard

  return Gloo::WebSvr::Response.redirect_response( @engine, target )
end