Last active
March 6, 2019 17:41
-
-
Save markhilton/2e898ae36891d365462471cbde852d69 to your computer and use it in GitHub Desktop.
Firebase Google Cloud Functions REST router
This file contains 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
require('dotenv').config({ path: __dirname + '/.env' }) | |
const fs = require('fs') | |
const api = require('./middleware/router') | |
const functions = require('firebase-functions') | |
// runtime options | |
const runtimeOpts = { | |
memory: '256MB', | |
timeoutSeconds: 500 | |
} | |
// exports | |
fs.readdirSync(__dirname + '/api/').forEach(functionName => { | |
exports[functionName.slice(0, -3)] = functions.runWith(runtimeOpts).https.onRequest(api) | |
}) |
This file contains 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 get = (req, res) => { | |
res.status(200).send('fetch site: ' + req.body.id) | |
} | |
const all = (req, res) => { | |
res.status(200).send('fetch sites: ' + req.body.id) | |
} | |
const post = (req, res) => { | |
res.status(200).send('create new site: ' + req.body.id) | |
} | |
const put = (req, res) => { | |
res.status(200).send('update site: ' + req.body.id) | |
} | |
// exports | |
module.exports = { | |
all, | |
get, | |
put, | |
post | |
} |
This file contains 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
require('colors') | |
const path = require('path') | |
const express = require('express') | |
const cookies = require('cookie-parser')() | |
const corsHdr = require('cors')({ origin: true }) | |
// const appAuth = require('./authGuard') | |
const api = express() | |
api.use(corsHdr) | |
api.use(cookies) | |
// api.use(appAuth) | |
api.all('*', (req, res, next) => { | |
const functionType = req.method.toLocaleLowerCase() | |
const functionFile = '../api/' + process.env.FUNCTION_NAME | |
try { | |
if (functionType === 'get' && path.basename(req.path).slice(-1) === 's') { | |
console.log( | |
functionType.toUpperCase().red, | |
'request:'.yellow, | |
('/' + path.basename(functionFile) + 's').red, | |
'body:'.yellow, | |
req.body | |
) | |
return require(functionFile).all(req, res, next) | |
} else if (functionType === 'get') req.body.id = path.basename(req.path) | |
console.log( | |
functionType.toUpperCase().red, | |
'request:'.yellow, | |
('/' + path.basename(functionFile)).red, | |
'body:'.yellow, | |
req.body | |
) | |
return require(functionFile)[functionType](req, res, next) | |
} catch (error) { | |
console.error(error) | |
res.status(400).send('Bad req') | |
} | |
}) | |
// exports | |
module.exports = api |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What: when creating firebase backend functions with express, the typical use case is to route all API requests to a single cloud function performing routing.
Problem: the issue with the solution above is that Firebase Functions log reports the same function name for all cloud function requests.
Solution: dynamically rendered Google Cloud Functions by scanning javascript files located in
/functions/api
folder. Each javascript will create a unique HTTP cloud function export route and execute function name based on the request method: GET, POST, PUT etc.To Do dynamically render firebase.json hosting rewrite rules based on available functions.