thecodinglove-kotlinjs is a cutting-edge Kotlin multiplatform project, powering TheCodingLove GIFs Slack app. Built with KotlinJS, it seamlessly bridges Kotlin and Javascript to bring a fully serverless platform, currently deployed as microservice Docker containers on Google Cloud using Pulumi infrastructure as code.
š Tech stack
- Hexagon microservice architecture - implemented as
domain,adapterandservicesub-projects for each microservice - KotlinJS - NodeJS transpiling
- PubSub - event-driven messaging
- Firestore - NoSQL database
- Docker - containerised deployment
- Cloud Run - serverless deployment of microservices
- Cloud Scheduler - cron jobs
- GitHub Actions - CI automation
- Pulumi - infrastructure as code, using micro-stacks
- nginx - web reverse proxy
- Google Analytics - analytics and engagement measurement
š Live demo
The project can be run locally and deployed on the cloud - in our case to Google Cloud via Pulumi. The local build talks to a Firestore database and sends PubSub messages, so the cloud setup is a prerequisite.
The below setup assumes you've already cloned the project locally.
1ļøā£ Google Cloud setup
- Create a new Google Cloud project.
- Create a Service Account for the infrastructure as code setup with the following roles:
Artifact Registry AdministratorFirebase AdminService Account UserService Usage AdminPub/Sub AdminCloud Scheduler AdminCloud Run Admin- (Optional) If you're specifying a custom domain mapping, as we are, verify domain ownership and add your service account as owner.
- Export a JSON API key for your Service Account and call it
credentials-gcp-infra.json. - Signup and Install Pulumi locally.
- Create a Pulumi access token and login locally using
pulumi login. - The project uses Pulumi micro-stacks to deploy the microservices individually. Each microservice has a corresponding
infrafolder containing itsPulumi.yamlinfrastructure program, egsearch/infra. To get the project going, you will need to manually initialise each microservice on GCP using the Pulumi scripts.
The order to do this matters, so go with common/infra first, then all other microservices, then proxy-web/infra. The reason is that the resouces are created incrementally at each stage and we currently have no way synchronize them.
- The steps to deploy a microservice's infrastructure is the same for all:
- Navigate to its
infrafolder. - Paste the
credentials-gcp-infra.jsonfile. - Create a new empty Pulumi project with no resources using the
pulumi newcommand and follow the instructions:- you can use the prompt
Empty project with no resourcesfor Pulumi AI; - you can use
prodas your stack name;
- you can use the prompt
- Replace the
namein the microservicePulumi.yamlwith the value you entered in the prompt. - Open
Pulumi.prod.ymland replace thegcp:projectvalue with your project id. - Run
pulumi upto automatically create the required microservice infrastructure. - Repeat for the remaining microservices.
- Navigate to its
- Find your auto-generated
firebase-adminsdkService Account and give it the following additional roles:Pub/Sub Publisher, for publishing messages to PubSub topics;
- Export a JSON API key for your
firebase-adminsdkService Account and call itcredentials-gcp-app.json- the app will need it later.
2ļøā£ Slack setup
The project powers an existing Slack app, so you'll need one in order to run it.
- Create a new Slack app.
- You will need an SSH tunnel to your localhost for Slack's APIs. You can use serveo.net for free and configure it with this command
ssh -R YOUR_DOMAIN.serveo.net:80:localhost:3000 serveo.net. - Point the following Slack features to the relevant project API endpoints that know how to respond to them using the url you used for serveo.net:
- Slash commands ->
YOUR_DOMAIN.serveo.net/api/slack/slash - OAuth ->
YOUR_DOMAIN.serveo.net/api/slack/auth - Events ->
YOUR_DOMAIN.serveo.net/api/slack/event - Interactivity ->
YOUR_DOMAIN.serveo.net/api/slack/interactivity
- Slash commands ->
- Make a note of your
Slack Client ID,SecretandSigning Secret.
3ļøā£ Local setup
- Install Docker Desktop and start it up. No additional configuration is required as the project uses Docker Compose to run locally. Checkout the
dockerfolder for the setup. - Install IntelliJ. This project has been tested with
IntelliJ IDEA 2023.2.5. - Open the root project with IntelliJ and wait for it to initialise.
- Create a Slack channel to receive server error messages and monitoring updates. The project is configured to post all unhandled
Throwables to that channel. We use the Incoming Webhooks app to obtain a channel URL webhook. - Create a Google Analytics 4 property and an API secret.
- Create the following
secrets.propertiesfiles:
# Under /slack/domain/secrets.properties
SLACK_SIGNING_SECRET=YOUR_SLACK_SIGNING_SECRET
SLACK_CLIENT_ID=YOUR_SLACK_CLIENT_ID
SLACK_CLIENT_SECRET=YOUR_SLACK_CLIENT_SECRET
# Under /slack-web/domain/secrets.properties
SLACK_CLIENT_ID=YOUR_SLACK_CLIENT_ID
# Under /common/monitoring/secrets.properties
MONITORING_SLACK_URL=YOUR_MONITORING_SLACK_URL
# Under /common/analytics/secrets.properties
GOOGLE_ANALYTICS_MEASUREMENT_ID=YOUR_ANALYTICS_MEASUREMENT_ID
GOOGLE_ANALYTICS_API_SECRET=YOUR_ANALYTICS_API_SECRET
- Copy the
credentials-gcp-app.jsonService Account JSON API key to the root project folder. - Make sure you select the right Java version. If you don't have it installed locally, use latest Azul Zulu on Intellij
After completing the setup, you should be able to run the project locally using the TheCodingLove-Docker IntelliJ IDE configuration. There is a landing page that should be available when you navigate to your serveo.net url.
This is really up to you! However, we've provided our setup below.
GitHub Actions
The project is configured to build with GitHub Actions and have a separate workflow for each microservice. Checkout the .github folder for details. Follow these steps to configure the CI environment:
- Add your Pulumi access token as a GitHub encrypted secret with the name
PULUMI_ACCESS_TOKEN. - Each of the variables defined in the
secrets.propertiesfiles you created above should be exposed as GitHub encrypted secrets, using the same names as keys. - Add an additional
GCP_SA_KEY_INFRAGitHub encrypted secret, containing the raw JSON API key for the above infrastructure as code Service Account. - Add an additional
GCP_SA_KEY_APPGitHub encrypted secret, containing the raw JSON API key for thefirebase-adminsdkService Account. - (Optional) Install the Pulumi GitHub app to get automated summaries of your infrastructure as code changes directly on your PR.
Once this is done:
- opening pull requests against the repo will trigger build/test checks as well as infrastructure changes preview for the microservice that has been changed;
- merging pull requests to the main branch deploys the changes to the corresponding microservice to Google Cloud;