application.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // application.js
  2. // This file contains a function for serving the application specific requests.
  3. var servehandler = require( './serve-handler' ),
  4. helpers = require( './helpers' ),
  5. persistence = require( './persistence' ),
  6. admin = require( './admin' );
  7. // Requests are attempted to be served as if they pertain to a specific application.
  8. // If any of these attempts succesfully serve the request, true is returned.
  9. // If these attempts do not succesfully serve the request, false is returned.
  10. function Serve( request, response, parsedRequest ) {
  11. // The first step is to attempt to serve the file out of the support/client/lib directory structure.
  12. // If there is no private path, and we have an application request, then we're the initial request and
  13. // thus we should be serving the default 'load the application' index page, which lives at
  14. // support/client/lib/index.html, hence the fileName logic.
  15. var fileName = parsedRequest[ 'private_path' ];
  16. if ( fileName == undefined ) {
  17. fileName = "index.html";
  18. }
  19. if ( servehandler.File( request, response, helpers.JoinPath( global.vwfRoot + "/support/client/lib/", fileName ) ) ) {
  20. return true;
  21. }
  22. // If the file was not served out of the support/client/lib path, the next attempt is to test if the file can be served out of
  23. // the actual application path in the public directory.
  24. if ( servehandler.File( request, response, helpers.JoinPath( global.applicationRoot, parsedRequest[ 'public_path' ], parsedRequest[ 'private_path' ] ) ) ) {
  25. return true;
  26. }
  27. // If the file was not found in the application path within the public directory, the next step is to see if it is a component within the
  28. // application path within the public directory.
  29. if ( servehandler.Component( request, response, helpers.JoinPath( global.applicationRoot, parsedRequest[ 'public_path' ], parsedRequest[ 'private_path' ] ) ) ) {
  30. return true;
  31. }
  32. // If the request was still not served, try and see if the persistence functionality will serve this request.
  33. if ( persistence.Serve( request, response, parsedRequest ) ) {
  34. return true;
  35. }
  36. // Finally, test if the admin functionality can serve this request.
  37. if ( admin.Serve( request, response, parsedRequest ) ) {
  38. return true;
  39. }
  40. //Nothing managed to serve this request, return false.
  41. return false;
  42. }
  43. // Make the application functionality available via global since
  44. // persistence needs to be able to call this, and this cannot
  45. // require persistence AND have persistence require this.
  46. global.application = {};
  47. global.application.Serve = Serve;
  48. exports.Serve = Serve;