Skip to content

Commit 3a842f7

Browse files
committed
Move some routing logic into App.
1 parent accb9ce commit 3a842f7

File tree

3 files changed

+60
-56
lines changed

3 files changed

+60
-56
lines changed

coder-apps/common/coderlib/app/app.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,63 @@ App.prototype._load = function(data) {
4242
}
4343
}
4444

45+
App.prototype.exec = function (req, res, path) {
46+
var self = this;
47+
res.locals["app_name"] = this.name;
48+
res.locals["app_url"] = this.appURL;
49+
res.locals["static_url"] = this.staticURL;
50+
res.locals["device_name"] = coderlib.device.name;
51+
res.locals["coder_owner"] = coderlib.device.owner;
52+
res.locals["coder_color"] = coderlib.device.color;
53+
54+
55+
//Redirect to sign-in for unauthenticated users
56+
var user = coderlib.auth.isAuthenticated(req, res);
57+
if ( !user && !this.metadata.public) {
58+
util.log( "redirect: " + '/app/auth' );
59+
res.redirect('/app/auth');
60+
return;
61+
}
62+
63+
this.require(function(err, userapp) {
64+
if (err) {
65+
res.send(500);
66+
return;
67+
}
68+
69+
var routes = [];
70+
if ( req.route.method === 'get' ) {
71+
routes = userapp.get_routes;
72+
} else if ( req.route.method === 'post' ) {
73+
routes = userapp.post_routes;
74+
}
75+
76+
if ( routes ) {
77+
var found = false;
78+
for ( var i in routes ) {
79+
route = routes[i];
80+
if ( route['path'] instanceof RegExp ) {
81+
var match = route['path'].exec( path );
82+
if ( match ) {
83+
userapp[route['handler']]( self, req, res, match );
84+
found = true;
85+
break;
86+
}
87+
88+
} else if ( route['path'] === path ) {
89+
userapp[route['handler']]( self, req, res );
90+
found = true;
91+
break;
92+
}
93+
}
94+
95+
if ( !found ) {
96+
res.send(404);
97+
}
98+
}
99+
});
100+
}
101+
45102
var LocalApp = exports.LocalApp = function(name) {
46103
LocalApp.super_.call(this, name);
47104

coder-apps/common/coderlib/app/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ exports.app = app.LocalApp.find;
5151
exports.listApps = app.LocalApp.list;
5252
exports.createApp = app.LocalApp.create;
5353

54+
exports.auth = require('../../auth/app/index');
55+
5456

5557
exports.device = function() {
5658
var devicefile = process.cwd() + "/device.json";

coder-base/server.js

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -60,62 +60,7 @@ var apphandler = function( req, res, appdir ) {
6060
return;
6161
}
6262

63-
res.locals["app_name"] = appname;
64-
res.locals["app_url"] = "/app/" + appname;
65-
res.locals["static_url"] = "/static/apps/" + appname;
66-
res.locals["device_name"] = coderlib.device.name;
67-
res.locals["coder_owner"] = coderlib.device.owner;
68-
res.locals["coder_color"] = coderlib.device.color;
69-
70-
//Redirect to sign-in for unauthenticated users
71-
user = auth.isAuthenticated(req, res);
72-
if ( !user && !app.metadata.public) {
73-
util.log( "redirect: " + '/app/auth' );
74-
res.redirect('/app/auth');
75-
return;
76-
}
77-
78-
app.require(function(err, userapp) {
79-
if (err) {
80-
res.send(500);
81-
return;
82-
}
83-
84-
var routes = [];
85-
if ( req.route.method === 'get' ) {
86-
routes = userapp.get_routes;
87-
} else if ( req.route.method === 'post' ) {
88-
routes = userapp.post_routes;
89-
}
90-
91-
if ( routes ) {
92-
var found = false;
93-
for ( var i in routes ) {
94-
route = routes[i];
95-
if ( route['path'] instanceof RegExp ) {
96-
var m = route['path'].exec( apppath );
97-
if ( m ) {
98-
userapp[route['handler']]( app, req, res, m );
99-
found = true;
100-
break;
101-
}
102-
103-
} else if ( route['path'] === apppath ) {
104-
userapp[route['handler']]( app, req, res );
105-
found = true;
106-
break;
107-
}
108-
109-
}
110-
111-
if ( !found ) {
112-
res.status( 404 );
113-
res.render('404', {
114-
title: 'error'
115-
});
116-
}
117-
}
118-
});
63+
app.exec(req, res, apppath);
11964
});
12065
};
12166

0 commit comments

Comments
 (0)