Welcome to the backend documentation for our open-source project. This document provides an overview of the backend structure, API routes, and how to contribute to the project.
The backend folder is organized into several directories, each serving a specific purpose:
.
└── /backend/
├── /community/
│ ├── /controllers # Logic for handling API requests related to Communities and it's members
│ ├── /middlewares # Middleware functions for verifying admin and member status
│ ├── /models # Database models for community and community members
│ ├── /routes # API route definitions for community and community members
│ ├── /services # Reusable services specific to community members
│ └── /utils # Utility file defining constants for user roles
├── /contest/
│ ├── /controllers # Logic for handling API requests related to contests/
│ │ └── /platforms # APIs for fetching the upcoming contests on each of the coding platforms
│ ├── /models # Database models for contests
│ └── /routes # API route definitions for contests
├── /core/
│ └── /api # File defining custom error and success handler objects for responses
├── /DSA_sheets/
│ ├── /controllers # Logic for handling API requests realted to DSA sheets and DSA questions
│ ├── /models # Database models for DSA sheets and questions
│ └── /routes # API route definitions for DSA sheets and questions
├── /services/
│ ├── /discord-webhook
│ └── /email
├── /users/
│ ├── /controllers # Logic for handling API requests related to users/
│ │ └── /platforms # Logic for fetching user's info on different coding platforms
│ ├── /middlewares # Custom middleware functions specific to users
│ ├── /models # Database models for users
│ ├── /routes # API route definitions for users
│ └── /services # Reusable services and utilities specific to users
└── index.js # Main server file
backend
: Main folder for the backend.community
: Controllers, middlewares, models, routes, services, and utils for community members.controllers
: Logic for handling API requests related to Communities and it's membersmiddlewares
: Middleware functions for verifying admin and member statusmodels
: Database models for community and community membersroutes
: API route definitions for community and community membersservices
: Reusable services specific to community membersutils
: Utility file defining constants for user roles
contest
: Controllers, models, and routes for contests.controllers
: Logic for handling API requests related to contestsplatforms
: APIs for fetching the upcoming contests on each of the coding platforms
models
: Database models for contestsroutes
: API route definitions for contests
core
: File defining custom error and success handler objects for responsesapi
: File defining custom error and success handler objects for responses
DSA_sheets
: Controllers, models, and routes for DSA sheets and questions.controllers
: Logic for handling API requests realted to DSA sheets and DSA questionsmodels
: Database models for DSA sheets and questionsroutes
: API route definitions for DSA sheets and questions
services
: Reusable services and utilities specific to usersusers
: Controllers, middlewares, models, routes, services, and utils for users.controllers
: Logic for handling API requests related to usersplatforms
: Logic for fetching user's info on different coding platforms
middlewares
: Custom middleware functions specific to usersmodels
: Database models for usersroutes
: API route definitions for usersservices
: Reusable services and utilities specific to users
index.js
: Main server file
- Clone the repository:
git clone https://github.com/pranshugupta54/digitomize.git
- Navigate to the backend directory:
cd digitomize/backend
- Install dependencies:
npm install
Configure your environment variables in the .env
file. This file should contain sensitive information and configurations that your application needs, such as database connection strings, API keys, and more.
TEST=Env file connected.
MONGODB_URL=mongodb://127.0.0.1:27017/digitomize
PORT=4001
BACKEND_URL=http://localhost:4001
CONTESTS=true
USERS=true
NODE_ENV=development
# Firebase Configuration
`FIREBASE_CREDENTIALS=` # you need to add JSON for this
This .env
file must be populated with the following environment variables for digitomize to work:
Variable | Explanation |
---|---|
TEST | Marker indicating that the environment file is successfully connected. |
MONGODB_URL | URL for connecting Digitomize to the MongoDB database. |
PORT | Port on which the application will run. |
BACKEND_URL | URL of the backend server. |
CONTESTS | Controls whether the application should fetch contest data. |
USERS | Controls whether the application should fetch user data. |
NODE_ENV | Specifies the environment in which the application is running. |
FIREBASE_CREDENTIALS | Variable intended for storing Firebase credential in JSON format. |
- Create a new
.env
file in the backend directory - Copy the contents of the
.example.env
in the the backend directory and paste them into your created.env
file - Fill in the
FIREBASE_CREDENTIALS=
variable in JSON with the JSON credentials generated from your created firebase project (see below for instructions on how to get these credentials)
To connect Digitomize to the database side, two options are available: MongoDB atlas or hosting a local instance. Here is a setup guide on both options.
-
Sign Up/Login to MongoDB Atlas:
- Go to the MongoDB Atlas website and sign up for an account or log in if you already have one.
-
Create a New Cluster:
- Once logged in, click on the "Build a Cluster" button or navigate to the "Clusters" tab and click on "Build a New Cluster".
- Choose the provider and region for your cluster. For example, you can select a cloud provider (AWS, Azure, or Google Cloud) and a region closest to your location.
- Select the desired cluster tier (e.g., M0 Sandbox is the free tier).
- Click "Create Cluster" to provision your new cluster.
-
Configure Cluster Settings:
- MongoDB Atlas will guide you through the process of setting up your cluster. You can keep the default settings or customize them based on your requirements.
- Choose the cluster name, project name, and other settings as needed.
-
Whitelist Your IP Address:
- In the MongoDB Atlas dashboard, navigate to the "Network Access" tab.
- Click on the "Add IP Address" button and add your current IP address to the whitelist. This allows your application to connect to the cluster.
-
Get Connection String:
- Once your cluster is created, click on the "Connect" button.
- Choose "Connect Your Application" and select your driver and version.
- Copy the connection string provided.
-
Create a
.env
File:-
Paste the following content into your
.env
file:MONGODB_URL=<connection-string>
-
Replace
<connection-string>
with the MongoDB Atlas connection string you copied earlier.
-
-
Save the
.env
File:- Save the changes to the
.env
file.
- Save the changes to the
To use MongoDB locally, you need to first download MongoDB locally and then update the .env
file.
-
Download MongoDB:
- Visit the official MongoDB download page and select the appropriate version for your operating system.
- Follow the installation instructions provided for your operating system.
-
Install MongoDB:
- For Windows:
- Run the downloaded installer (.msi file) and follow the setup wizard.
- Choose the Complete installation type and proceed with the installation.
- For macOS:
- Open the downloaded
.dmg
file and drag the MongoDB application to the Applications folder.
- Open the downloaded
- For Linux:
- Follow the installation instructions specific to your Linux distribution provided on the MongoDB download page.
- For Windows:
-
Start MongoDB Server:
- Open a command-line interface (CLI) and navigate to the MongoDB bin directory.
- Start the MongoDB server by running the
mongod
command:mongod
- MongoDB should now be running locally on the default port (27017) unless configured otherwise.
-
Verify MongoDB Installation:
- Open a new CLI window and navigate to the MongoDB bin directory.
- Run the
mongo
command to open the MongoDB shell. - If MongoDB is running properly, you should see a prompt indicating that you've connected to the MongoDB server.
MONGODB_URL=mongodb://127.0.0.1:27017/digitomize
To get credentials you should first have a firebase project. If you don't know how to make one, please check Frontend Documentation.
-
Select your project.
-
Go to "Project Settings" by clicking on the gear icon next to "Project Overview" in the top-left corner.
-
Navigate to the "Service accounts" tab.
-
If you haven't created a service account yet, click on "Generate new private key."
-
This will generate a new service account key and automatically download it as a JSON file (
serviceAccountKey.json
). it will look something like this{ "type": "service_account", "project_id": "your-project-id", "private_key_id": "your-private-key-id", "private_key": "-----BEGIN PRIVATE KEY-----\nYourPrivateKey\n-----END PRIVATE KEY-----\n", "client_email": "your-service-account-email@your-project-id.iam.gserviceaccount.com", "client_id": "your-client-id", "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://oauth2.googleapis.com/token", "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/your-service-account-email%40your-project-id.iam.gserviceaccount.com" }
-
copy this entire thing and set it as the value of FIREBASE_CREDENTIALS in your .env file. (Make sure the entire json is placed on a single line)
Start the server: npm start
- Run
npm run lint
to check for errors using the linter. - Run
npm run lint-fix
to have the linter automatically fix errors where possible.