Changeset 9252
- Timestamp:
- 09/26/08 17:13:56 (1 year ago)
- Files:
-
- helma-ng/trunk/apps/demo/main.js (modified) (5 diffs)
- helma-ng/trunk/apps/demo/setup.js (added)
- helma-ng/trunk/apps/demo/webmodule.js (modified) (1 diff)
- helma-ng/trunk/apps/storage/main.js (modified) (1 diff)
- helma-ng/trunk/apps/storage/setup.js (modified) (1 diff)
- helma-ng/trunk/modules/helma/httpserver.js (modified) (3 diffs)
- helma-ng/trunk/modules/helma/webapp.js (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
helma-ng/trunk/apps/demo/main.js
r9249 r9252 3 3 var render = loadModule('helma.skin').render; 4 4 // loadModule('helma.continuation'); 5 var logging = loadModule('helma.logging'); 6 logging.enableResponseLog(); 7 var log = logging.getLogger(__name__); 5 var helma = { 6 logging : loadModule('helma.logging') 7 } 8 helma.logging.enableResponseLog(); 9 var log = helma.logging.getLogger(__name__); 8 10 9 11 var mount = { … … 12 14 13 15 // the main action is invoked for http://localhost:8080/ 14 function main_action(req, res) {16 function index(req, res) { 15 17 res.render('skins/index.html', { title: 'Welcome to Helma NG' }); 16 18 } 17 19 18 20 // demo for skins, macros, filters 19 function skins _action(req, res) {21 function skins(req, res) { 20 22 var context = { 21 23 title: 'Skin Demo', … … 27 29 28 30 // demo for log4j logging 29 function logging _action(req, res) {31 function logging(req, res) { 30 32 // make sure responselog is enabled 31 var hasResponseLog = logging.responseLogEnabled();33 var hasResponseLog = helma.logging.responseLogEnabled(); 32 34 if (!hasResponseLog) { 33 logging.enableResponseLog();35 helma.logging.enableResponseLog(); 34 36 log.debug("enabling response log"); 35 37 } … … 46 48 if (!hasResponseLog) { 47 49 log.debug("disabling response log"); 48 logging.disableResponseLog();50 helma.logging.disableResponseLog(); 49 51 } 50 logging.flushResponseLog();52 helma.logging.flushResponseLog(); 51 53 } 52 54 53 55 // demo for continuation support 54 function continuation _action(req, res) {56 function continuation(req, res) { 55 57 56 58 // local data - this is the data that is shared between resuming and suspension … … 118 120 } 119 121 120 121 function test_action(req, res) {122 res.buffer.write(req, req.session);123 }124 125 122 // main method called to start application 126 123 if (__name__ == "__main__") { helma-ng/trunk/apps/demo/webmodule.js
r9246 r9252 1 1 // a simple web app/module 2 2 3 function main_action(req, res) {3 function index(req, res) { 4 4 var context = { 5 5 title: 'Module Demo', helma-ng/trunk/apps/storage/main.js
r9249 r9252 7 7 // the main action is invoked for http://localhost:8080/ 8 8 // this also shows simple skin rendering 9 function main_action(req, res) {9 function index(req, res) { 10 10 if (req.data.save) { 11 11 createBook(req, res); helma-ng/trunk/apps/storage/setup.js
r9251 r9252 1 var urls =[ 2 [ /^$/, 'main.index' ] 3 ] helma-ng/trunk/modules/helma/httpserver.js
r9250 r9252 27 27 * <li>staticDir ('static')</li> 28 28 * <li>staticMountpoint ('/static')</li> 29 * <li>servletParams ({ moduleName: ' main',29 * <li>servletParams ({ moduleName: 'helma.webapp', 30 30 * functionName: 'handleRequest', 31 31 * requestTimeout: 30 })</li> … … 58 58 // java.lang.System.err.println("idmap: " + idMap); 59 59 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)); 62 62 var staticHolder = new jetty.servlet.ServletHolder(jetty.servlet.DefaultServlet); 63 63 staticCtx.addServlet(staticHolder, "/*"); … … 69 69 var servletHolder = new jetty.servlet.ServletHolder(helmaServlet); 70 70 var params = config.servletParams || { 71 moduleName: ' main',71 moduleName: 'helma.webapp', 72 72 functionName: 'handleRequest', 73 73 requestTimeout: 30 helma-ng/trunk/modules/helma/webapp.js
r9249 r9252 47 47 return; 48 48 } 49 49 50 // 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"); 58 64 } 59 65 } 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 } 72 106 } 73 } 74 try { 75 handler[action].call(handler, req, res); 107 notfound(req, res); 76 108 } catch (e) { 77 109 error(req, res, e); … … 120 152 function start(config) { 121 153 // 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); 123 163 } 124 164 … … 141 181 } 142 182 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(); 152 184 }