Created
March 29, 2017 06:07
-
-
Save udnaan/1e0d0bf5831ef940e618e1965f6f64db to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const functions = require('firebase-functions'); | |
const admin = require('firebase-admin'); | |
const {credentials} = functions.config().auth; | |
credentials.private_key = credentials.private_key.replace(/\\n/g, '\n'); | |
const config = Object.assign({}, functions.config().firebase, {credentials}); | |
admin.initializeApp(config); | |
const gcs = require('@google-cloud/storage')({credentials}); | |
const dns = require('@google-cloud/dns')({credentials}); | |
const zoneName = 'applambda'; | |
const zone = dns.zone(zoneName); | |
exports.createDeleteDNSAndStorage = functions.database.ref('/apps/{uid}/{appid}/name') | |
.onWrite(event => { | |
// Only edit data when it is first created. | |
const {uid, appid} = event.params; | |
const name = event.data.val(); | |
const dbRef = admin.database().ref(`/apps/${uid}/${appid}`); | |
if (event.data.previous.exists()) { | |
console.log(`already exists ${uid}/${appid}`); | |
return; | |
} | |
// Exit when the data is deleted. | |
if (!event.data.exists()) { | |
console.log(`data is being deleted ${uid}/${appid}`); | |
return; | |
} | |
const url = `${name}.${zoneName}.com`; | |
console.log(`data: ${uid}/${appid}/${name}\nsetting up: ${url}`); | |
setupDNS({url, dbRef}); | |
setupStorage({url, dbRef}); | |
return; | |
}); | |
function setupDNS({url, dbRef}) { | |
// Create an NS record. | |
let cnameRecord = zone.record('cname', { | |
name: `${url}.`, | |
data: 'c.storage.googleapis.com.', | |
ttl: 3000 | |
}); | |
zone.addRecords(cnameRecord).then(function() { | |
console.log(`done setting up zonerecord for ${url}`); | |
dbRef.update({dns: url}).then(res => console.log(res)).catch(err => console.log(err)); | |
}).catch(function(err) { | |
console.error(`error setting up zonerecord for ${url}`); | |
console.error(err); | |
}); | |
} | |
function setupStorage({url, dbRef}) { | |
console.log(`setting up storage bucket for ${url}`); | |
gcs.createBucket(url, { | |
website: { | |
mainPageSuffix: `https://${url}`, | |
notFoundPage: `https://${url}/404.html` | |
} | |
}).then(function(res) { | |
let bucket = res[0]; | |
console.log(`created bucket ${url}, setting it as public`); | |
dbRef.update({storage: url}).then(function() { | |
console.log(`done setting up bucket for ${url}`); | |
}).catch(function(err) { | |
console.error(`db update for storage failed ${url}`); | |
console.error(err); | |
}); | |
bucket.makePublic().then(function() { | |
console.log(`bucket set as public for ${url}`); | |
}).catch(function(err) { | |
console.error(`setting public for storage failed ${url}`); | |
console.error(err); | |
}); | |
}).catch(function(err) { | |
console.error(`creating bucket failed ${url}`); | |
console.error(err); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment