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.vars
to.dev.vars
cp .dev.example.vars .dev.vars
-
Generate
UPSTASH_REDIS_REST_TOKEN
usingopenssl rand -hex 32
and copy it to your.dev.vars
file -
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.toml
towrangler.toml
and fill the necessary information -
Create a new PostgreSQL database in Neon and copy the connection string value as
DATABASE_URL
in your.production.vars
file -
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_ENV
to your.production.vars
file
COMULINE_ENV=production
-
Create a new Redis database in Upstash and copy the value of
UPSTASH_REDIS_REST_TOKEN
andUPSTASH_REDIS_REST_URL
to your.production.vars
file -
Save your
.production.vars
file 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 deploy
TBD