Skip to content

swqa7697/cs6650-backend

Repository files navigation

Deploy An API Server For An Airline

1. Clone this repo

Install Git in your system, and run:

git clone https://github.com/swqa7697/cs6650-backend.git
cd cs6650-backend

2. Configure

Before starting the server, there are several things to be prepared:

  1. MongoDB Cluster: the cluster created on Atlas which is used by all api servers within the subsystem of an airline (The free M0 cluster of Atlas is default to have a 3-node replication enabled)
  2. AWS Cognito User Pool: the only user pool that is used by the entire system across all airlines
  3. API Key of API Ninjas: also used by all airlines (only one key required) for determining airport and timezone
  4. [Optional] List of Admin Users: fill the user subs (id used by Cognito) in the array to enable their permissions to add/update flights through API calls (You can get the user sub of yourself when creating your own account manually in the console of Cognito)

In the config file, provide the MongoDB connecting information, Cognito user pool ID, API Ninjas Key, and user subs of admin users. Also indicate the airline name and code. Then, rename the config file to be 'config.json' and put it into the directory '/cs6650-backend/config'

The correct config.json will be like:

{
  "airline": "Airline 1",
  "airlineCode": "AB",

  "mongodbConnectURI": "yourMongoDbConnectingString",
  "dbName": "YourDbName",

  "CognitoRegion": "region",
  "CognitoUserPoolId": "yourUserPoolId",
  "CognitoTokenUse": "access",
  "CognitoTokenExpiration": 8640000,

  "apiNinjasKey": "yourApiKey",

  "adminUsers": ["yourUserSub"]
}

3. Install Dependencies

Install Node.js in your system, and run the command under the directory '/cs6650-backend':

npm install

4. Start Server

npm start

Additional: Deploy The Load Balancer

The load balancer is used for all api servers under the same subsystem/airline

1. Install Nginx

In an extra instance (idealy) or in any one of the api servers (for test/demo), install Nginx and start it by:

(For Ubuntu)

sudo apt install nginx
sudo systemctl start nginx
sudo systemctl enable nginx

2. Configure

Go to the directory:

cd /etc/nginx/conf.d

Create a config file (for example 'load_balancing.conf') with the content of:

upstream backend {
  # Add all backend api servers of this airline below. All of them should call port 3000
  server backend1.example.com:3000;
  server backend2.example.com:3000;
  server backend3.example.com:3000;
  # ... More servers if exist ...
}

server {
  listen 80;
  location / {
    proxy_pass http://backend;
  }
}

Change the URLs like 'backend1.example.com' to be address of your api servers, and keep the ports to be 3000. If you are using EC2 deploying all the servers including load balancer under the same VPC, you can use the private IPv4 address instead of public address

3. Reload Nginx

Then, validate the config and reload Nginx by running:

sudo nginx -t
sudo systemctl reload nginx

Configure API Gateway

The instruction below shows the configuration of Amazon API Gateway, triggering 2 Amazon Lambda Functions

1. Create Required Layer For Lambda

Download the zip file here: axios.zip

  • In your AWS console, go to Lambda - Additional resources - Layers, and click Create layer
  • Fill 'axios' in the fields Name and Description, and keep other options as default
  • Upload the zip file just downloaded and create the layer

Record the Version ARN for later use

2. Create Lambda Functions

Download these 2 files: cs6650-airline-aggregation.zip and cs6650-airline-routing.zip

  • Unzip them and edit the two 'constants.mjs' files according to the comments (configuring the airlines with their related load balancers)
    • The two 'constants.mjs' should be exactly same, so you can just edit one of them and use it for both functions
  • Click Create function in the Lambda console to create function
    • There are two functions should be created, one called aggregation and another one called routing, both using the default Node.js runtime
  • Copy/paste the code from the files you downloaded into the related functions
    • Make sure for both functions, the edited 'constants.mjs' are uploaded
  • For both functions, at the bottom of the Code page, click Add a layer, and choose Specify an ARN and enter the Version ARN you got in the previous step. Add the layer

3. Create API Gateway

In the AWS console, find API Gateway:
  • Click on Create API, and find REST API
    • Then, click on Build
    • Choose New API, and specify an API name
    • Click on Create API
  • In the API just created, use Create resource to build API routes(endpoints) with the same structure of backend servers
    • Enable the CORS option(checkbox) when creating each resource
/flight/flights          GET   aggregation
/flight/new              POST  routing
/reservation/autoCancel  PUT   routing
/reservation/confirm     PUT   routing
/user/book               POST  routing
/user/reservations       GET   aggregation
  • Use Create Method in each route specified above, Choosing the related Method type (GET/POST/PUT)
    • Choose Lambda function as Integration type
    • Enable Lambda proxy integration option
    • Choose the Lambda function to be triggered as specified above
  • For each route created, click on Enable CORS
    • Enable all checkboxes under Gateway responses and Access-Control-Allow-Methods
    • Add two headers in Access-Control-Allow-Headers (separate headers by comma): 'cognito-token' and 'airline-name'
    • Click Save
  • Click on Deploy API to make changes effective (create a new stage)
  • In the Stages page, you can find the Invoke URL of the API gateway you just deployed. Record it for later use in the frontend

Add Testing Data into the Database

Since I didn't implement a UI for adding/updating new flights in the frontend, you can only use the api call /flight/new with Postman to add new flights

If the configuration above are correctly performed, you can use this POST api with a body and required headers as below:

body

{
    "flightCode": "123",
    "departure": "BOS",
    "destination": "SJC",
    "departureTime": "4/30/2024, 11:30:00 AM PDT",
    "travelTime": 100,
    "capacity": 120,
    "price": 400,
    "currency": "USD"
}

headers

'cognito-token': 'cognito access token'
'airline-name': 'airline name configured in the backend'

After logged in, you can find your access token in the Local Storage

You will have to add your User Sub into the config.json to get permission, or you can just remove the permission checking middleware call from the router, which is in '/cs6650-backend/routes/flight.js'

  • Delete ', isCognitoAuthAdmin' from 'router.post('/new', isCognitoAuthAdmin, FlightController.addFlight);' in line 14

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published