Class: Objs::Svr

Inherits:
Gloo::Core::Obj
  • Object
show all
Defined in:
lib/objs/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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#assetObject

Returns the value of attribute asset.



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

def asset
  @asset
end

#embedded_rendererObject

Returns the value of attribute embedded_renderer.



78
79
80
# File 'lib/objs/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/objs/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/objs/svr.rb', line 77

def redirect_hard
  @redirect_hard
end

#routerObject

Returns the value of attribute router.



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

def router
  @router
end

#sessionObject

Returns the value of attribute session.



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

def session
  @session
end

Class Method Details

.doc_dataObject

Get the object's documentation data.



717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
# File 'lib/objs/svr.rb', line 717

def self.doc_data
  {
    :name => KEYWORD,
    :shortcut => KEYWORD_SHORT,
    :description => 'A web server running inside gloo.',
    :children => [
      'config (container) — Configuration and settings for the server. See below for its children.',
      'on_start (script) — Run when the web server is started.',
      'on_stop (script) — Run when the web server is stopped.',
      "layout (partial) — By convention an alias pointing to the layout used for all pages (layouts live in the layout root-level folder).",
      'home (page) — By convention an alias pointing to the home page object (pages live in the page root-level folder).',
      'error (page) — By convention an alias pointing to the default error page object.',
      'pages (container) — Routes. By convention, aliases pointing to pages in the page root-level folder.',
      'config.scheme (string) — \'http\' or \'https\'.',
      "config.host (string) — Default: 'localhost'.",
      "config.port (string) — Default: '8080'.",
      'config.session_name (string) — Optional. Example: \'_myapp_session\'. The name of the session cookie.',
      'config.encryption_key (string) — Optional. Encryption key for the session cookie.',
      'config.encryption_iv (string) — Optional. Initialization vector for the session cookie encryption key.',
      "config.cookie_expires (string) — Optional, default 'in 1 week'. When the session expires.",
      "config.cookie_path (string) — Optional, default '/'. The path for the session cookie.",
      'config.ssl_cert (string) — Optional, required for SSL. Path to certificate.pem.',
      'config.ssl_key (string) — Optional, required for SSL. Path to key.pem.',
      'on_request (script) — Optional. Run when the web server receives a request.',
      'request_data (container) — Optional. Data available for use in on_request or elsewhere in the page rendering life-cycle: method, host, path, query, ip (all string, all optional, populated only if present).',
      'on_response (script) — Optional. Run when the web server is done rendering and about to return a response.',
      'response_data (container) — Optional. Data available for use in on_response (request_data is also available at this point): page, type, code, elapsed, db (all string, all optional, populated only if present).'
    ],
    :messages => [
      'start — Start the web server.',
      'stop — Stop the web server.',
      'list_routes — Show the routing table. A debugging tool.',
      'list_assets — Show the list of assets. A debugging tool.',
      'list_asset_img — Show the list of image assets. A debugging tool.',
      'list_asset_css — Show the list of CSS assets. A debugging tool.',
      'list_asset_js — Show the list of JavaScript assets. A debugging tool.',
      'add_session_to_response — Tell the gloo web server to include the session data in the response (put in the session cookie). Use when the user successfully authenticates.',
      'clear_session_data — Clear the session data and tell the gloo web server to include the session data in the response. This destroys the session; use when the user logs out.'
    ],
    :examples => <<~EXAMPLES.strip
      app [can] :
        # The address of the running web server.
        url [uri] : http://localhost:8083/

        svr [svr] :
          # Configuration for the web server.
          config [container] :
            scheme [string] : http
            host [string] : localhost
            port [string] : 8083

          # Scripts to run when the server starts and stops.
          on_start [script] : show '*** started ***'
          on_stop [script] : show '*** stopped ***'

          # Default Routes for the web server.
          layout [alias] : layout.primary
          home [alias] : page.home
          error [alias] : page.err

          # Routes for the web server.
          pages [container] :
            home [alias] : page.home
            other [alias] : page.other
    EXAMPLES
  }
end

.messagesObject

Get a list of message names that this object receives.



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

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.



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

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



84
85
86
# File 'lib/objs/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:

  • (Boolean)


371
372
373
# File 'lib/objs/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/objs/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/objs/svr.rb', line 267

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

#default_layoutObject

Get the default layout for pages.



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

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



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

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



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

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/objs/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_add_session_to_responseObject

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



502
503
504
# File 'lib/objs/svr.rb', line 502

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.



509
510
511
512
# File 'lib/objs/svr.rb', line 509

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.



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

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.



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

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.



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

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.



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

def msg_list_assets
  if @router
    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.



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

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

#msg_startObject

Start the gloo web server.



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

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.



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

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:

  • (Boolean)


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

def multiline_value?
  return false
end

#pages_containerObject

Get the pages container.



673
674
675
# File 'lib/objs/svr.rb', line 673

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/objs/svr.rb', line 154

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.



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

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.



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

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.



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

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.



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

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.



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

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



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

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.



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

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



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

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

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

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

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

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

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

#stopObject

Stop the running web server.



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

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:

  • (Boolean)


166
167
168
# File 'lib/objs/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:

  • (Boolean)


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

def use_ssl?
  return ssl_cert && ssl_key
end