Permalink
Browse files

Allow partials living in app/views/partials to be sent as static file…

…s to the front-end.
  • Loading branch information...
1 parent c882494 commit 27fe544f1e8cea661d7d284c9efc1293c1402d3c @larzconwell larzconwell committed Oct 31, 2012
Showing with 31 additions and 16 deletions.
  1. +31 −16 lib/app.js
View
@@ -328,7 +328,9 @@ var App = function () {
, controller
, controllerName
, actionName
- , staticPath
+ , staticUrl
+ , publicPath
+ , partialsPath
, staticResp
, nonMethodRoutes
, acceptableMethods
@@ -343,7 +345,8 @@ var App = function () {
}
, finish
, accessTime = (new Date()).getTime()
- , inFlighId;
+ , inFlighId
+ , sendStaticFile;
reqUrl = req.url;
// Sanitize URL; reduce multiple slashes to single slash
@@ -576,31 +579,44 @@ var App = function () {
}
// Either static, 404, or 405
else {
- // Get the path to the file
- staticPath = geddy.config.staticFilePath + reqUrl;
// Ignore querystring
- staticPath = staticPath.split('?')[0];
+ staticUrl = reqUrl.split('?')[0];
// Decode path (e.g. %20)
- staticPath = decodeURIComponent(staticPath);
+ staticUrl = decodeURIComponent(staticUrl);
+ // Get path to public file path
+ publicPath = geddy.config.staticFilePath + reqUrl;
+ // Get path to partials path(Assume the path includes "partials/")
+ partialsPath = path.join(cwd, 'app', 'views', reqUrl);
+
+ // Send a static file path
+ sendStaticFile = function (p) {
+ staticResp = new response.Response(resp);
- if (utils.file.existsSync(staticPath)) {
// May be a path to a directory, with or without a trailing
// slash -- any trailing slash has already been stripped by
// this point
- if (fs.statSync(staticPath).isDirectory()) {
+ if (fs.statSync(p).isDirectory()) {
// TODO: Make the name of any index file configurable
- staticPath = path.join(staticPath, 'index.html');
- if (utils.file.existsSync(staticPath) && fs.statSync(staticPath).isFile()) {
- staticResp = new response.Response(resp);
- staticResp.sendFile(staticPath);
+ p = path.join(p, 'index.html');
+ if (utils.file.existsSync(p) && fs.statSync(p).isFile()) {
+ staticResp.sendFile(p);
}
}
// Path to an actual file. Just serve it up
- else if (fs.statSync(staticPath).isFile()) {
- staticResp = new response.Response(resp);
- staticResp.sendFile(staticPath);
+ else if (fs.statSync(p).isFile()) {
+ staticResp.sendFile(p);
}
+ };
+
+ // If the path is in the static file path send it
+ if (utils.file.existsSync(publicPath)) {
+ sendStaticFile(publicPath);
}
+ // If the path includes the full partials path and is in the partials file in views send it
+ else if (staticUrl.match('partials/') && utils.file.existsSync(partialsPath)) {
+ sendStaticFile(partialsPath);
+ }
+ // Must be a 404 or 405
else {
// 405?
nonMethodRoutes = router.all(reqUrl);
@@ -628,7 +644,6 @@ var App = function () {
errResp.send(err.message, err.statusCode,
{'Content-Type': 'text/html'});
}
-
}
}
});

1 comment on commit 27fe544

@Techwraith
Contributor

Come to think of it, we need two helper functions:

  • makePublic where you can pass a directory name, and a path and it will make it public for you:
makePublic('app/views/partials', 'partials');
// make anything in 'app/views/partials' public on http://localhost:4000/partials/*
  • concatDir where you can pass a directory name and a file template, and it will concat all the file contents together:
var content = concatDir("app/views/partials", 
        "(function(){geddy.template = geddy.template || {}; geddy.template.{{name}} = '{{content}}';}())");

The first would be useful for this case, and the second would be useful for the model case (and perhaps this partials case too).

Please sign in to comment.