serveur de hook HTTP
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

backup 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/env node
  2. var fs = require('fs');
  3. var path = require('path');
  4. var http = require('http');
  5. var config = require('../lib/config');
  6. fetchAPI('/api/slots', (err, slots) => {
  7. if (err) {
  8. console.error(err.message);
  9. process.exit(1);
  10. }
  11. var backup = {
  12. meta: "Marang Backup",
  13. format: 0,
  14. date: new Date(),
  15. slots: slots,
  16. };
  17. var exists = fs.existsSync(config.backupPath);
  18. if (!exists) {
  19. console.error("Destination directory '%s' does not exist.", config.backupPath);
  20. process.exit(1);
  21. }
  22. var now = new Date();
  23. var backupName = now.toJSON() + '.json';
  24. var destFile = path.join(config.backupPath, backupName);
  25. fs.writeFile(destFile, JSON.stringify(backup), (err) => {
  26. if (err) {
  27. console.error(err.message);
  28. process.exit(1);
  29. }
  30. console.log('data saved in ' + destFile);
  31. });
  32. });
  33. function fetchAPI(path, cb) {
  34. var credentials = config.webApp.credentials.username +
  35. ':' +
  36. process.env.MARANG_PASSWORD
  37. ;
  38. var opts = {
  39. host: config.webApp.host,
  40. port: config.webApp.port,
  41. path: path,
  42. auth: credentials
  43. }
  44. http.get(opts, res => {
  45. if(res.statusCode < 200 || res.statusCode >= 400) {
  46. return cb(new Error('Unexpected HTTP response: ' + res.statusCode + ' ' + res.statusMessage));
  47. }
  48. res.setEncoding('utf8');
  49. var data = '';
  50. res.on('data', chunk => {
  51. data += chunk;
  52. });
  53. res.once('end', () => {
  54. var json;
  55. try {
  56. json = JSON.parse(data);
  57. } catch(err) {
  58. return cb(err);
  59. }
  60. cb(null, json);
  61. });
  62. res.once('error', (err) => {
  63. res.removeAllListeners();
  64. cb(err);
  65. });
  66. });
  67. }