This document provides instructions on how to set up and start a running instance of talawa-api on your local system. The instructions are written to be followed in sequence so make sure to go through each of them step by step without skipping any sections.
- Talawa-api installation
- Table of contents
Best way to install and manage node.js
is making use of node version managers. Two most popular node version managers right now are fnm and nvm. We'd recommend fnm
because it's written in rust
and is much faster than nvm
. Install whichever one you want and follow their guide to set up node.js
on your system.
Follow the setup guide for git
on official git docs. Basic git
knowledge is required for open source contribution so make sure you're comfortable with it. Here's a good tutorial to get started with git
and github
.
First you need a local copy of talawa-api. Run the following command in the directory of choice on your local system.
-
Navigate to the folder where you want to setup the repository. Here, I will set it up in a folder called
talawa
. -
Navigate to the folder and open a terminal in this folder (you can right-click and choose appropiate option based onn your OS). Next, we'll fork and clone the
talawa-api
repository. -
Navigate to https://github.com/PalisadoesFoundation/talawa-api/ and click on the fork button. It is placed on the right corner opposite the repository name
PalisadoesFoundation/talawa-api
.
- You should now see
talawa-api
under your repositories. It will be marked as forked fromPalisadoesFoundation/talawa-api
- Clone the repository to your local computer (replacing the values in
{{}}
):
$ git clone https://github.com/{{GITHUB USERNAME}}/talawa-api.git
This will setup the repository and the code files locally for you. For more detailed instructios on contributing code, and managing the versions of this repository with Git, checkout CONTRIBUTING.md here
NOTE:- All the commands we're going to execute in the following instructions will assume you are in the root directory of the project. If you fail to do so, the commands will not work.
A file named .env
is required in the root directory of talawa-api for storing environment variables used at runtime. It is not a part of the repo and you will have to create it. For a sample of .env
file there is a file named .env.sample
in the root directory. Create a new .env
file by copying the contents of the .env.sample
into .env
file.
cp .env.sample .env
This .env
file must be populated with the following environment variables for talawa-api to work:-
Variable | Description |
---|---|
ACCESS_TOKEN_SECRET | Used for signing/verifying JWT tokens |
REFRESH_TOKEN_SECRET | Used for signing/verifying JWT tokens |
MONGO_DB_URL | Used for connecting talawa-api to the mongoDB database |
RECAPTCHA_SECRET_KEY | Used for authentication using reCAPTCHA |
MAIL_USERNAME | Used for mailing service |
MAIL_PASSWORD | Used for mailing service |
Follow the instructions from Access/refresh token secrets section up to and including Google/firebase section to learn more about these environment variables and how to set them up.
Access and refresh token secrets are used for authentication purposes.
Run the following command and copy/paste the result to the variable named ACCESS_TOKEN_SECRET
in .env
file.
openssl rand -hex 32
Run the following command and copy/paste the result to the variable named REFRESH_TOKEN_SECRET
in .env
file.
openssl rand -hex 32
Talawa-api makes use of MongoDB
for its database needs. We make use of mongoose ODM
to interact with the MongoDB database from within the code.
We're listing some common approaches to set up a running instance of MongoDB database:-
-
Hosted database approach:-
MongoDB Atlas is the easiest way to get a running instance of mongodb database. It is a hosted(remote) mongodb database provided by mongodb itself. If you're a beginner and don't want too much of a hassle setting up the database you should use this approach but you should eventually switch to local instance. Follow the setup guide on official MongoDB Atlas Docs. Mongodb Atlas is just one of the many hosted database solutions. Some issues that you might face while using this are slower tests, slower API requests, dependence on Internet connection etc. -
System native database approach:-
You can install mongodb natively on your system and create/connect to the database. Follow the setup guide on official MongoDB Docs for your respective operating system. -
Docker container approach:-
If you are fluent in working with docker you should use this approach. Docker is a great way to manage and run applications without natively installing anything on your system. With this you can set up the mongodb database inside a docker container and manage it as per your will. Follow this video tutorial to set up a mongodb docker container.
Which approach you choose to set up your mongodb database does not matter. What matters is the connection string
to that database using which talawa-api can connect to it. Connection string
can differ depending on the approach you used to set up your database instance. Please read the official mongodb docs on connection string
. Copy/paste this connection string
to the variable named MONGO_DB_URL
in .env
file.
Your MongoDB installation may include either the mongo
or mongosh
command line utility. An easy way of determining the connection string
is to:
- Run the command line utility
- Note the
connection string
in the first lines of the output. - Add the first section of the
connection string
to theMONGO_DB_URL
section of the.env
file. In this case it ismongodb://127.0.0.1:27017/
$ mongosh
Current Mongosh Log ID: e6ab4232a963d456920b3736
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.2
Using MongoDB: 6.0.4
Using Mongosh: 1.6.2
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
...
...
...
...
Note: Windows user may proceed to next section of this documentation. In order to complete step 7 of process, please follow instructions outlined in this section which is universal for all operating systems.
It is recommended to have a local instance of MongoDB database instead of a cloud-based one, as it enhances the development experience and provides a more streamlined experience.
-
Download the latest version of MongoDB Community Server, which includes MongoDB Compass, from the following link:MongoDB Community Server
-
Separately download the MongoDB Shell from the tools section at the following link:Mongo Shell
-
Extract the downloaded shell folder, locate the "mongosh" application, and paste it to the following location:
Program Files
->MongoDB
->bin
. [Note: You will find the mongosh application inside thebin
folder] -
Add the path of the location where you pasted the "mongosh" application to your system's environment variables.
-
Create a folder named "data" in the C drive and within it create a new folder named "db".
-
Open a terminal and run the "mongosh" command in the terminal you will get the connection string.
-
In the
.env
file of talawa-api, add the connection string to the MONGO_DB_URL section.
$ mongosh
Current Mongosh Log ID: e6ab4232a963d456920b3736
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.2
Using MongoDB: 6.0.4
Using Mongosh: 1.6.2
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
...
...
...
...
For eg. here-mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.2
is your connection string.
-
In a separate terminal, run the "mongod" command to start the local instance of the database.
-
Open MongoDB Compass and click on "Connect." You will now be able to access the graphical user interface of the local database.
NOTE: You can do the same in macOS and linux with minor tweaks. This has been provided to give a brief overview for beginners to setup their own local instance.
This guide is for VSCode
users to easily manage their MongoDB
databases:-
-
Connect your
MongoDB
database to the extension.
-
Now you can manage the database you are using for
talawa-api
through this extension withinVSCode
.
Note: You can skip these instructions for now if you don't have running instance of Talawa-Admin.
-
This step is for mandatory Linux specific users others can skip to next step:
- You need to start
mongod
[Mongo daemon process] formongosh
to work use the following command for the same:
sudo service mongod start
[System V init(service)] orsudo systemctl start mongod
[systemd(systemctl)]
- To verify whether
mongod
[Mongo daemon process] is running you can use either:
sudo systemctl status mongod
[systemd(systemctl)] orsudo service mongod status
[System V init(service)]
- You need to start
-
Open MongoDB Compass and click on
Connect
. -
Select
user
collections and edit the data. Change:userType
from ADMIN to SUPERADMINadminApproved
from false to true
-
Open a terminal and run
mongosh
command to open interactive command line interface to work with MongoDB database. -
In the
mongosh
terminal use the following command to edit theusers
collections data:- Find all users of the type
ADMIN
.
db.users.find({userType: 'ADMIN'})
- Identify the
user
whose data you want to edit note its email address. Elevate permission of the user fromADMIN
toSUPERADMIN
and setadminApproved
totrue
db.users.updateOne({ email: '<user's email address>' },{ $set: { userType: 'SUPERADMIN', adminApproved: true }})
- To verify the details were updated correctly use:
db.users.find({email:'<user's email address>' })
- Find all users of the type
Note: You can do the edits via any of the two methods.
You need to have a google
account to follow the following steps.
We use reCAPTCHA
for authentication. Follow these steps:-
-
Visit this url.
-
Fill in the input blocks as shown in the screenshot:-
-
Click on
Submit
button. -
Copy the generated
Secret Key
to variable namedRECAPTCHA_SECRET_KEY
in.env
file. -
Save the generated
Site key
as it will be used intalawa-admin
.
NOTE:- Your google account needs to have two factor authentication set up for the following steps to work.
-
Go to your google account page.
-
Select
Security
. -
Under
Signing in to Google
section selectApp Passwords
. -
Click on
Select app
section and chooseOther(Custom name)
, entertalawa
as the custom name and pressGenerate
button. -
Copy the 16 character generated app password to the variable named
MAIL_PASSWORD
in.env
file. -
Copy you usual gmail address to the variable named
MAIL_USERNAME
in.env
file.
For more info refer to this.
We use firebase for mobile app notifications. To configure the notification service create a new firebase project and follow these steps:-
-
Create a new Firebase project for Talawa-API
-
When created you will automatically enter the project's console area
-
Click on the settings icon beside the
Project Overview
heading -
Click on
Project Settings
-
Click on the
Service Accounts
tab -
Click on the
Node.js
radio button -
Click on
Generate New Private Key
button -
Confirm by clicking on
Generate Key
. This will automatically download the private keys in your browser. -
Securely store the
JSON
file containing the private key. These will be used in the next section.
The key generated in the previous step is in a format suitable for use in a mobile app. We need to convert it for use by the API. This will require you to do some work in the talawa repository to do the necessary conversion. The resulting output will be stored in a lib/firebase_options.dart
file. Some of the contents of this file will then need to be added to the API's .env
file. Here we go.
-
Clone the talawa mobile app in a separate directory that is not under your Talawa-API directory.
-
Enter that directory as you will need to edit files there
-
Run the following commands to set the key in the environment variable for your respective operating system:
-
Linux/macOS:
export GOOGLE_APPLICATION_CREDENTIALS="/PATH/TO/JSON/FILE/filename.json"
-
Windows:
$env:GOOGLE_APPLICATION_CREDENTIALS="C:\PATH\TO\JSON\FILE\filename.json"
-
-
Install the Firebase CLI.
-
Save the origintal copy the
lib/firebase_options.dart
file as it will be modified. -
Run the following commands in the project directory of talawa mobile app:
firebase login dart pub global activate flutterfire_cli
-
Run any commands about exporting variables from the previous
dart
command. -
Run the following command to configure the application for Firebase
flutterfire configure
-
Select the project you created in the firebase console.
-
Add
iOS
andandroid
platforms to the project. -
Overwrite the
firebase_options.dart
file if asked so. -
The command will generate keys for the
iOS
andandroid
platforms respectively and place them in thefirebase_options.dart
file. -
Edit the
firebase_options.dart
file. -
Add the parameters in the
static const FirebaseOptions android = FirebaseOptions
section of thefirebase_options.dart
file to the Talawa API.env
file under theandroidFirebaseOptions
heading.-
Replace any parameters that are already there in that section.
-
Remove any trailing commas on the lines you have added.
-
Remove any leading spaces on the lines you have added.
-
The final result in the
.env
file should look like thisapiKey: '9f6297b283db701dab7766c993c48b', appId: '1:261699118608:android:366ff7dbdfba5c5a9e8392', messagingSenderId: '261699118608', projectId: 'talawa-thingy', storageBucket: 'talawa-thingy.appspot.com',
-
-
Add the parameters in the
static const FirebaseOptions ios = FirebaseOptions
section of thefirebase_options.dart
file to the Talawa API.env
file under theiosFirebaseOptions
heading. Replace any paramters that are already there.-
Replace any parameters that are already there in that section.
-
Remove any trailing commas on the lines you have added.
-
Remove any leading spaces on the lines you have added.
-
The final result in the
.env
file should look like thisapiKey: 'c2d283aa45f4e858c9cbfe32c58c67', appId: '1:261699118608:ios:1babbb3c07b8461ebdcb2', messagingSenderId: '261699118608', projectId: 'talawa-thingy', storageBucket: 'talawa-thingy.appspot.com', iosClientId: '261699118608-d519b739e43c6214374c0da62feaef.apps.googleusercontent.com', iosBundleId: 'com.example.talawa',
-
-
Undo the changes made to the
firebase_options.dart
file by overwriting it with the version you saved at the beginning of this section.
Install the packages required by talawa-api
using this command:
npm install
Talawa-api development server runs two processes simultaneously in the background. They are:-
-
GraphQL code generator:-
This watches for changes in the graphQL type definition files and generates corresponding typescript types in the background. This results in good code editor experience with typescript. -
Talawa-api server:-
This runs talawa-api directly transpiling the typescript files and running them without emitting as javascript files. It also watches for changes in the code files and restarts the server if it detects any changes.
Run the following command to start talawa-api development server:-
npm run dev
By default talawa-api runs on port 4000
on your system's localhost. It is available on the following endpoint:-
http://localhost:4000/
If you navigate to the endpoint you and see a JSON
response like this it means talawa-api is running successfully:-
{"talawa-version":"v1","status":"healthy"}
GraphQL endpoint for handling queries
and mutations
is this:-
http://localhost:4000/graphql/
GraphQL endpoint for handling subscriptions
is this:-
ws://localhost:4000/graphql/
If port 4000
is not free on your system you can pass a custom environment variable named PORT
to the script to make it use a different port on your system's localhost. Here's the syntax for it:-
PORT=<CUSTOM_PORT_VALUE> npm run dev
where <CUSTOM_PORT_VALUE>
is whatever value you want the PORT
to be. Whatever you pass will be substituted as the value for port and talawa-api development server on that port. Syntax wise it looks like-
http://localhost:<CUSTOM_PORT_VALUE>/
For example:-
PORT=5000 npm run dev
will make talawa-api accessible on the following endpoint:-
http://localhost:5000/
Talawa-api makes use of vitest
to run tests because it is much faster than jest
and more comfortable to work with.
You can run the tests for talawa-api using this command:-
npm run test