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
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.



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

def asset
  @asset
end

#embedded_rendererObject

Returns the value of attribute embedded_renderer.



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

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.



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

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.



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

def redirect_hard
  @redirect_hard
end

#routerObject

Returns the value of attribute router.



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

def router
  @router
end

#sessionObject

Returns the value of attribute session.



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

def session
  @session
end

Class Method Details

.messagesObject

Get a list of message names that this object receives.



407
408
409
# File 'lib/gloo/objs/web_svr/svr.rb', line 407

def self.messages
  return super + [ 'start', 'stop', 'routes' ]
end

.short_typenameObject

The short name of the object type.



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

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



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

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:



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

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.



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

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.



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

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

#clear_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.



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

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

#default_layoutObject

Get the default layout for pages.



625
626
627
628
629
630
631
# File 'lib/gloo/objs/web_svr/svr.rb', line 625

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.



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

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.



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

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.



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

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.



614
615
616
617
618
619
620
# File 'lib/gloo/objs/web_svr/svr.rb', line 614

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 ).



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

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.



603
604
605
606
607
608
609
# File 'lib/gloo/objs/web_svr/svr.rb', line 603

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.



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

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

  return host.value
end

#msg_routesObject

Helper message to show all routes in the running server.



438
439
440
441
442
443
444
# File 'lib/gloo/objs/web_svr/svr.rb', line 438

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

#msg_startObject

Start the gloo web server.



414
415
416
417
418
419
420
421
# File 'lib/gloo/objs/web_svr/svr.rb', line 414

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.



426
427
428
429
430
431
432
433
# File 'lib/gloo/objs/web_svr/svr.rb', line 426

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:



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

def multiline_value?
  return false
end

#pages_containerObject

Get the pages container.



596
597
598
# File 'lib/gloo/objs/web_svr/svr.rb', line 596

def pages_container
  return find_child PAGES
end

#port_valueObject

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



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

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

  return port.value
end

#run_on_requestObject

Run the on request script if there is one.



523
524
525
526
527
528
529
# File 'lib/gloo/objs/web_svr/svr.rb', line 523

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

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

#run_on_responseObject

Run the on response script if there is one.



534
535
536
537
538
539
540
# File 'lib/gloo/objs/web_svr/svr.rb', line 534

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.



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

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.



513
514
515
516
517
518
# File 'lib/gloo/objs/web_svr/svr.rb', line 513

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.



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

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.



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

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.



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

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.



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

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.



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

def session_cookie_secure
  return scheme_value.downcase == HTTPS
end

#session_nameObject

Get the session cookie name.



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

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.



546
547
548
549
550
551
552
553
554
555
556
# File 'lib/gloo/objs/web_svr/svr.rb', line 546

def set_request_data( request )
  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.



563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# File 'lib/gloo/objs/web_svr/svr.rb', line 563

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.



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

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.



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

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.



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

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.



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

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.



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

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.



456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# File 'lib/gloo/objs/web_svr/svr.rb', line 456

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.



481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/gloo/objs/web_svr/svr.rb', line 481

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

  # Last chance to clear out session data.
  clear_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:



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

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:



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

def use_ssl?
  return ssl_cert && ssl_key
end