Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 22nd, 2012  |  syntax: None  |  size: 3.63 KB  |  hits: 159  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Here's the code for level00.js, the main server file:
  2.  
  3. // Install dependencies with 'npm install'
  4. // Run as 'node level00.js'
  5.  
  6. var express = require('express'), // Web framework
  7.     mu = require('mu2'),          // Mustache.js templating
  8.     sqlite3 = require('sqlite3'); // SQLite (database) driver
  9.  
  10. // Look for templates in the current directory
  11. mu.root = __dirname;
  12.  
  13. // Set up the DB
  14. var db = new sqlite3.Database('level00.db');
  15. db.run(
  16.   'CREATE TABLE IF NOT EXISTS secrets (' +
  17.     'key varchar(255),' +
  18.     'secret varchar(255)' +
  19.   ')'
  20. );
  21.  
  22. // Create the server
  23. var app = express();
  24. app.use(express.bodyParser());
  25.  
  26. function renderPage(res, variables) {
  27.   var stream = mu.compileAndRender('level00.html', variables);
  28.   res.header('Content-Type', 'text/html');
  29.   stream.pipe(res);
  30. }
  31.  
  32. app.get('/*', function(req, res) {
  33.   var namespace = req.param('namespace');
  34.  
  35.   if (namespace) {
  36.     var query = 'SELECT * FROM secrets WHERE key LIKE ? || ".%"';
  37.     db.all(query, namespace, function(err, secrets) {
  38.              if (err) throw err;
  39.  
  40.              renderPage(res, {namespace: namespace, secrets: secrets});
  41.            });
  42.   } else {
  43.     renderPage(res, {});
  44.   }
  45. });
  46.  
  47. app.post('/*', function(req, res) {
  48.   var namespace = req.body['namespace'];
  49.   var secret_name = req.body['secret_name'];
  50.   var secret_value = req.body['secret_value'];
  51.  
  52.   var query = 'INSERT INTO secrets (key, secret) VALUES (? || "." || ?, ?)';
  53.   db.run(query, namespace, secret_name, secret_value, function(err) {
  54.      if (err) throw err;
  55.  
  56.            res.header('Content-Type', 'text/html');
  57.            res.redirect(req.path + '?namespace=' + namespace);
  58.          });
  59. });
  60.  
  61. if (process.argv.length > 2) {
  62.   var socket = process.argv[2];
  63.   console.log("Starting server on UNIX socket " + socket);
  64.   app.listen(socket);
  65. } else {
  66.   console.log("Starting server at http://localhost:3000/");
  67.   app.listen(3000);
  68. }
  69.  
  70.  
  71. And here's the code for level00.html, its mustache.js template:
  72.  
  73. <html>
  74.   <head>
  75.     <title>Secret Safe</title>
  76.   </head>
  77.   <body>
  78.     {{#namespace}}
  79.     <div style="border-width: 2px; border-style: outset; padding: 5px">
  80.       Showing secrets for <strong>{{namespace}}</strong>:
  81.       <table>
  82.         <thead>
  83.           <tr>
  84.             <th>Key</th>
  85.             <th>Value</th>
  86.           </tr>
  87.         </thead>
  88.         <tbody>
  89.           {{#secrets}}
  90.           <tr>
  91.             <td>{{ key }}</td>
  92.             <td>{{ secret }}</td>
  93.           </tr>
  94.           {{/secrets}}
  95.           {{^secrets}}
  96.           <tr>
  97.             <td span="2">
  98.               You have no secrets stored with us. Try using the form below.
  99.             </td>
  100.           </tr>
  101.           {{/secrets}}
  102.         </tbody>
  103.       </table>
  104.  
  105.       <hr />
  106.     </div>
  107.     {{/namespace}}
  108.  
  109.     <form action="" method="POST">
  110.       <p>
  111.         <label for="namespace">Namespace:</label>
  112.         <input type="text" name="namespace" id="namespace"
  113.             value="{{ namespace }}" />
  114.       </p>
  115.       <p>
  116.         <label for="secret_name">Name of your secret:</label>
  117.         <input type="text" name="secret_name" id="secret_name">
  118.       </p>
  119.       <p>
  120.         <label for="secret_value">Your secret:</label>
  121.         <input type="password" name="secret_value" id="secret_value">
  122.       </p>
  123.       <p>
  124.         <input type="submit" value="Store my secret!" />
  125.       </p>
  126.     </form>
  127.     <form action="" method="GET">
  128.       <label for="change_namespace">
  129.         Want to retrieve your secrets? View secrets for:
  130.       </label>
  131.       <input name="namespace" id="change_namespace" />
  132.       <input type="submit" value="View" />
  133.     </form>
  134.   </body>
  135. </html>