index_old.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // built into node.
  2. var http = require('http'),
  3. fs = require('fs'),
  4. argv = require('optimist').argv,
  5. https = require('https'),
  6. Gun = require('gun'),
  7. Sea = require('gun/sea')
  8. // require('gun/lib/shim')
  9. // require('gun/lib/path')
  10. // require('gun/lib/bye')
  11. require('gun/nts')
  12. // Create a new server instance.
  13. //var server = http.createServer();
  14. //var location = {host:"localhost"};
  15. //var gun = Gun( { file: 'read.json', peers: ['http://' + location.host + ':8080/gun'] });
  16. //gun.get( 'players' ).path('me4').put('from server',function(v){ console.log("put new value " + v ); } );
  17. //gun.get('players').get('me4').bye().put('offline');
  18. //gun.on('bye', function(peer){ console.log('disconnected from', peer) })
  19. var ssl = ( argv.s || argv.ssl );
  20. var pass = ( ( argv.w) ? ( argv.w) : undefined );
  21. var sslOptions = {
  22. key: ( ( argv.k || argv.key ) ? fs.readFileSync( argv.k || argv.key ) : undefined ),
  23. cert: ( ( argv.c || argv.cert ) ? fs.readFileSync( argv.c || argv.cert ) : undefined ),
  24. ca: ( ( argv.t || argv.ca ) ? fs.readFileSync( argv.t || argv.ca ) : undefined ),
  25. passphrase: JSON.stringify(pass)
  26. };
  27. //create the server
  28. var port = ( ( argv.p || argv.port ) ? ( argv.p || argv.port ) : 8080 );
  29. var srv = ssl ? https.createServer( sslOptions, OnRequest): http.createServer(OnRequest);
  30. //console.log( 'Serving on port ' + port );
  31. // Our GUN setup from the last example.
  32. var gun = Gun({
  33. //file: 'data.json',
  34. //file: false,
  35. web: srv
  36. });
  37. function serve(request, response){
  38. response.writeHead( 200, {
  39. "Content-Type": "application/json"
  40. } );
  41. var jsonobject = {
  42. "db": "v0.0.2"
  43. //"instances": inst
  44. }
  45. response.write( JSON.stringify( jsonobject ), "utf8" );
  46. response.end();
  47. //console.log("Serve here")
  48. }
  49. function OnRequest(request, response) {
  50. try {
  51. serve(request, response);
  52. // vwf.Serve( request, response );
  53. } catch (e) {
  54. response.writeHead(500, {
  55. "Content-Type": "text/plain"
  56. });
  57. response.write(e.toString(), "utf8");
  58. response.end();
  59. }
  60. } // close onRequest
  61. // Start the server on port 8080.
  62. srv.listen(port, function () {
  63. console.log('Server listening on http://localhost:'+ port+ '/gun')
  64. })