setup.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. module.exports = function(request, response) {
  2. /**
  3. * Intégration des dépendences SQLITE3
  4. */
  5. const sqlite3 = require('sqlite3').verbose();
  6. const db = new sqlite3.Database('./db/data.db');
  7. /**
  8. * Dépendance permettant la modification du fichier config et la génération du mot de passe de l'api
  9. */
  10. const fs = require('fs');
  11. const generator = require('generate-password');
  12. /**
  13. * Création de la DB ainsi que les tables contenues dedans
  14. */
  15. db.serialize(function() {
  16. db.run('CREATE TABLE IF NOT EXISTS calls (itsfrom TEXT, itsto TEXT, digits TEXT, callSid TEXT, status TEXT, date TEXT, user TEXT, name TEXT, service TEXT)');
  17. db.run('CREATE TABLE IF NOT EXISTS sms (itsfrom TEXT, itsto TEXT, smssid TEXT, content TEXT, status TEXT, date TEXT, user TEXT, service TEXT)');
  18. });
  19. fs.readFile('config.js', 'utf-8', function(err, data) {
  20. if (err) throw err;
  21. var pass = generator.generate({
  22. length: 32,
  23. numbers: true
  24. });
  25. var newapipassword = data.replace(/passwordtochange/gim, pass);
  26. fs.writeFile('config.js', newapipassword, 'utf-8', function(err, data) {
  27. if (err) throw err;
  28. console.log('Setup the new api password : done.');
  29. fs.readFile('config.js', 'utf-8', function(err, data) {
  30. if (err) throw err;
  31. var setupdone = data.replace(/false/gim, 'true');
  32. fs.writeFile('config.js', setupdone, 'utf-8', function(err, data) {
  33. if (err) throw err;
  34. console.log('Automatic setup : done.');
  35. });
  36. });
  37. });
  38. });
  39. };