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

Configuration

'scheme'.freeze
HOST =
'host'.freeze
PORT =
'port'.freeze
SSL_CERT =

SSL Configuration

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

Events

'on_start'.freeze
ON_STOP =
'on_stop'.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
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?, #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.



46
47
48
# File 'lib/gloo/objs/web_svr/svr.rb', line 46

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.



46
47
48
# File 'lib/gloo/objs/web_svr/svr.rb', line 46

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.



46
47
48
# File 'lib/gloo/objs/web_svr/svr.rb', line 46

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.



46
47
48
# File 'lib/gloo/objs/web_svr/svr.rb', line 46

def router
  @router
end

Class Method Details

.messagesObject

Get a list of message names that this object receives.



212
213
214
# File 'lib/gloo/objs/web_svr/svr.rb', line 212

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

.short_typenameObject

The short name of the object type.



58
59
60
# File 'lib/gloo/objs/web_svr/svr.rb', line 58

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



51
52
53
# File 'lib/gloo/objs/web_svr/svr.rb', line 51

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:



181
182
183
# File 'lib/gloo/objs/web_svr/svr.rb', line 181

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.



190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/gloo/objs/web_svr/svr.rb', line 190

def add_default_children
  fac = @engine.factory

  fac.create_string SCHEME, 'http', self
  fac.create_string HOST, 'localhost', self
  fac.create_string PORT, '8080', self

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

  fac.create_can PAGES, self
  fac.create_can HOME, self
end

#default_layoutObject

Get the default layout for pages.



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

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

#err_pageObject

Get the application error page.



345
346
347
348
349
350
351
# File 'lib/gloo/objs/web_svr/svr.rb', line 345

def err_page
  o = find_child ERR_PAGE
  return nil unless o

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

#home_pageObject

Get the home page, the root/default route.



334
335
336
337
338
339
340
# File 'lib/gloo/objs/web_svr/svr.rb', line 334

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.



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

def host_value
  host = find_child HOST
  return nil unless host

  return host.value
end

#msg_routesObject

Helper message to show all routes in the running server.



243
244
245
246
247
248
249
# File 'lib/gloo/objs/web_svr/svr.rb', line 243

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

#msg_startObject

Start the gloo web server.



219
220
221
222
223
224
225
226
# File 'lib/gloo/objs/web_svr/svr.rb', line 219

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.



231
232
233
234
235
236
237
238
# File 'lib/gloo/objs/web_svr/svr.rb', line 231

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:



73
74
75
# File 'lib/gloo/objs/web_svr/svr.rb', line 73

def multiline_value?
  return false
end

#pages_containerObject

Get the pages container.



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

def pages_container
  return find_child PAGES
end

#port_valueObject

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



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

def port_value
  port = find_child PORT
  return nil unless port

  return port.value
end

#run_on_startObject

Run the on render script if there is one.



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

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 rendered script if there is one.



312
313
314
315
316
317
# File 'lib/gloo/objs/web_svr/svr.rb', line 312

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.



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

def scheme_value
  scheme = find_child SCHEME
  return nil unless scheme

  return scheme.value
end

#set_value(new_value) ⇒ Object

Set the value with any necessary type conversions.



65
66
67
# File 'lib/gloo/objs/web_svr/svr.rb', line 65

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.



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

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.



162
163
164
165
166
167
168
169
170
# File 'lib/gloo/objs/web_svr/svr.rb', line 162

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.



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

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.



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/gloo/objs/web_svr/svr.rb', line 261

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 )

  run_on_start
  @engine.log.info "Web server started and listening…"
end

#stopObject

Stop the running web server.



284
285
286
287
288
289
290
291
292
# File 'lib/gloo/objs/web_svr/svr.rb', line 284

def stop
  @engine.log.info "Stopping web server…"
  @web_server.stop
  @web_server = nil
  @router = nil

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

#use_ssl?Boolean

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

Returns:



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

def use_ssl?
  return ssl_cert && ssl_key
end