Class: Cuca::App

Inherits:
Object
  • Object
show all
Defined in:
lib/cuca/app.rb,
lib/cuca/app_cgi.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.



132
133
134
135
# File 'lib/cuca/app.rb', line 132

def initialize
  $app    = self
  init_app
end

Instance Attribute Details

#app_pathObject (readonly)

Returns the value of attribute app_path.



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

def app_path
  @app_path
end

#cgiObject (readonly)

Returns the value of attribute cgi.



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

def cgi
  @cgi
end

#log_pathObject (readonly)

Returns the value of attribute log_path.



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

def log_path
  @log_path
end

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

#public_pathObject (readonly)

Returns the value of attribute public_path.



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

def public_path
  @public_path
end

#urlmapObject (readonly)

Returns the value of attribute urlmap.



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

def urlmap
  @urlmap
end

Class Method Details

.configObject



70
71
72
# File 'lib/cuca/app.rb', line 70

def self.config
  @@app_config
end

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

Yields:

  • (@@app_config)


66
67
68
# File 'lib/cuca/app.rb', line 66

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



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/cuca/app.rb', line 139

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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# 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)

 # 4th: Now let's run the actual page script code
 if Cuca::App.config['controller_naming'] then
   controller_class_name = Cuca::App.config['controller_naming'].call(@urlmap.action)
 else
   controller_class_name = @urlmap.action.capitalize+"Controller"
 end

 # Clear all hints
 Widget::clear_hints()

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


 # things fail in this block get error logged and/or displayed in browser
 begin
    # load controller
    begin
       controller_module.module_eval(File.read(script), script)  unless \
                   controller_module.const_defined?(controller_class_name.intern)
    rescue SyntaxError,LoadError => e
         err = get_error("Can not load script", e,
         Cuca::App.config['display_errors'], Cuca::App.config['http_500'])
         @cgi.out('status' => 'SERVER_ERROR') { err }
         return
    end

    # Catch a common user error
    raise Cuca::ApplicationException.new("Could not find #{controller_class_name} defined in #{script}") \
         unless controller_module.const_defined?(controller_class_name.intern)


    # run controller
    (status, mime, content, headers) = Sandbox.run(controller_class_name,
                          @urlmap.action_module, @urlmap.assigns,
                          @cgi.request_method, @urlmap.subcall)

    logger.debug "CGICall OK: #{status}/#{mime}"


    @cgi.out(headers.merge( { 'type' => mime, 'status' => status} )) {content}

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

     logger.info "CGICall Syntax Error"
     return


 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:



152
153
154
155
156
157
158
159
160
# File 'lib/cuca/app.rb', line 152

def load_support_files(urlmap)  # :nodoc:
  all_support_directories(urlmap.path_tree) do |dir, proc|
    if proc then
       autoload_files(dir, proc)
    else
       include_files(dir)
    end
  end
end