google cloud api | authorize google service | | Search

The code imports various functions and modules, including getSheet for retrieving data from a Google Sheets API and addIP for performing a DNS lookup, and defines a setupBackend function that uses these functions to set up a Google Cloud Backend. The setupBackend function takes link and domain as arguments, performs a series of operations to set up the backend, and returns the domain value.

Run example

npm run import -- "setup sheet backend"

setup sheet backend

var importer = require('../Core');
var getSheet = importer.import("get sheet purchases");
//var purchaseId = '1kWjkjLGxQyzFUzRLBk3LpcjPW3UjcaF-PBMDX_3hZfM';
var addIP = importer.import("check dns");
var {
    insertBackendBucket,
    insertGlobalForward,
    updateUrlMap
} = importer.import("add google bucket web map");

var urlMap = 'web-map';

function setupBackend(link, domain) {
    var project;

    return getSheet(link, domain)
        .then(match => {
            project = match.project;
            domain = domain || match.bucket;
            return addIP(project, domain);
        })
        .then(ip => insertGlobalForward(project, ip, urlMap, domain))
        .then(() => insertBackendBucket(project, domain))
        .then(() => updateUrlMap(project, urlMap, domain))
        .then(() => domain)
}

module.exports = setupBackend;

What the code could have been:

const { getSheetPurchases } = require('../Core');
const { checkDns } = require('../Core');
const { addGoogleBucketWebMap } = require('../Core');

const {
  insertBackendBucket,
  insertGlobalForward,
  updateUrlMap,
} = addGoogleBucketWebMap;

/**
 * Set up the backend by retrieving sheet data, adding a DNS, inserting a global forward, 
 * and updating the URL map.
 * @param {string} link - The link to the Google Sheet.
 * @param {string} [domain] - The domain to use. Defaults to the bucket from the sheet.
 * @returns {Promise} A promise resolving to the domain.
 */
function setupBackend(link, domain) {
  const urlMap = 'web-map';

  return getSheetPurchases(link)
   .then(({ project, bucket }) => {
      const projectDomain = domain || bucket;
      return checkDns(project, projectDomain);
    })
   .then(ip => insertGlobalForward(project, ip, urlMap, domain))
   .then(() => insertBackendBucket(project, domain))
   .then(() => updateUrlMap(project, urlMap, domain))
   .then(() => domain);
}

module.exports = setupBackend;

Code Breakdown

Importing Modules and Functions

var importer = require('../Core');
var getSheet = importer.import('get sheet purchases');
var addIP = importer.import('check dns');
var {
    insertBackendBucket,
    insertGlobalForward,
    updateUrlMap
} = importer.import('add google bucket web map');

This section imports functions and modules from another file (../Core):

Defining the setupBackend Function

function setupBackend(link, domain) {
    var project;

    return getSheet(link, domain)
       .then(match => {
            project = match.project;
            domain = domain || match.bucket;
            return addIP(project, domain);
        })
       .then(ip => insertGlobalForward(project, ip, urlMap, domain))
       .then(() => insertBackendBucket(project, domain))
       .then(() => updateUrlMap(project, urlMap, domain))
       .then(() => domain)
}

This function, setupBackend, takes two arguments: link and domain. It:

  1. Calls getSheet to retrieve data and sets project and domain accordingly.
  2. Calls addIP to perform a DNS lookup.
  3. Calls insertGlobalForward to set up a global forward.
  4. Calls insertBackendBucket to set up a backend bucket.
  5. Calls updateUrlMap to update a URL map.
  6. Returns the domain value.

Exporting the setupBackend Function

module.exports = setupBackend;

This line exports the setupBackend function as a module, making it available for use in other files.