123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- var cloudflare = require('./cloudflare');
- var Cortex = require('cortexjs');
- var DomainCortex = new Cortex([]);
- function findDomain(name) {
- return DomainCortex.find(function(d) {
- return d.name.val() === name;
- });
- }
- function loadRecords(name) {
- var domain = findDomain(name);
- if(domain.records.count() > 0) {
- return;
- }
- cloudflare.records(domain.id.val()).then(function(data) {
- domain.records.set(data.result);
- });
- }
- function loadSettings(name) {
- var domain = findDomain(name);
- if(domain.settings.count() > 0) {
- return;
- }
- cloudflare.settings(domain.id.val()).then(function(data) {
- data.result.forEach(function(setting) {
- if(setting.id == 'development_mode') {
- setting.timestamp = Date.now() / 1000 + setting.time_remaining;
- }
- });
- domain.settings.set(data.result);
- });
- }
- function recordAdd(name, record) {
- var domain = findDomain(name);
- return cloudflare.recordAdd(domain.id.val(), record).then(function(data) {
- if(data.success) {
- domain.records.push(data.result);
- }
- });
- }
- function recordEdit(name, record) {
- var domain = findDomain(name);
- return cloudflare.recordEdit({zoneId: domain.id.val(), recId: record.id}, record).then(function(data) {
- if(data.success) {
- var oldRecord = domain.records.find(function(r) {
- return r.id.val() === record.id;
- });
- oldRecord.set(data.result);
- }
- });
- }
- function recordDelete(name, id) {
- var domain = findDomain(name);
- return cloudflare.recordDelete({zoneId: domain.id.val(), recId: id}).then(function(data) {
- if(data.success) {
- var oldRecord = domain.records.find(function(r) {
- return r.id.val() === id;
- });
- oldRecord.remove();
- }
- });
- }
- function settingChange(name, setting, value) {
- var domain = findDomain(name);
- var params = {zoneId: domain.id.val(), setting: setting};
- return cloudflare.settingChange(params, {value: value}).then(function(data) {
- if(data.success) {
- if(data.result.id == 'development_mode') {
- data.result.timestamp = Date.now() / 1000 + data.result.time_remaining;
- }
- domain.settings.find(function(s) { return s.id.val() == setting }).set(data.result);
- }
- });
- }
- function purgeCache(name) {
- return cloudflare.purgeCache(findDomain(name).id.val(), {purge_everything: true});
- }
- cloudflare.domains().then(function(data) {
- data.result.forEach(function(domain) {
- domain.records = [];
- domain.settings = [];
- });
- DomainCortex.set(data.result);
- });
- module.exports = {
- Domains: {
- find: findDomain,
- recordAdd: recordAdd,
- recordEdit: recordEdit,
- recordDelete: recordDelete,
- settingChange: settingChange,
- purgeCache: purgeCache,
- loadRecords: loadRecords,
- loadSettings: loadSettings,
- cortex: DomainCortex
- }
- };
- window.Cortex = Cortex;
|