Class: Mentawai::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/mentawai/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ Server

Returns a new instance of Server.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mentawai/server.rb', line 12

def initialize(path = nil)
  
  @host = "127.0.0.1"
  @port = 8081
  
  @apps = Hash.new
  
  if not path
    @loader = nil
  else
    @loader = Mentawai::Loader.new(path + '/Mentawai')
    @loader.reloadFiles
  end
end

Instance Attribute Details

#appsObject (readonly)

Returns the value of attribute apps.



27
28
29
# File 'lib/mentawai/server.rb', line 27

def apps
  @apps
end

#hostObject

Returns the value of attribute host.



10
11
12
# File 'lib/mentawai/server.rb', line 10

def host
  @host
end

#portObject

Returns the value of attribute port.



10
11
12
# File 'lib/mentawai/server.rb', line 10

def port
  @port
end

Instance Method Details

#add_application(app_options) ⇒ Object Also known as: add



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mentawai/server.rb', line 29

def add_application(app_options)
  
  app = Application.new(app_options)
  
  raise "Context already exists: " + app.contextPath if @apps.has_key?(app.contextPath)
  
  # add /WEB-INF/src and lib directory to the load path...
  $:.unshift(".#{app.contextPathDir}/WEB-INF/lib")
  $:.unshift(".#{app.contextPathDir}/WEB-INF/src")
  
  @apps[app.contextPath] = app
  
  app.reloadAppManager
  
end

#call(env) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mentawai/server.rb', line 51

def call(env)

  # force reload of any mentawai file if modified...
  filesLoaded = 0; filesLoaded = @loader.reloadFiles if @loader

  req = Rack::Request.new(env)
  res = Rack::Response.new(env)

  contextPath = find_context_path(req.fullpath)
  
  env['menta.contextPath'] = contextPath
  
  app = @apps[contextPath]
  app.reloadAppManager(filesLoaded > 0)
  
  controller = Mentawai::Core::Controller.new(app)
  controller.service(env, req, res)
end

#contextsObject



47
48
49
# File 'lib/mentawai/server.rb', line 47

def contexts
  @apps.keys
end

#find_context_path(fullpath) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/mentawai/server.rb', line 70

def find_context_path(fullpath)
  # Extract context path from URL...
  if fullpath =~ /(\/[^\/]*)/ then
    contextPath = $1
  else
    raise "Cannot get context path: " + env.fullpath
  end
  
  # Decide about context path...
  if @apps.has_key?(contextPath) then
    contextPath
  elsif @apps.has_key?('/') then
    '/'
  else
    raise "Cannot find context: " + contextPath
  end
end

#startObject



88
89
90
91
92
93
94
# File 'lib/mentawai/server.rb', line 88

def start
  
  session_config = { :cookie_expire_after => 0, :session_expire_after => 10 }
  session_server = Mentawai::Session::MentaSession.new(self, session_config)
  config = {:Host => @host, :Port => @port }
  Mentawai::Handler::MentaHandler.run(self, session_server, config)
end