Class: Intranet::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/intranet/core.rb,
lib/intranet/core/builder.rb,
lib/intranet/core/locales.rb,
lib/intranet/core/servlet.rb,
lib/intranet/core/version.rb,
lib/intranet/core/haml_wrapper.rb

Overview

The core of the Intranet.

Defined Under Namespace

Modules: HamlWrapper

Constant Summary collapse

NAME =

The name of the gem.

'intranet-core'
VERSION =

The version of the gem, according to semantic versionning.

'2.5.0'
HOMEPAGE_URL =

The URL of the gem homepage.

'https://rubygems.org/gems/intranet-core'
SOURCES_URL =

The URL of the gem source code.

'https://bitbucket.org/ebling-mis/intranet-core'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, preferred_port = 80) ⇒ Core

Initializes a new Intranet core instance. The first available port will be used, starting at preferred_port. If preferred_port port is 80 and the user has not enough priviledges to use that port, port 8080 (or one of the following ports) will be used.

Parameters:

  • logger (Object)

    The logger.

  • preferred_port (Integer) (defaults to: 80)

    The preferred port for the web server.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/intranet/core.rb', line 25

def initialize(logger, preferred_port = 80)
  @logger = logger

  # Initialize translation module
  Intranet::Core::Locales.initialize

  # Initialize HAML wrapper
  Intranet::Core::HamlWrapper.initialize

  # Instanciate Intranet Builder and register page builders
  @builder = Intranet::Core::Builder.new(@logger)

  # Instanciate WebRick HTTP server
  @server = load_http_server(preferred_port)
  www_dir = File.join(__dir__, 'resources', 'www')
  mount_default_servlets(www_dir)
end

Instance Attribute Details

#portInteger (readonly)

The port currently used by the Intranet.

Returns:

  • (Integer)


18
19
20
# File 'lib/intranet/core.rb', line 18

def port
  @port
end

Instance Method Details

#home_url=(url) ⇒ Object

Defines the URL of the home page.

Parameters:

  • url (String)

    The absolute URL of the home page.

Raises:

  • (ArgumentError)

    If the URL is not an absolute path.



67
68
69
# File 'lib/intranet/core.rb', line 67

def home_url=(url)
  @builder.home_url = url
end

#register_module(responder, path, resources_dir = responder.resources_dir) ⇒ Object

Registers a new module to the core. The server must not be running. The module will then be accessible through the given path under the web server root.

Parameters:

  • responder (Intranet::AbstractResponder)

    The module responder instance.

  • path (Array)

    The path, relative to the web server root, representing the module root directory. The path cannot be empty and have more than 2 elements. Each element must only contain the following characters: -_a-z0-9.

  • resources_dir (String) (defaults to: responder.resources_dir)

    The path to the directory that contains additional resources required by the module. This directory should contain three subdirectories: haml/, locales/ and www/. The www/ is mounted under <path>/design on the web server.

Raises:

  • (ArgumentError)

    If the path is not valid.

  • (Errno::EALREADY)

    If the server is already running.



55
56
57
58
59
60
61
62
# File 'lib/intranet/core.rb', line 55

def register_module(responder, path, resources_dir = responder.resources_dir)
  raise Errno::EALREADY if @server.status != :Stop

  @builder.register(responder, path)
  module_add_servlet(path, resources_dir)
  module_add_locales(resources_dir)
  module_add_haml_templates(resources_dir)
end

#startObject

Starts the web server.



72
73
74
75
76
77
# File 'lib/intranet/core.rb', line 72

def start
  @logger.info("Intranet::Core: using locale '#{I18n.default_locale}'")
  @logger.info("Intranet::Core: running Intranet version #{VERSION}")
  # Start serving HTTP requests
  @server.start
end

#stopObject

Stops the web server and finalizes all registered module responders.



80
81
82
83
84
85
86
87
# File 'lib/intranet/core.rb', line 80

def stop
  @logger.info('Intranet::Runner: requesting system shutdown...')
  @server.shutdown
  while @server.status != :Stop
  end
  @builder.finalize
  @logger.close
end