Changeset 9252

Show
Ignore:
Timestamp:
09/26/08 17:13:56 (1 year ago)
Author:
hannes
Message:

Implement regexp based url dispatching. Url patterns are looked up as property 'urls' in module 'setup'.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • helma-ng/trunk/apps/demo/main.js

    r9249 r9252  
    33var render = loadModule('helma.skin').render; 
    44// loadModule('helma.continuation'); 
    5 var logging = loadModule('helma.logging'); 
    6 logging.enableResponseLog(); 
    7 var log = logging.getLogger(__name__); 
     5var helma = { 
     6    logging : loadModule('helma.logging') 
     7
     8helma.logging.enableResponseLog(); 
     9var log = helma.logging.getLogger(__name__); 
    810 
    911var mount = { 
     
    1214 
    1315// the main action is invoked for http://localhost:8080/ 
    14 function main_action(req, res) { 
     16function index(req, res) { 
    1517    res.render('skins/index.html', { title: 'Welcome to Helma NG' }); 
    1618} 
    1719 
    1820// demo for skins, macros, filters 
    19 function skins_action(req, res) { 
     21function skins(req, res) { 
    2022    var context = { 
    2123        title: 'Skin Demo', 
     
    2729 
    2830// demo for log4j logging 
    29 function logging_action(req, res) { 
     31function logging(req, res) { 
    3032    // make sure responselog is enabled 
    31     var hasResponseLog = logging.responseLogEnabled(); 
     33    var hasResponseLog = helma.logging.responseLogEnabled(); 
    3234    if (!hasResponseLog) { 
    33         logging.enableResponseLog(); 
     35        helma.logging.enableResponseLog(); 
    3436        log.debug("enabling response log"); 
    3537    } 
     
    4648    if (!hasResponseLog) { 
    4749        log.debug("disabling response log"); 
    48         logging.disableResponseLog(); 
     50        helma.logging.disableResponseLog(); 
    4951    } 
    50     logging.flushResponseLog(); 
     52    helma.logging.flushResponseLog(); 
    5153} 
    5254 
    5355// demo for continuation support 
    54 function continuation_action(req, res) { 
     56function continuation(req, res) { 
    5557 
    5658    // local data - this is the data that is shared between resuming and suspension 
     
    118120} 
    119121 
    120  
    121 function test_action(req, res) { 
    122     res.buffer.write(req, req.session); 
    123 } 
    124  
    125122// main method called to start application 
    126123if (__name__ == "__main__") { 
  • helma-ng/trunk/apps/demo/webmodule.js

    r9246 r9252  
    11// a simple web app/module 
    22 
    3 function main_action(req, res) { 
     3function index(req, res) { 
    44    var context = { 
    55        title: 'Module Demo', 
  • helma-ng/trunk/apps/storage/main.js

    r9249 r9252  
    77// the main action is invoked for http://localhost:8080/ 
    88// this also shows simple skin rendering 
    9 function main_action(req, res) { 
     9function index(req, res) { 
    1010    if (req.data.save) { 
    1111        createBook(req, res); 
  • helma-ng/trunk/apps/storage/setup.js

    r9251 r9252  
     1var urls =[ 
     2    [ /^$/, 'main.index' ] 
     3] 
  • helma-ng/trunk/modules/helma/httpserver.js

    r9250 r9252  
    2727     * <li>staticDir ('static')</li> 
    2828     * <li>staticMountpoint ('/static')</li> 
    29      * <li>servletParams ({ moduleName: 'main', 
     29     * <li>servletParams ({ moduleName: 'helma.webapp', 
    3030     *                      functionName: 'handleRequest', 
    3131     *                      requestTimeout: 30 })</li> 
     
    5858                // java.lang.System.err.println("idmap: " + idMap); 
    5959                var staticCtx = idMap.get('staticContext'); 
    60                 if (staticCtx) { 
    61                     staticCtx.setResourceBase(getResource(config.staticDir || 'static')); 
     60                if (staticCtx && typeof config.staticDir == "string") { 
     61                    staticCtx.setResourceBase(getResource(config.staticDir)); 
    6262                    var staticHolder = new jetty.servlet.ServletHolder(jetty.servlet.DefaultServlet); 
    6363                    staticCtx.addServlet(staticHolder, "/*"); 
     
    6969                    var servletHolder = new jetty.servlet.ServletHolder(helmaServlet); 
    7070                    var params = config.servletParams || { 
    71                         moduleName: 'main', 
     71                        moduleName: 'helma.webapp', 
    7272                        functionName: 'handleRequest', 
    7373                        requestTimeout: 30 
  • helma-ng/trunk/modules/helma/webapp.js

    r9249 r9252  
    4747        return; 
    4848    } 
     49 
    4950    // resolve path and invoke action 
    50     var path = req.path.split('/'); 
    51     var handler = this; 
    52     // first element is always empty string if path starts with '/' 
    53     for (var i = 1; i < path.length -1; i++) { 
    54         handler = handler[path[i]]; 
    55         if (!handler) { 
    56             notfound(req, res); 
    57             return; 
     51    var path = req.path; 
     52    if (path.startsWith('/')) { 
     53        // strip leading slash 
     54        path = path.slice(1) 
     55    } 
     56 
     57    function getRegExp(pattern) { 
     58        if (pattern instanceof RegExp) { 
     59            return pattern; 
     60        } else if (typeof pattern == "string") { 
     61            return new RegExp(pattern); 
     62        } else { 
     63            throw Error("Pattern must be a regular expression or string"); 
    5864        } 
    5965    } 
    60     var lastPart = path[path.length - 1]; 
    61     var action = lastPart ? 
    62                  lastPart.replace('.', '_', 'g') + '_action' : 
    63                  'main_action'; 
    64     // res.writeln(handler, action, handler[action]); 
    65     if (!(handler[action] instanceof Function)) { 
    66         if (!req.path.endsWith('/') && handler[lastPart] && 
    67             handler[lastPart]['main_action'] instanceof Function) { 
    68             res.redirect(req.path + '/'); 
    69         } else if (!handler[action]) { 
    70             notfound(req, res); 
    71             return 
     66 
     67    try { 
     68        log.debug('resolving path ' + path); 
     69        var setup = loadModule('setup'); 
     70        log.debug('got setup: ' + setup); 
     71        if (setup.urls instanceof Array) { 
     72            var urls = setup.urls; 
     73            for (var i = 0; i < urls.length; i++) { 
     74                log.debug("checking url line: " + urls[i]); 
     75                var match = getRegExp(urls[i][0]).exec(path); 
     76                log.debug("got match: " + match); 
     77                if (match != null) { 
     78                    var action = urls[i][1]; 
     79                    log.debug("action: " + action); 
     80                    if (typeof action == "string") { 
     81                        log.debug("action is string"); 
     82                        var dot = action.lastIndexOf('.'); 
     83                        if (dot < 0) { 
     84                            throw Error('Action must be of form "module.function"'); 
     85                        } 
     86                        var module = action.slice(0, dot); 
     87                        var func = action.slice(dot + 1); 
     88                        action = loadModule(module)[func]; 
     89                        if (log.isDebugEnabled()) { 
     90                            log.debug("resolved action: " + action); 
     91                        } 
     92                    } else if (typeof action == "function") { 
     93                        throw Error('Action must either be a string or a function'); 
     94                    } 
     95                    // got a match - add any regexp groups as additional action arguments 
     96                    var args = [req, res]; 
     97                    for (var j = 1; j < match.length; j++) { 
     98                        args.push(match[j]); 
     99                    } 
     100                    if (typeof action == "function") { 
     101                        action.apply(null, args); 
     102                        return; 
     103                    } 
     104                } 
     105            } 
    72106        } 
    73     } 
    74     try { 
    75         handler[action].call(handler, req, res); 
     107        notfound(req, res); 
    76108    } catch (e) { 
    77109        error(req, res, e); 
     
    120152function start(config) { 
    121153    // start jetty http server 
    122     server.start(config); 
     154    var setup; 
     155    var httpConf; 
     156    try { 
     157        setup = config || loadModule('setup'); 
     158        httpConf = setup.httpConf; 
     159    } catch (noSetup) { 
     160        log.info('Couldn\'t load setup module - using defaults'); 
     161    } 
     162    server.start(httpConf); 
    123163} 
    124164 
     
    141181    } 
    142182    log.info('Setup module search: ' + system.getRepositories()); 
    143     var setup; 
    144     var httpConf; 
    145     try { 
    146         setup = loadModule('setup'); 
    147         httpConf = setup.httpConf; 
    148     } catch (noSetup) { 
    149         log.info('Couldn\'t load setup module - using defaults'); 
    150     } 
    151     start(httpConf); 
     183    start(); 
    152184}