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
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, help, inherited, #initialize, #is_alias?, #is_function?, #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.



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

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.



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

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.



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

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.



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

def router
  @router
end

#sessionObject

Returns the value of attribute session.



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

def session
  @session
end

Class Method Details

.messagesObject

Get a list of message names that this object receives.



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

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

.short_typenameObject

The short name of the object type.



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

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



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

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:



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

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.



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

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.



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

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.



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

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

#default_layoutObject

Get the default layout for pages.



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

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.



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

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.



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

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.



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

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.



598
599
600
601
602
603
604
# File 'lib/gloo/objs/web_svr/svr.rb', line 598

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



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

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.



587
588
589
590
591
592
593
# File 'lib/gloo/objs/web_svr/svr.rb', line 587

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.



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

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.



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

def msg_routes
  if @router
    @router.show_routes
  else
    @engine.log.error SERVER_NOT_RUNNING
  end
end

#msg_startObject

Start the gloo web server.



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

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.



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

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

#multiline_value?Boolean

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

Returns:



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

def multiline_value?
  return false
end

#pages_containerObject

Get the pages container.



580
581
582
# File 'lib/gloo/objs/web_svr/svr.rb', line 580

def pages_container
  return find_child PAGES
end

#port_valueObject

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



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

def session_cookie_secure
  return scheme_value.downcase == HTTPS
end

#session_nameObject

Get the session cookie name.



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

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.



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

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



561
562
563
564
565
566
567
568
569
570
# File 'lib/gloo/objs/web_svr/svr.rb', line 561

def set_response_data( request, response )
  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 )
end

#set_session_var(key, value) ⇒ Object

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



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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:



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

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:



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

def use_ssl?
  return ssl_cert && ssl_key
end