Class: Cuca::App

Inherits:
Object
  • Object
show all
Defined in:
lib/cuca/app.rb

Overview

Cuca Application

A Cuca::App object will be created directly by the dispatcher - which again is the direct cgi or fastcgi script that get run by the webserver. Normally you just create a Cuca::App object and run the cgicall function with optional with a cgi object as paramenter. Before doing that you must set $cuca_path to the root of your appication structure.

Constant Summary collapse

@@app_config =
Cuca::Config.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApp

Returns a new instance of App.



121
122
123
124
# File 'lib/cuca/app.rb', line 121

def initialize
  $app    = self
  init_app
end

Instance Attribute Details

#app_pathObject (readonly)

Returns the value of attribute app_path.



50
51
52
# File 'lib/cuca/app.rb', line 50

def app_path
  @app_path
end

#cgiObject (readonly)

Returns the value of attribute cgi.



50
51
52
# File 'lib/cuca/app.rb', line 50

def cgi
  @cgi
end

#log_pathObject (readonly)

Returns the value of attribute log_path.



50
51
52
# File 'lib/cuca/app.rb', line 50

def log_path
  @log_path
end

#loggerObject (readonly)

Returns the value of attribute logger.



50
51
52
# File 'lib/cuca/app.rb', line 50

def logger
  @logger
end

#public_pathObject (readonly)

Returns the value of attribute public_path.



50
51
52
# File 'lib/cuca/app.rb', line 50

def public_path
  @public_path
end

#urlmapObject (readonly)

Returns the value of attribute urlmap.



50
51
52
# File 'lib/cuca/app.rb', line 50

def urlmap
  @urlmap
end

Class Method Details

.configObject



60
61
62
# File 'lib/cuca/app.rb', line 60

def self.config
  @@app_config
end

.configure {|@@app_config| ... } ⇒ Object

Yields:

  • (@@app_config)


56
57
58
# File 'lib/cuca/app.rb', line 56

def self.configure
  yield @@app_config
end

Instance Method Details

#all_support_directories(path_tree) ⇒ Object

this will yield all support directories from base path and if defined the naming proc else nil



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/cuca/app.rb', line 128

def all_support_directories(path_tree)
  path_tree.each do |t|
      (App::config['include_directories'] || []).each do |id|
         if id.instance_of?(Hash) then 
             yield "#{t}/#{id[:dir]}", id[:class_naming]
         else 
      yield "#{t}/#{id}", nil
         end
      end
  end  
end

#cgicall(cgi = nil) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/cuca/app.rb', line 185

def cgicall(cgi = nil)
 @cgi = cgi || CGI.new
 

 #
 # 1st priority: Serve a file if it exists in the 'public' folder
 #
 file = @public_path + '/' + @cgi.path_info
 if File.exists?(file) && File.ftype(file) == 'file' then
   require 'cuca/mimetypes'
   mt = MimeTypes.new
   f = File.new(file)
   extension = file.scan(/.*\.(.*)$/)[0][0] if file.include?('.')
   extension ||= 'html'
   mime = mt[extension] || 'text/html'
   @cgi.out('type' => mime, 
            'expires' => (Time.now+App.config['http_static_content_expires'])) { f.read }
   f.close
   return
 end

 init_call(@cgi.path_info)

 # If config (urlmap) couldn't find a script then let's give up
 # with a file-not-found 404 error
 if @urlmap.nil? then
    begin
     file = "#{@public_path}/#{Cuca::App.config['http_404']}"
     c = File.open(file).read
     @cgi.out('status' => 'NOT_FOUND', 'type' => 'text/html') { c }
    rescue => e
     @cgi.out('status' => 'NOT_FOUND', 'type' => 'text/html') { "404 - File not found!" }
    end
    return
 end

 logger.info "CGICall on #{@urlmap.url} - #{@urlmap.script}"
 script = @urlmap.script

 #
 # 2nd: Check if we have a script for requested action
 #
 if (!File.exists?(script)) then
   @cgi.out { "Script not found: #{script}" }
   return
 end

 # 3rd: Load additional files
 load_support_files(@urlmap)

 test = ApplicationController.new


 # 4th: Now let's run the actual page script code
 controller_class_name = @urlmap.action.capitalize+"Controller"
 
 # Clear all hints
 Widget::clear_hints()

 # Load the code of the action into the module
 controller_module = @urlmap.action_module


 controller_module.module_eval(File.open(script).read, script)  unless \
                 controller_module.const_defined?(controller_class_name.intern)

 # Catch a common user error
 if !controller_module.const_defined?(controller_class_name.intern) then 
    @cgi.out { "Could not find #{controller_class_name} defined in #{script}" }
    return
 end

 #
 # Load the controller
 #
 begin

    (status, mime, content) = Sandbox.run(controller_class_name, 
                          @urlmap.action_module, @urlmap.assigns, 
                          @cgi.request_method, @urlmap.subcall)
 
    logger.info "CGICall OK"
    @cgi.out('type' => mime, 'status' => status) {content}

 rescue Cuca::ApplicationException => e
     err = get_error("Application Error", e,
             Cuca::App.config['display_errors'], Cuca::App.config['http_500'])
     @cgi.out('status' => 'SERVER_ERROR') { err }

    logger.info "CGICall Application Error"
     return

 rescue  => e
     err = get_error("System Error", e, 
             Cuca::App.config['display_errors'], Cuca::App.config['http_500'])
     @cgi.out('status' => 'SERVER_ERROR') { err }

     logger.info "CGICall System Error"
     return 

 end
end

#load_support_files(urlmap) ⇒ Object

:nodoc:



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/cuca/app.rb', line 141

def load_support_files(urlmap)  # :nodoc:
#   $stderr.puts "======== load support files ========="
#   $stderr.puts "Inc Dir: #{App::config['include_directories'].inspect}"
#   $stderr.puts "Path tr: #{@conf['PATH_TREE'].inspect}"
#   $stderr.puts "====================================="
  
#   db = $:.dup
#   all_support_directories(@conf['APP_PATH'])  { |d,p| $: << d }

#   all_support_directories(@app_path, @urlmap.path_tree) do |dir, proc|
#    $stderr.puts "PATH TREE: #{urlmap.path_tree.inspect}"
  all_support_directories(urlmap.path_tree) do |dir, proc|
#     $stderr.puts "Support Directory #{dir}"
    if proc then
       autoload_files(dir, proc)
    else
       include_files(dir)
    end   
  end
end