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.
-
Clone the project
git clone https://github.com/B21-CAP0164/Cloud_Laporaja-API.git
-
Rename settings.py to settings-cloud-run.py and settings-local.py to setting.py
-
Create virtual environment
Install pipsudo apt-get install python3-pip sudo pip3 install virtualenv
Create virtual environment called "venv"
virtualenv venv
Activate the environment
source venv/bin/activate -
Install dependencies
pip install -r requirements.txt
-
Create MySQL database and grant user
Login to MySQL database as root usermysql -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; -
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. -
Migrate for the first time
python manage.py migrate
-
Create Admin account for Django Admin page
python manage.py createsuperuser
-
Create migration for Admin
python manage.py makemigration
-
Migrate for the Admin migration
python manage.py migrate
-
Run the server
python manage.py runserver
- Admin page : http://localhost:8000/admin
- GET all report : http://localhost:8000/request
- GET report history (for 1 user ID) : http://localhost:8000/request/[USER-ID]
- POST report : http://localhost:8000/request/[USER-ID]/add
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.
- Login to GCP or create an account. If you new you can activate free trial with the amount of $300 credits.
- Choose a project or create one.
- Enable billing. For more information about enabling billing, you can visit here.
- Enable API for Cloud SQL, Cloud Run, Cloud Build, Secret Manager, and Compute Engine API.
- Ensure sufficient permissions are available. The minimum permission needed are below.
- Cloud SQL Admin
- Storage Admin
- Cloud Run Admin
- Secret Manager Admin
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 |
- In GCP console, go to the Cloud SQL page.
- Click Create Instance.
- Choose MySQL.
- Fill the necessary informations.
- Click Create
- Go to the Instance page.
- Click Instance name.
- Click Create database.
- Fill the necessary informations.
- Click Create
- In the cloud shell, connect to the instances.
gcloud sql connect [INSTANCE NAME] --user=root
- 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';
- Go to IAM page.
- Edit role of PROJECTNUM@cloudbuild.gserviceaccount.com.
- Click Add another role.
- In the Select a role dialog, select Cloud SQL Client.
- Click Save.
In cloud shell type
gsutil mb -l REGION gs://PROJECT_ID-media- 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)
- In cloud console go to Secret Manager page
- Click Create Secret, and upload the .env file.
- Click Create Secret
- In the right side of the screen click Add Member
- In the New Members field, enter PROJECTNUM-compute@developer.gserviceaccount.com, and then press Enter.
- In the New Members field, enter PROJECTNUM@cloudbuild.gserviceaccount.com, and then press Enter.
- Set the role to Secret Manager Secret Accessor.
- Click Save.
- In cloud console go to Secret Manager page
- Click Create Secret, and insert random password in the value field.
- Click Create Secret
- In the right side of the screen click Add Member
- In the New Members field, enter PROJECTNUM@cloudbuild.gserviceaccount.com, and then press Enter.
- Set the role to Secret Manager Secret Accessor.
- Click Save.
- Clone the project.
git clone https://github.com/B21-CAP0164/Cloud_Laporaja-API.git
- Run the cloudmigration.yaml file.
gcloud builds submit --config cloudmigrate.yaml \ --substitutions _INSTANCE_NAME=[INSTANCE-NAME],_REGION=[REGION]
- 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