`

Sample Project for Firebase Functions

 
阅读更多
预计SCR文档结构如下:

/SRC
-*ModuleA
   --index.ts
-*ModuleB
   --index.ts
-*Usgers
   --index.ts
   --users.ts
--api_v1.ts
--index.ts


SRC/ModuleA/index.ts
import { Router } from "express";

const router = Router();

export default router;



SRC/ModuleB/index.ts
import { Router } from "express";

const router = Router();

export default router;




SRC/Usgers/index.ts
import { Router } from "express";

var functions = require("firebase-functions");
const router = Router();

//Added by Jacky
const strTestData = "{name:test}";

//Init the session to the target firbease project
var admin = require("firebase-admin");
var serviceAccount = require("../../Res/multimanagementsystem.json");
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: `https://${serviceAccount.project_id}-default-rtdb.firebaseio.com`
});
const db = admin.firestore();

//
router.get('/', async(request, response) => {
    response.json("User's root url");
});

router.get('/getUsers', async(request, response) => {
    //Sample URL:/managmentapi/v1/users/getUsers?id=111&name=2222
    if (request.query != null)
       response.send(request.query);
    else
       response.json( {strTestData} );
});
  
async function writeDBFunction() {
    const docRef = db.collection('users').doc('alovelace');
    await docRef.set({
        first: 'Ada',
        last: 'Lovelace',
        born: 1815
        });
}

router.get('/writeDB', async(request, response) => {
    writeDBFunction();
    response.send("Succeed to write DB!");
  });


async function readDBFunction() {
    const snapshot = await db.collection('users').get();
    functions.logger.info(snapshot, {structuredData: true});
}

router.get('/readDB', async(request, response) => {
    readDBFunction();
    response.send("Succeed to read DB!");
  });

router.get('/getUser/:id/:name', async(request, response) => {
    //Sample URL:/managmentapi/v1/users/getUser/id111/name2222
    functions.logger.info("Param[id]=" + request.params.id + " Param[name]=" + request.params.name);

    const user = db.collection('users').doc(request.params.id);
    const doc = user.get();
    if (!doc.exists) {
        console.log('No such document!');
    } else {
        console.log('Document data:', doc.data());
    }

    response.json(request.params.id + request.params.name);
});

export default router;





SRC/Usgers/users.ts
/*
//TODO the class according to related database shcema
export class Users {
    params: any;
}

export const users = new Users;
*/



SRC/api_v1.ts
import { Router } from "express";
import router_moduleA from './ModuleA';
import router_moduleB from './ModuleB';
import router_users from './users';


const router = Router();

router.use('/ModuleA', router_moduleA);
router.use('/ModuleB', router_moduleB);
router.use('/users', router_users);

export default router;




SRC/index.ts
var functions = require("firebase-functions");
const express = require('express');
const cors = require('cors');
const app = express();

//root dir of current apis
import api_v1 from './api_v1';

// Automatically allow cross-origin requests
app.use(cors({ origin: true })); 

// Add middleware to authenticate requests
//Main router of current backend system
app.use('/v1', api_v1);

//Added by Jacky
app.get('/getCurVersion', async(req, res) => {
  res.json("01.0001.20210315" );
})

// build multiple CRUD interfaces:
app.get('/', async(req, res) => {
  res.send(""); 
});

app.get('/*', async(req, res) => {
  res.send(""); 
});

/*
app.post('/', async(req, res) => {
  res.send('create()');
});

app.put('/:id', async(req, res) => {
  res.send(req.params.id + req.body);
});

app.delete('/:id', async(req, res) => {
  res.send(req.params.id);
});
*/

// Expose Express API as a single Cloud Function:
exports.managmentapi = functions.https.onRequest(app);
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics