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.



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

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

def handle request
  @request = request
  page_obj = nil

  page, id = @server_obj.router.page_for_route( @request.path, @request.method )
  @engine.log.debug "Found Page: #{page&.name}" if page
  request.id = id
  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.



75
76
77
78
79
80
81
82
83
# File 'lib/gloo/web_svr/handler.rb', line 75

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.



61
62
63
64
65
66
67
68
69
70
# File 'lib/gloo/web_svr/handler.rb', line 61

def handle_page page
  result = page.render @request
  if 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_set?Boolean

Is there a redirect page set in the running app?

Returns:

  • (Boolean)


119
120
121
122
# File 'lib/gloo/web_svr/handler.rb', line 119

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.



94
95
96
97
98
99
100
101
# File 'lib/gloo/web_svr/handler.rb', line 94

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