An API to get the schedule of KRL commuter line in Jakarta and Yogyakarta using Hono and Bun, deployed to Cloudflare Workers. This API is primarily used on the web app (source code).
This API uses a daily cron job (at 00:00) to fetch the schedule of KRL commuter line in Jakarta and Yogyakarta from the official website of PT. KAI. The data is then processed and stored in a PostgreSQL database and cached in a Redis (for every once read request). All endpoints can be found in the docs.
- Hono API framework
- Bun runtime
- (Serverless) PostgresSQL (Neon)
- (Serverless) Redis (Upstash)
- Cloudflare Workers deployment platform
- Drizzle ORM
- Clone the repository
git clone https://github.com/comuline/api.git- Install the dependencies
bun install- Copy the
.dev.example.varsto.dev.vars
cp .dev.example.vars .dev.vars
-
Generate
UPSTASH_REDIS_REST_TOKENusingopenssl rand -hex 32and copy it to your.dev.varsfile -
Run database locally
docker-compose up -d- Run the database migration
bun run migrate:apply- Sync the data and populate it into your local database (once only as you needed)
# Please do this in order
# 1. Sync station data and wait until it's done
bun run sync:station
# 2. Sync schedule data
bun run sync:schedule-
Rename the
wrand.example.tomltowrangler.tomland fill the necessary information -
Create a new PostgreSQL database in Neon and copy the connection string value as
DATABASE_URLin your.production.varsfile -
Run the database migration
bun run migrate:apply- Sync the data and populate it into your remote database (once only as you needed)
# Please do this in order
# 1. Sync station data and wait until it's done
bun run sync:station
# 2. Sync schedule data
bun run sync:schedule- Add
COMULINE_ENVto your.production.varsfile
COMULINE_ENV=production
-
Create a new Redis database in Upstash and copy the value of
UPSTASH_REDIS_REST_TOKENandUPSTASH_REDIS_REST_URLto your.production.varsfile -
Save your
.production.varsfile to your environment variables in your Cloudflare Workers usingwrangler
bunx wrangler secret put --env production $(cat .production.vars)- Deploy the API to Cloudflare Workers
bun run deployTBD