GithubHelp home page GithubHelp logo

sinardyas / comuline-api Goto Github PK

View Code? Open in Web Editor NEW

This project forked from comuline/api

0.0 0.0 0.0 198 KB

The API for Indonesian public transportation data, built using Elysia

Home Page: https://www.api.comuline.com/docs

JavaScript 0.24% TypeScript 99.28% Dockerfile 0.47%

comuline-api's Introduction

@comuline/api

An API to get the schedule of KRL commuter line in Jakarta and Yogyakarta using Elsyia and Bun, deployed to Render. This API is primarily used on the web app (source code).

How does it work?

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.

Technology stacks

  1. Elsyia API framework
  2. Bun runtime
  3. PostgresSQL (Neon)
  4. Redis (Upstash)
  5. Render deployment platform
  6. Drizzle ORM

Getting Started

Development

  1. Clone the repository
git clone https://github.com/comuline/api.git
  1. Install the dependencies
bun install
  1. Run database locally
docker-compose up -d
  1. Copy the .env.example to .env
cp .env.example .env
  1. Run the database migration
bun db:generate && bun db:migrate
  1. 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
curl --request POST --url http://localhost:3001/v1/station/
# 2. Sync schedule data
curl --request POST --url http://localhost:3001/v1/schedule/

Deployment

  1. Create a new PostgreSQL database in Neon and copy the connection string value as DATABASE_URL in your .env file

  2. Run the database migration

bun db:generate && bun db:migrate
  1. 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
curl --request POST --url http://localhost:3001/v1/station/
# 2. Sync schedule data
curl --request POST --url http://localhost:3001/v1/schedule/
  1. Generate SYNC_TOKEN (This is used in production level only to prevent unauthorized access to your POST /v1/station and POST /v1/schedule endpoint)
openssl rand -base64 32
# Copy the output value as a `SYNC_TOKEN`
  1. Create a new Redis database in Upstash and copy the connection string value as REDIS_URL

  2. Create a Web Service in Render, copy the DATABASE_URL, REDIS_URL, and SYNC_TOKEN as environment variables, and deploy the application.

  3. Set the cron job to fetch the schedule data using Cron-Job. Don't forget to set the SYNC_TOKEN as a header in your request. Add the ?from_cron=true query parameter to flag the request as a cron job request.

# Example
curl --request POST --url https://your-service-name.onrender.com/v1/station?from_cron=true -H "Authorization: Bearer ${SYNC_TOKEN}"
curl --request POST --url https://your-service-name.onrender.com/v1/schedule?from_cron=true -H "Authorization: Bearer ${SYNC_TOKEN}"

Database schema

Station

Column Name Data Type Description
id TEXT Primary key (Station ID)
name TEXT Station name
daop INTEGER Station regional operation code
fgEnable BOOLEAN -
haveSchedule BOOLEAN Schedule availability status
updatedAt TEXT Last updated date

Schedule

Column Name Data Type Description
id TEXT Primary key (Station ID + Train ID)
stationId TEXT Station ID
trainId TEXT Train ID
line TEXT Train commuter line
route TEXT Train route
color TEXT Commuter line color
destination TEXT Train destination
timeEstimated TIME Estimated time
destinationTime TIME Destination time
updatedAt TEXT Last updated date

Sync

Column Name Data Type Description
id TEXT Primary key (Sync ID)
n BIGINT n of sync
type ENUM Sync type (manual, cron)
status ENUM Sync status (PENDING, SUCCESS, FAILED)
item ENUM Sync item (station, schedule)
duration BIGINT Sync duration
message TEXT Sync message (if status failed)
startedAt TEXT Sync started date
endedAt TEXT Sync ended date
createdAt TEXT Sync created date

comuline-api's People

Contributors

abielzulio avatar theodorusclarence avatar dendianugerah avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.