This repository hosts our API for the application Kawan Usaha. As the CC team, we made this API to serve and manage various data such as articles and users but also to connect between the MD side to the ML side. After MD sends the conversation, we can forward it to the model. After that, we will also forward the answers we got from the model to the application.
- Gabrielle Evan Farrel (C151DSX2011)
- Muhammad Kholif Alami (C151DSX1941)
Before we start, I’ll explain the purpose of the Kawan-Usaha API. It’s used to manage and serve all the backend needs of the application. It also is used as a proxy to access the AI model for the chatbot function.
The picture above is our cloud infrastructure. The mobile app accesses the API through a static domain provided to refer to our API. The API is hosted on Cloud Run for easy CI/CD (Continous Integration/Continous Deployment) and the ability to stream messages. The API uses Cloud SQL with PostgreSQL for database and it can access the compute engine where the model is being hosted for inference.
Default API Message (Without JWT)
Kawan-Usaha API Repository
Google Cloud Platforn with enough permissions (Cloud Run Admin, Network Admin, Compute Engine Admin, Cloud SQL Admin)
Enough credits for all the resources
- Open navigation menu and go to SQL
- Create new PostgreSQL instance with this following configurations:
- Region: as needed
- Single Zone (cheaper)
- Machine Type: Standard 1vCPU 3.75 GB
- Storage: SSD 20GB
- Network: Default, Public IP, Private IP.
- Connect to the instance and create new user and database.
- Database is ready to use.
- Open navigation menu and go to Cloud Storage.
- Create a new bucket with this following configurations.
- Bucket name: needs to be unique.
- Availability: single region (cheap) multi region (high availability).
- Class: Standard.
- ACL: Fine grained, uncheck the enforce public access.
- Protection: None, or retention policy
- After the bucket is created, click on the name.
- Go to PERMISSIONS tab, and grant access to AllUsers the permission “Storage Object Viewer” to make it publicly readable.
- Open the navigation menu, and go to IAM & Admin
- Go to Service Accounts on the Sidebar
- Either use an existing service account or create a new one to access the cloud SQL and cloud Storage
- Click on the name of the service account of your choosing
- Go to the KEYS tab
- Press the ADD KEY button and create a new JSON key
- Save that key to be used in deploying the API later
- Go back to IAM to grant permissions for the service account
- Give the service account these permissions:
- Cloud SQL Editor
- Storage Object Creator
Cloud Run Deployment
- Clone repository to local machine with git clone https://github.com/Kawan-Usaha/Kawan-Usaha-API.git
- Move your Service Account Key to the directory.
- Copy the .env.example file to .env
- Fill out the necessary variables inside the environment
- Use this following command:
gcloud run deploy kawan-usaha-cloud-run \
--allow-unauthenticated \
--service-account=service@account:you.used \
--timeout=600 \
--max-instances=5 \
--set-cloudsql-instances=sql-instance-name:you:have-made \
--binary-authorization default \
--region=your-region \
--source .
- The application is now deployed on your project.
Cloud Domain
- Go to deployed Cloud Run service
- Go to the information button near the URL
- Go to Manage Custom Domains
- Click Register Domain
- Pick the domain you want, and pay it.
- Map the service to the domain
Cloud VPC - Firewall
- Go to VPC Network - Firewall
- Create a new firewall rule
- Create a rule that allows TCP:8000 for the ML API
- Confirm
Commit history is available on https://github.com/Kawan-Usaha/Kawan-Usaha-API/commits/main.