unit22

Untitled

Feb 17th, 2012
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. (function () {
  2. var express = require("express"),
  3. fluid = require("infusion"),
  4. gpii = fluid.registerNamespace("gpii");
  5. fluid.require("../../../../../dataSource.js");
  6.  
  7. process.on("uncaughtException", function (err) {
  8. console.log("Uncaught Exception: " + err);
  9. process.exit(1);
  10. });
  11.  
  12. process.on("SIGTERM", function () {
  13. process.exit(0);
  14. });
  15.  
  16. var getBaseDir = function () {
  17. return __dirname;
  18. };
  19.  
  20. var buildPath = function (pathSeg) {
  21. return path.join(getBaseDir(), pathSeg);
  22. };
  23.  
  24. fluid.defaults("gpii.preferencesServer", {
  25. gradeNames: ["fluid.littleComponent", "autoInit"],
  26. preInitFunction: "gpii.preferencesServer.preInit",
  27. finalInitFunction: "gpii.preferencesServer.finalInit",
  28. components: {
  29. userDataSource: {
  30. type: "gpii.dataSource",
  31. options: {
  32. termMap: {
  33. token: "%token"
  34. }
  35. }
  36. }
  37. }
  38. });
  39.  
  40. gpii.preferencesServer.preInit = function (that) {
  41. that.server = express.createServer(
  42. // In case we want to support https
  43. // {
  44. // key: fs.readFileSync('path/to/key.pem'),
  45. // cert: fs.readFileSync('path/to/cert.pem')
  46. // }
  47. );
  48. that.server.configure(function () {
  49. that.server.use(express.bodyParser());
  50. });
  51. that.server.configure("production", function () {
  52. // Set production options.
  53. fluid.staticEnvironment.production = fluid.typeTag("gpii.production");
  54. });
  55. that.server.configure("development", function () {
  56. // Set development options.
  57. fluid.staticEnvironment.production = fluid.typeTag("gpii.development");
  58. });
  59. };
  60.  
  61. gpii.preferencesServer.finalInit = function (that) {
  62. that.server.all("/user/:id", function (req, res, next) {
  63. // params available: id
  64. req.id = req.params.id;
  65. next();
  66. });
  67.  
  68. that.server.get("/user/:id", function (req, res) {
  69. that.userDataSource.get({
  70. token: req.id
  71. }, function (resp) {
  72. res.send(resp, 200);
  73. }, function (message, error) {
  74. console.log(message);
  75. });
  76. });
  77.  
  78. that.server.listen(8080);
  79. };
  80.  
  81. fluid.demands("userDataSource", "gpii.development", {
  82. options: {
  83. url: "%db/test/data/%token.json"
  84. }
  85. });
  86.  
  87. fluid.demands("userDataSource", "gpii.production", {
  88. options: {
  89. host: "0.0.0.0",
  90. port: 5984,
  91. url: "%db/user/%token"
  92. }
  93. });
  94.  
  95. gpii.preferencesServer();
  96.  
  97. })();
Advertisement
Add Comment
Please, Sign In to add comment