Skip to content

B21-CAP0164/Cloud_Laporaja-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 

Repository files navigation

Laporaja API

The main function of this API is:

  • Send GET request to get all the reports data from the database.
  • Send GET request to get the reports history of a user
  • Send POST request to post the report data to the database.

This API is using Django as its framework and MySQL for its database. There are no authentication feature yet in this API, because we did the authentication in the Android side. We use Google account to sign in to the apps. We deploy the API on Google Cloud Run, but for testing purpose we suggest to use local environtment.

The database used in this API is actually a non-relational database. We suggest you use noSQL database like Firestore to store the data. Because the project's data structure is changed in the last minutes, we didn't have time to change the database to noSQL database.

Local Configuration

  1. Clone the project

    git clone https://github.com/B21-CAP0164/Cloud_Laporaja-API.git
  2. Rename settings.py to settings-cloud-run.py and settings-local.py to setting.py

  3. Create virtual environment
    Install pip

    sudo apt-get install python3-pip
    sudo pip3 install virtualenv

    Create virtual environment called "venv"

    virtualenv venv

    Activate the environment

    source venv/bin/activate
  4. Install dependencies

    pip install -r requirements.txt
  5. Create MySQL database and grant user
    Login to MySQL database as root user

    mysql -u root -p

    Create database

    CREATE DATABASE dbname;

    Create user and grant its privileges

    CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
    GRANT ALL PRIVILEGES ON dbname . * TO 'newuser'@'localhost';

    Exit MySQL

    quit;
  6. Fill the necessary information in laporaja-api/settings.py
    You need to fill secret key and MySQL database connection in the settings file. You can visit https://djecrety.ir/ to generate secret key.

  7. Migrate for the first time

    python manage.py migrate
  8. Create Admin account for Django Admin page

    python manage.py createsuperuser
  9. Create migration for Admin

    python manage.py makemigration
  10. Migrate for the Admin migration

    python manage.py migrate
  11. Run the server

    python manage.py runserver

Cloud Run Configuration

We follow the steps from Google Cloud documentation to deploy the API on Cloud Run. To deploy model service using Flask framework just jump to the Deploy step.

Initialization

  1. Login to GCP or create an account. If you new you can activate free trial with the amount of $300 credits.
  2. Choose a project or create one.
  3. Enable billing. For more information about enabling billing, you can visit here.
  4. Enable API for Cloud SQL, Cloud Run, Cloud Build, Secret Manager, and Compute Engine API.
  5. Ensure sufficient permissions are available. The minimum permission needed are below.
    • Cloud SQL Admin
    • Storage Admin
    • Cloud Run Admin
    • Secret Manager Admin

Preparing the backend services

Detail information about services that we use.

Service Usage
Cloud Storage To save the API static files
Cloud SQL For database
Secret Manager To store secret key and password
Cloud Run To deploy the API container

Create MySQL database and grant user on Cloud SQL

Create MySQL instances
  1. In GCP console, go to the Cloud SQL page.
  2. Click Create Instance.
  3. Choose MySQL.
  4. Fill the necessary informations.
  5. Click Create
Create database
  1. Go to the Instance page.
  2. Click Instance name.
  3. Click Create database.
  4. Fill the necessary informations.
  5. Click Create
Create user and grant privileges
  1. In the cloud shell, connect to the instances.
    gcloud sql connect [INSTANCE NAME] --user=root
  2. Create user and grant privileges.
    WARNING! Using % as host is the same as making the user to have root level access by granting access from all host.
    CREATE USER '[USER-NAME]'@'%' IDENTIFIED BY 'password';
    GRANT ALL PRIVILEGES ON [DB-NAME] . * TO '[USER-NAME]'@'localhost';

Grant Cloud Build to acess Clous SQL

  1. Go to IAM page.
  2. Edit role of PROJECTNUM@cloudbuild.gserviceaccount.com.
  3. Click Add another role.
  4. In the Select a role dialog, select Cloud SQL Client.
  5. Click Save.

Create Cloud Storage bucket

In cloud shell type

gsutil mb -l REGION gs://PROJECT_ID-media

Save secret key and password with Secret Manager

Django Setting secret
  1. Create .env with this value
    DATABASE_URL=mysql://[USER-NAME]:[PASSWORD]@/[DB-NAME]?unix_socket=/cloudsql/${PROJECT_ID}:${REGION}:<instances-name>
    GS_BUCKET_NAME=${PROJECT_ID} 
    SECRET_KEY=(a random string, length 50)
  2. In cloud console go to Secret Manager page
  3. Click Create Secret, and upload the .env file.
  4. Click Create Secret
  5. In the right side of the screen click Add Member
  6. In the New Members field, enter PROJECTNUM-compute@developer.gserviceaccount.com, and then press Enter.
  7. In the New Members field, enter PROJECTNUM@cloudbuild.gserviceaccount.com, and then press Enter.
  8. Set the role to Secret Manager Secret Accessor.
  9. Click Save.
Django Admin password
  1. In cloud console go to Secret Manager page
  2. Click Create Secret, and insert random password in the value field.
  3. Click Create Secret
  4. In the right side of the screen click Add Member
  5. In the New Members field, enter PROJECTNUM@cloudbuild.gserviceaccount.com, and then press Enter.
  6. Set the role to Secret Manager Secret Accessor.
  7. Click Save.

Deploy to Cloud Run

  1. Clone the project.
    git clone https://github.com/B21-CAP0164/Cloud_Laporaja-API.git
  2. Run the cloudmigration.yaml file.
    gcloud builds submit --config cloudmigrate.yaml \
    --substitutions _INSTANCE_NAME=[INSTANCE-NAME],_REGION=[REGION]
  3. Deploy to Cloud Run.
    gcloud run deploy [SERVICE-NAME] \
    --platform managed \
    --region [REGION] \
    --image gcr.io/[PROJECT_ID]/[SERVICE-NAME] \
    --add-cloudsql-instances [PROJECT_ID]:[REGION]:[INSTANCE_NAME] \
    --allow-unauthenticated
    • Admin page : url/admin
    • GET all report : url/request
    • GET report history (for 1 user ID) : url/request/[USER-ID]
    • POST report : url/request/[USER-ID]/add

About

API to serve Laporaja mobile apps requests. This API is deployed in Google Cloud Run.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors