Advertisement
evilqubit

zabre

Dec 18th, 2014
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. #!/bin/env node
  2. // OpenShift sample Node application
  3. var express = require('express');
  4. var fs = require('fs');
  5.  
  6. /**
  7. *Twitter
  8. */
  9. var Twit = require('twit')
  10.  
  11. var T = new Twit({
  12. consumer_key: '2DK003ueNLdRD7VxkywGWRoPo'
  13. , consumer_secret: 'yOaHBYv06IPH5SOP5JUYFzdLFekIPVyvCqBWUJXNTpQmYkhhpv'
  14. , access_token: '77060821-bShjezKztJmEVv6rb2YrpOmEpB3rNYpAtSQXl9xOe'
  15. , access_token_secret: 'Z3P6hUq4GX9U380TvHvLwl9eLuFiEK1kZTUj43I0rpO1e'
  16. })
  17.  
  18.  
  19.  
  20. /**
  21. * Define the sample application.
  22. */
  23. var SampleApp = function() {
  24.  
  25. // Scope.
  26. var self = this;
  27.  
  28.  
  29. /* ================================================================ */
  30. /* Helper functions. */
  31. /* ================================================================ */
  32.  
  33. /**
  34. * Set up server IP address and port # using env variables/defaults.
  35. */
  36. self.setupVariables = function() {
  37. // Set the environment variables we need.
  38. self.ipaddress = process.env.OPENSHIFT_NODEJS_IP;
  39. self.port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
  40.  
  41. if (typeof self.ipaddress === "undefined") {
  42. // Log errors on OpenShift but continue w/ 127.0.0.1 - this
  43. // allows us to run/test the app locally.
  44. console.warn('No OPENSHIFT_NODEJS_IP var, using 127.0.0.1');
  45. self.ipaddress = "127.0.0.1";
  46. };
  47. };
  48.  
  49.  
  50. /**
  51. * Populate the cache.
  52. */
  53. self.populateCache = function() {
  54. if (typeof self.zcache === "undefined") {
  55. self.zcache = { 'index.html': '' };
  56. }
  57.  
  58. // Local cache for static content.
  59. self.zcache['index.html'] = fs.readFileSync('./index.html');
  60. };
  61.  
  62.  
  63. /**
  64. * Retrieve entry (content) from cache.
  65. * @param {string} key Key identifying content to retrieve from cache.
  66. */
  67. self.cache_get = function(key) { return self.zcache[key]; };
  68.  
  69.  
  70. /**
  71. * terminator === the termination handler
  72. * Terminate server on receipt of the specified signal.
  73. * @param {string} sig Signal to terminate on.
  74. */
  75. self.terminator = function(sig){
  76. if (typeof sig === "string") {
  77. console.log('%s: Received %s - terminating sample app ...',
  78. Date(Date.now()), sig);
  79. process.exit(1);
  80. }
  81. console.log('%s: Node server stopped.', Date(Date.now()) );
  82. };
  83.  
  84.  
  85. /**
  86. * Setup termination handlers (for exit and a list of signals).
  87. */
  88. self.setupTerminationHandlers = function(){
  89. // Process on exit and signals.
  90. process.on('exit', function() { self.terminator(); });
  91.  
  92. // Removed 'SIGPIPE' from the list - bugz 852598.
  93. ['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT',
  94. 'SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGTERM'
  95. ].forEach(function(element, index, array) {
  96. process.on(element, function() { self.terminator(element); });
  97. });
  98. };
  99.  
  100.  
  101. /* ================================================================ */
  102. /* App server functions (main app logic here). */
  103. /* ================================================================ */
  104.  
  105. /**
  106. * Create the routing table entries + handlers for the application.
  107. */
  108. self.createRoutes = function() {
  109. self.routes = { };
  110.  
  111. self.routes['/:ids'] = function(req, res) {
  112. T.get('statuses/show/:id', { id: req.param('ids') }, function(err, data, response) {
  113. console.log(data)
  114. })
  115. };
  116.  
  117. self.routes['/'] = function(req, res) {
  118. res.setHeader('Content-Type', 'text/html');
  119. res.send(self.cache_get('index.html') );
  120. };
  121. };
  122.  
  123.  
  124. /**
  125. * Initialize the server (express) and create the routes and register
  126. * the handlers.
  127. */
  128. self.initializeServer = function() {
  129. self.createRoutes();
  130. self.app = express.createServer();
  131.  
  132. // Add handlers for the app (from the routes).
  133. for (var r in self.routes) {
  134. self.app.get(r, self.routes[r]);
  135. }
  136. };
  137.  
  138.  
  139. /**
  140. * Initializes the sample application.
  141. */
  142. self.initialize = function() {
  143. self.setupVariables();
  144. self.populateCache();
  145. self.setupTerminationHandlers();
  146.  
  147. // Create the express server and routes.
  148. self.initializeServer();
  149. };
  150.  
  151.  
  152. /**
  153. * Start the server (starts up the sample application).
  154. */
  155. self.start = function() {
  156. // Start the app on the specific interface (and port).
  157. self.app.listen(self.port, self.ipaddress, function() {
  158. console.log('%s: Node server started on %s:%d ...',
  159. Date(Date.now() ), self.ipaddress, self.port);
  160. });
  161. };
  162.  
  163. }; /* Sample Application. */
  164.  
  165.  
  166.  
  167. /**
  168. * main(): Main code.
  169. */
  170. var zapp = new SampleApp();
  171. zapp.initialize();
  172. zapp.start();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement