Class: Gloo::Objs::Svr

Inherits:
Core::Obj show all
Defined in:
lib/gloo/objs/web_svr/svr.rb

Constant Summary collapse

KEYWORD =
'server'.freeze
KEYWORD_SHORT =
'svr'.freeze
CONFIG =

CONFIGURATION KEYS

'config'.freeze
SCHEME =
'scheme'.freeze
HTTP =
'http'.freeze
HTTPS =
'https'.freeze
HOST =
'host'.freeze
PORT =
'port'.freeze
SESSION_NAME =
'session_name'.freeze
ENCRYPT_KEY =
'encryption_key'.freeze
ENCRYPT_IV =
'encryption_iv'.freeze
'cookie_expires'.freeze
'cookie_path'.freeze
'/'.freeze
SSL_CERT =

SSL Configuration

'ssl_cert'.freeze
SSL_KEY =
'ssl_key'.freeze
ON_START =

Events

'on_start'.freeze
ON_STOP =
'on_stop'.freeze
ON_REQUEST =
'on_request'.freeze
ON_RESPONSE =
'on_response'.freeze
RESQUEST_DATA =
'request_data'.freeze
METHOD =
'method'.freeze
PATH =
'path'.freeze
QUERY =
'query'.freeze
IP =
'ip'.freeze
RESPONSE_DATA =
'response_data'.freeze
TYPE =
'type'.freeze
CODE =
'code'.freeze
ELAPSED =
'elapsed'.freeze
DB =
'db'.freeze
PAGE =
'page'.freeze
CURRENT_PAGE =
'current_page'.freeze
PAGES =

Container with pages in the web app.

'pages'.freeze
LAYOUT =

Default layout for pages.

'layout'.freeze
HOME =

Alias to the home and error pages

'home'.freeze
ERR_PAGE =
'error'.freeze
SESSION =

Session

'session'.freeze
SERVER_NOT_RUNNING =

Messages

'The web server is not running!'.freeze

Constants inherited from Core::Baseo

Core::Baseo::NOT_IMPLEMENTED_ERR

Instance Attribute Summary collapse

Attributes inherited from Core::Obj

#children, #parent, #value

Attributes inherited from Core::Baseo

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Core::Obj

#add_child, can_create?, #can_receive_message?, #child_count, #child_index, #contains_child?, #delete_children, #dispatch, #display_value, #find_add_child, #find_child, #find_child_resolve_alias, #find_child_value, help, inherited, #initialize, #is_alias?, #is_container?, #is_function?, #msg_blank?, #msg_contains?, #msg_reload, #msg_unload, #pn, #remove_child, #render, #root?, #send_message, #set_parent, #sql_value, #type_display, #value_display, #value_is_array?, #value_is_blank?, #value_string?

Methods inherited from Core::Baseo

#initialize, #type_display

Constructor Details

This class inherits a constructor from Gloo::Core::Obj

Instance Attribute Details

#assetObject

Returns the value of attribute asset.



79
80
81
# File 'lib/gloo/objs/web_svr/svr.rb', line 79

def asset
  @asset
end

#embedded_rendererObject

Returns the value of attribute embedded_renderer.



79
80
81
# File 'lib/gloo/objs/web_svr/svr.rb', line 79

def embedded_renderer
  @embedded_renderer
end

#redirectObject

Should the current request be redirected? If the redirect is set, then use that page instead of the one requested.



78
79
80
# File 'lib/gloo/objs/web_svr/svr.rb', line 78

def redirect
  @redirect
end

#redirect_hardObject

Should the current request be redirected? If the redirect is set, then use that page instead of the one requested.



78
79
80
# File 'lib/gloo/objs/web_svr/svr.rb', line 78

def redirect_hard
  @redirect_hard
end

#routerObject

Returns the value of attribute router.



79
80
81
# File 'lib/gloo/objs/web_svr/svr.rb', line 79

def router
  @router
end

#sessionObject

Returns the value of attribute session.



80
81
82
# File 'lib/gloo/objs/web_svr/svr.rb', line 80

def session
  @session
end

Class Method Details

.messagesObject

Get a list of message names that this object receives.



408
409
410
411
412
413
# File 'lib/gloo/objs/web_svr/svr.rb', line 408

def self.messages
  return super + [ 'start', 'stop', 
    'list_routes', 'list_assets', 
    'add_session_to_response', 'clear_session_data',
    'list_asset_img', 'list_asset_css', 'list_asset_js' ]
end

.short_typenameObject

The short name of the object type.



92
93
94
# File 'lib/gloo/objs/web_svr/svr.rb', line 92

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



85
86
87
# File 'lib/gloo/objs/web_svr/svr.rb', line 85

def self.typename
  return KEYWORD
end

Instance Method Details

#add_children_on_create?Boolean

Does this object have children to add when an object is created in interactive mode? This does not apply during obj load, etc.

Returns:



372
373
374
# File 'lib/gloo/objs/web_svr/svr.rb', line 372

def add_children_on_create?
  return true
end

#add_default_childrenObject

Add children to this object. This is used by containers to add children needed for default configurations.



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/gloo/objs/web_svr/svr.rb', line 381

def add_default_children
  fac = @engine.factory

  # Configuration
  config = fac.create_can CONFIG, self
  fac.create_string SCHEME, HTTP, config
  fac.create_string HOST, 'localhost', config
  fac.create_string PORT, '8080', config

  fac.create_script ON_START, '', self
  fac.create_script ON_STOP, '', self

  fac.create_alias LAYOUT, nil, self
  fac.create_alias HOME, nil, self
  fac.create_alias ERR_PAGE, nil, self

  fac.create_can PAGES, self
end

#add_session_containerObject

Add the session container because it is missing.



268
269
270
271
# File 'lib/gloo/objs/web_svr/svr.rb', line 268

def add_session_container
  fac = @engine.factory
  return fac.create_can SESSION, self
end

#default_layoutObject

Get the default layout for pages.



703
704
705
706
707
708
709
# File 'lib/gloo/objs/web_svr/svr.rb', line 703

def default_layout
  o = find_child LAYOUT
  return nil unless o

  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return o
end

#default_page_layoutObject

Get the default layout for the app.



114
115
116
117
118
119
120
# File 'lib/gloo/objs/web_svr/svr.rb', line 114

def default_page_layout
  o = find_child LAYOUT
  return nil unless o

  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return o
end

#encryption_ivObject

Get the initialization vector for the cipher.



200
201
202
203
204
205
206
207
# File 'lib/gloo/objs/web_svr/svr.rb', line 200

def encryption_iv
  config = find_child CONFIG
  o = config.find_child ENCRYPT_IV
  return nil unless o

  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return o.value
end

#encryption_keyObject

Get the key for the encryption cipher.



188
189
190
191
192
193
194
195
# File 'lib/gloo/objs/web_svr/svr.rb', line 188

def encryption_key
  config = find_child CONFIG
  o = config.find_child ENCRYPT_KEY
  return nil unless o

  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return o.value
end

#err_pageObject

Get the application error page.



692
693
694
695
696
697
698
# File 'lib/gloo/objs/web_svr/svr.rb', line 692

def err_page
  o = find_child ERR_PAGE
  return nil unless o

  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return o
end

#get_session_dataObject

Get the data from the session container. Data will be in the form of a hash ( key => value ).



277
278
279
280
281
282
283
284
285
286
287
# File 'lib/gloo/objs/web_svr/svr.rb', line 277

def get_session_data
  data = {}

  session_container.children.each do |session_var|
    key = session_var.name
    value = session_var.value
    data[ key ] = value
  end

  return data
end

#home_pageObject

Get the home page, the root/default route.



681
682
683
684
685
686
687
# File 'lib/gloo/objs/web_svr/svr.rb', line 681

def home_page
  o = find_child HOME
  return nil unless o

  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return o
end

#host_valueObject

Get the host from the child object. Returns nil if there is none.



143
144
145
146
147
148
149
# File 'lib/gloo/objs/web_svr/svr.rb', line 143

def host_value
  config = find_child CONFIG
  host = config.find_child HOST
  return nil unless host

  return host.value
end

#msg_add_session_to_responseObject

Add the session data to the response. This will be done for the current (next) request only.



503
504
505
# File 'lib/gloo/objs/web_svr/svr.rb', line 503

def msg_add_session_to_response
  @session.add_session_to_response if @session
end

#msg_clear_session_dataObject

Clear out the session data, and remove it from the response.



510
511
512
513
# File 'lib/gloo/objs/web_svr/svr.rb', line 510

def msg_clear_session_data
  reset_session_data
  @session.clear_session_data if @session
end

#msg_list_asset_cssObject

List all asset css in the running server. A Debugging tool.



479
480
481
482
483
484
485
# File 'lib/gloo/objs/web_svr/svr.rb', line 479

def msg_list_asset_css
  if @router
    @asset.list_css_assets
  else
    @engine.err SERVER_NOT_RUNNING
  end
end

#msg_list_asset_imgObject

List all asset images in the running server. A Debugging tool.



467
468
469
470
471
472
473
# File 'lib/gloo/objs/web_svr/svr.rb', line 467

def msg_list_asset_img
  if @router
    @asset.list_image_assets
  else
    @engine.err SERVER_NOT_RUNNING
  end
end

#msg_list_asset_jsObject

List all asset javascript in the running server. A Debugging tool.



491
492
493
494
495
496
497
# File 'lib/gloo/objs/web_svr/svr.rb', line 491

def msg_list_asset_js
  if @router
    @asset.list_js_assets
  else
    @engine.err SERVER_NOT_RUNNING
  end
end

#msg_list_assetsObject

Helper message to show all assets in the running server. A Debugging tool.



455
456
457
458
459
460
461
# File 'lib/gloo/objs/web_svr/svr.rb', line 455

def msg_list_assets
  if @router
    Gloo::WebSvr::AssetInfo.list_all( @engine )
  else
    @engine.err SERVER_NOT_RUNNING
  end
end

#msg_list_routesObject

Helper message to show all routes in the running server. A Debugging tool.



443
444
445
446
447
448
449
# File 'lib/gloo/objs/web_svr/svr.rb', line 443

def msg_list_routes
  if @router
    @router.show_routes
  else
    @engine.err SERVER_NOT_RUNNING
  end
end

#msg_startObject

Start the gloo web server.



418
419
420
421
422
423
424
425
# File 'lib/gloo/objs/web_svr/svr.rb', line 418

def msg_start
  @engine.log.debug "Starting web server…"
  # @engine.log.quiet = true

  # Set running app to this object.
  @engine.start_running_app( self )
  # The running app will call the start function (below)
end

#msg_stopObject

Stop the running web server.



430
431
432
433
434
435
436
437
# File 'lib/gloo/objs/web_svr/svr.rb', line 430

def msg_stop
  if @web_server
    @engine.stop_running_app
    # The running app will call the stop function (below)
  else
    @engine.err SERVER_NOT_RUNNING
  end
end

#multiline_value?Boolean

Does this object support multi-line values? Initially only true for scripts.

Returns:



107
108
109
# File 'lib/gloo/objs/web_svr/svr.rb', line 107

def multiline_value?
  return false
end

#pages_containerObject

Get the pages container.



674
675
676
# File 'lib/gloo/objs/web_svr/svr.rb', line 674

def pages_container
  return find_child PAGES
end

#port_valueObject

Get the port from the child object. Returns nil if there is none.



155
156
157
158
159
160
161
# File 'lib/gloo/objs/web_svr/svr.rb', line 155

def port_value
  config = find_child CONFIG
  port = config.find_child PORT
  return nil unless port

  return port.value
end

#reset_session_dataObject

Clear out all session data. Important to do this after the response is sent to avoid holding on to data that is no longer needed.



307
308
309
310
311
# File 'lib/gloo/objs/web_svr/svr.rb', line 307

def reset_session_data
  session_container.children.each do |session_var|
    session_var.value = ''
  end
end

#run_on_request(current_page) ⇒ Object

Run the on request script if there is one. Set thee current page object so the app knows which page is being requested.



594
595
596
597
598
599
600
601
602
603
# File 'lib/gloo/objs/web_svr/svr.rb', line 594

def run_on_request current_page
  for_page = find_child CURRENT_PAGE
  alias_value = current_page.pn
  for_page.set_value( alias_value ) if for_page
  o = find_child ON_REQUEST
  return unless o
  o = Gloo::Objs::Alias.resolve_alias( @engine, o )

  Gloo::Exec::Dispatch.message( @engine, 'run', o, CURRENT_PAGE => current_page )
end

#run_on_responseObject

Run the on response script if there is one.



608
609
610
611
612
613
614
# File 'lib/gloo/objs/web_svr/svr.rb', line 608

def run_on_response
  o = find_child ON_RESPONSE
  return unless o
  o = Gloo::Objs::Alias.resolve_alias( @engine, o )

  Gloo::Exec::Dispatch.message( @engine, 'run', o )
end

#run_on_startObject

Run the on start script if there is one.



572
573
574
575
576
577
# File 'lib/gloo/objs/web_svr/svr.rb', line 572

def run_on_start
  o = find_child ON_START
  return unless o

  Gloo::Exec::Dispatch.message( @engine, 'run', o )
end

#run_on_stopObject

Run the on stop script if there is one.



582
583
584
585
586
587
# File 'lib/gloo/objs/web_svr/svr.rb', line 582

def run_on_stop
  o = find_child ON_STOP
  return unless o

  Gloo::Exec::Dispatch.message( @engine, 'run', o )
end

#scheme_valueObject

Get the Scheme (http or https) from the child object. Returns nil if there is none.



131
132
133
134
135
136
137
# File 'lib/gloo/objs/web_svr/svr.rb', line 131

def scheme_value
  config = find_child CONFIG
  scheme = config.find_child SCHEME
  return nil unless scheme

  return scheme.value
end

#session_containerObject

Get the session container object. If there is none, one will be created.



255
256
257
258
259
260
261
262
263
# File 'lib/gloo/objs/web_svr/svr.rb', line 255

def session_container
  o = find_child SESSION

  unless o
    o = add_session_container
  end

  return o
end

Get the expiration time for the session cookie. If not specified, use one week from now.



227
228
229
230
231
232
233
234
235
236
# File 'lib/gloo/objs/web_svr/svr.rb', line 227

def session_cookie_expires
  config = find_child CONFIG
  o = config.find_child COOKIE_EXPIRES
  if o
    dt = Chronic.parse( o.value )
    return dt
  else
    return 1.week.from_now
  end
end

Get the path for the session cookie. If not specified, use the root path.



213
214
215
216
217
218
219
220
221
# File 'lib/gloo/objs/web_svr/svr.rb', line 213

def session_cookie_path
  config = find_child CONFIG
  o = config.find_child COOKIE_PATH
  if o
    return o.value
  else
    return DEFAULT_COOKIE_PATH
  end
end

Should the session cookie be secure? Get the value from the scheme settings/config.



242
243
244
# File 'lib/gloo/objs/web_svr/svr.rb', line 242

def session_cookie_secure
  return scheme_value.downcase == HTTPS
end

#session_nameObject

Get the session cookie name.



174
175
176
177
178
179
180
181
182
183
# File 'lib/gloo/objs/web_svr/svr.rb', line 174

def session_name
  config = find_child CONFIG
  session_name = config.find_child SESSION_NAME
  return nil unless session_name

  name = session_name.value
  return nil if name.blank?

  return name
end

#set_request_data(request) ⇒ Object

Set up the request data for the page load. This is done before the on_request event is fired.



620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
# File 'lib/gloo/objs/web_svr/svr.rb', line 620

def set_request_data( request )
  # Clear out the redirect if there is one since this is the start of
  # a new request.
  @redirect = nil

  data = find_child RESQUEST_DATA
  return unless data
  data = Gloo::Objs::Alias.resolve_alias( @engine, data )

  data.find_child( METHOD )&.set_value( request.method )
  data.find_child( HOST )&.set_value( request.host )
  data.find_child( PATH )&.set_value( request.path )
  data.find_child( QUERY )&.set_value( request.query )
  data.find_child( IP )&.set_value( request.ip )
end

#set_response_data(request, response, page_obj = nil) ⇒ Object

Set up the response data for the page load. This is done after the page is rendered and before the on_response event is fired.



641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
# File 'lib/gloo/objs/web_svr/svr.rb', line 641

def set_response_data( request, response, page_obj=nil )
  begin
    data = find_child RESPONSE_DATA
    return unless data
    data = Gloo::Objs::Alias.resolve_alias( @engine, data )

    data.find_child( ELAPSED )&.set_value( request.elapsed )
    data.find_child( DB )&.set_value( request.db )

    if ( response )
      data.find_child( TYPE )&.set_value( response.type )
      data.find_child( CODE )&.set_value( response.code )
    else
      data.find_child( TYPE )&.set_value( '' )
      data.find_child( CODE )&.set_value( '' )
    end

    if page_obj
      data.find_child( PAGE )&.set_value( page_obj.pn ) 
    end
  rescue => e
    @engine.log_exception e
  end
end

#set_session_var(key, value) ⇒ Object

Get the session child object with the given value. Create the child if it does not exist.



293
294
295
296
297
298
299
300
# File 'lib/gloo/objs/web_svr/svr.rb', line 293

def set_session_var( key, value )
  child_obj = session_container.find_child( key )
  unless child_obj
    fac = @engine.factory
    child_obj = fac.create_string key, value, session_container
  end
  child_obj.value = value
end

#set_value(new_value) ⇒ Object

Set the value with any necessary type conversions.



99
100
101
# File 'lib/gloo/objs/web_svr/svr.rb', line 99

def set_value( new_value )
  self.value = new_value.to_s
end

#ssl_certObject

Get the SSL certificate from the child object. Returns nil if there is none.



330
331
332
333
334
335
336
# File 'lib/gloo/objs/web_svr/svr.rb', line 330

def ssl_cert
  cert = find_child SSL_CERT
  return nil unless cert

  cert = Gloo::Objs::Alias.resolve_alias( @engine, cert )
  return cert
end

#ssl_configObject

Get the SSL configuration for the server.



353
354
355
356
357
358
359
360
361
# File 'lib/gloo/objs/web_svr/svr.rb', line 353

def ssl_config
  return nil unless use_ssl?

  return {
    :private_key_file => ssl_key.value,
    :cert_chain_file => ssl_cert.value,
    :verify_peer => false,
  }
end

#ssl_keyObject

Get the SSL key from the child object. Returns nil if there is none.



342
343
344
345
346
347
348
# File 'lib/gloo/objs/web_svr/svr.rb', line 342

def ssl_key
  key = find_child SSL_KEY
  return nil unless key

  key = Gloo::Objs::Alias.resolve_alias( @engine, key )
  return key
end

#startObject

Start running the web server.



525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# File 'lib/gloo/objs/web_svr/svr.rb', line 525

def start
  config = Gloo::WebSvr::Config.new( scheme_value, host_value, port_value )
  @engine.log.info "Web Server URL: #{config.base_url}"

  handler = Gloo::WebSvr::Handler.new( @engine, self )
  @web_server = Gloo::WebSvr::Server.new( @engine, handler, config, ssl_config )
  @web_server.start

  @router = Gloo::WebSvr::Routing::Router.new( @engine, self )
  @router.add_page_routes

  @asset = Gloo::WebSvr::Asset.new( @engine, self )
  @asset.add_asset_routes

  @embedded_renderer = Gloo::WebSvr::EmbeddedRenderer.new( @engine, self )

  @session = Gloo::WebSvr::Session.new( @engine, self )
  
  run_on_start
  @engine.log.info "Web server started and listening…"
end

#stopObject

Stop the running web server.



550
551
552
553
554
555
556
557
558
559
560
561
562
# File 'lib/gloo/objs/web_svr/svr.rb', line 550

def stop
  @engine.log.info "Stopping web server…"

  # Last chance to clear out session data.
  reset_session_data

  @web_server.stop
  @web_server = nil
  @router = nil

  run_on_stop
  @engine.log.info "Web server stopped…"
end

#use_session?Boolean

Is this server configured to use a session? It is if theere is a non-empty session name.

Returns:



167
168
169
# File 'lib/gloo/objs/web_svr/svr.rb', line 167

def use_session?
  return ! session_name.blank?
end

#use_ssl?Boolean

Is SSL configured for this server? True if the Cert and Key are both present.

Returns:



322
323
324
# File 'lib/gloo/objs/web_svr/svr.rb', line 322

def use_ssl?
  return ssl_cert && ssl_key
end