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_function?, #msg_blank?, #msg_contains?, #msg_reload, #msg_unload, #pn, #remove_child, #render, #root?, #send_message, #set_parent, #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

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 asset
  @asset
end

#embedded_rendererObject

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

#routerObject

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 router
  @router
end

#sessionObject

Returns the value of attribute session.



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

def session
  @session
end

Class Method Details

.messagesObject

Get a list of message names that this object receives.



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

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

.short_typenameObject

The short name of the object type.



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

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



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

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:



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

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.



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

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.



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

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.



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

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

#default_layoutObject

Get the default layout for pages.



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

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.



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

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.



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

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.



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

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.



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

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



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

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.



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

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.



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

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.



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

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

#msg_startObject

Start the gloo web server.



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

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.



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

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:



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

def multiline_value?
  return false
end

#pages_containerObject

Get the pages container.



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

def pages_container
  return find_child PAGES
end

#port_valueObject

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



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

def session_cookie_secure
  return scheme_value.downcase == HTTPS
end

#session_nameObject

Get the session cookie name.



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

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.



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

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.



562
563
564
565
566
567
568
569
570
571
572
573
574
575
# File 'lib/gloo/objs/web_svr/svr.rb', line 562

def set_response_data( request, response, page_obj=nil )
  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 )
  data.find_child( TYPE )&.set_value( response.type )
  data.find_child( CODE )&.set_value( response.code )

  if page_obj
    data.find_child( PAGE )&.set_value( page_obj.pn ) 
  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.



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

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_obj
  end
  child_obj.value = value
end

#set_value(new_value) ⇒ Object

Set the value with any necessary type conversions.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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:



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

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:



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

def use_ssl?
  return ssl_cert && ssl_key
end