Giter Club home page Giter Club logo

its_urgent_backend's Introduction

Its Urgent Backend

This is backend for It's Urgent Flutter Project. It uses Typescript on Firebase Cloud Functions.

Project Structure

Main backend code can be found in functions/src directory

To avoid writing all functions in one single file index.ts, I am using a feature first project structure which is as follows (NOTE: This is not necessary):

/src
├───core/
├───feature1/
│   ├───reactive/
│   └───restful/
├───feature2/
│   ├───reactive/
│   └───restful/
└───feature3/...
  • src/index.ts only contains all the imports from subdirectories like:

    import {helloWorld} from "./hello-world/restful/helloWorld";
    import {createUserRef} from "./auth/reactive/createUserRef";
    import {deleteUserRef} from "./auth/reactive/deleteUserRef";
    
    // this code is required to deploy the imported cloud functions
    export { helloWorld, createUserRef, deleteUserRef};
  • src/core/ - Contains all the core functionality which can be common like:

    // filepath: src/core/admin.ts
    
    import * as admin from "firebase-admin";
    
    if (!admin.apps.length) {
    admin.initializeApp();
    }
    
    export default admin;
  • Each src/feature/ subfolder contains two subfolders -> reactive/ & restful/.

    • reactive/ - Contains all the cloud functions which are triggered (reactive) in response to firebase products events.
    • Here are a few examples of things that you can do with Cloud Functions triggers:

      • Authentication trigger to send a welcome email when a new user signs in.
      • Receive an email or a Slack notification when a new issue is discovered in Crashlytics.
      • Generate a thumbnail when a user uploads an image to Firebase Storage.
      • Moderate or remove any offensive words that are entered by a user inside your app.
      • Send a FCM notification to users in your app (useful for chat applications).
      • Index your Firestore DB and implement full-text search by tapping into external services like Algolia.
      // filepath: src/auth/reactive/createUserRef.ts
      
      import * as logger from "firebase-functions/logger";
      import * as functionsV1 from "firebase-functions/v1";
      import admin from "../../core/admin";
      
      
      // Create a new userRef in Firestore when a new user is created in Firebase Auth
      export const createUserRef = functionsV1.auth.user().onCreate((user) => {
          const phoneNumber = user.phoneNumber?.replace(/\s+/g, ""); //remove spaces from phone number
          const userRef = admin.firestore().doc(`usersRef/${phoneNumber}`);
      
          logger.info(`New userRef created: ${phoneNumber}`);
          return userRef.set({
              uid: user.uid,
          });
      });
    • restful/ - Contains all the cloud functions which are callable from client like http resful apis.

      // filepath: 'src/hello-world/restful/helloWorld.ts'
      
      import {onRequest} from "firebase-functions/v2/https";
      import * as logger from "firebase-functions/logger";
      
      
      export const helloWorld = onRequest((request, response) => {
          response.send("Hello World!");
      });
      • When someone calls this api endpoint like http://<localhost_or_hosted_url>/<your_firebase project_id>/us-central1/helloWorld

        output response:

        Hello World!
        

Useful Links & Resources (Ctrl/Cmd + click to open in new tab)

its_urgent_backend's People

Contributors

0xharkirat avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.