A website to view arrival times for the NYC subway! Mirrored to https://github.com/gavinmorrow/sbwy. sbwy.gavinmorrow.com
nyc subway gleam mta-nyct gtfs
README.md

sbwy #

This is a little web app to view subway arrival times for the NYC Subway :]

It sends over server-side rendered HTML pages using lustre, which are then hydrated (if possible) on the client. It aims to be progressively enhanced, and work without javascript enabled.

This project is mirrored between GitHub and Tangled.

Subprojects #

  • server: The code for the HTTP server. Targets Erlang. Run via just run (which will build the client first) or just server run (which won't).
  • shared: Shared code between the server and client. It contains all of the lustre view code—ie fn view() and type Model. This allows for server-side rendering.
  • client: Contains code that runs exclusively in the browser. It contains all the interactive bits of lustre code—ie fn init(), fn update(), and type Msg. Build with just build-pages or just build-page <page-name>.
  • gtfs: GTFS parsing code for both static and realtime.

Development #

There are a few just recipes available for use.

To run the project: build the client, start the server, then go to localhost:8080 in your web browser of choice. You can also use just run.

If this is your first time running it, make sure that the env var are set to fetch from the internet and save the values to disk. After doing gleam run once followed by gleam run -m gen_schedule (inside of gtfs), they will be saved, so development can continue locally without re-fetching and parsing each time. The folder ./gtfs/priv/ may need to be created. Disable fetch and save to disk afterwards, and only enable when needed.

To test the project, run just test.

Env vars #

name description
host The interface to bind to when starting the server
port The port to bind to when starting the server
gtfs_st If "local": use the cached st data.
gtfs_rt If "local": use the cached rt data.
save_fetched_st If "true": write fetched st data to disk.
save_fetched_rt If "true": write fetched rt data to disk.
gtfs_rt_fetch_time The unix time in sec the cached rt was fetched at.
log_level The minimum log level to show. Defaults to all.
log_tz_offset The offset (in hours) from GMT the log timestamps are in.
profile_pages Which pages to enable eflame profiling for.
profile_funcs If true, log the duration of certain functions.

Log Levels #

This project uses the Erlang logger. The levels are emergency/alert/critical/error/warning/notice/info/debug.

Profiling #

For profile_pages, it is a comma-separated list with potential values of: stops, stop, stop_alerts, and train.

Extra st data #

Some data about stations (e.g. what borough they are in) is semi-manually stored. data.gov provides it, but the exact format may change slightly every so often, making it more unreliable to parse live. Furthermore, this type of data changes very very infrequently.

If a station is added or removed to the subway (or the data otherwise changes):

  1. cd into the gtfs project
  2. Download the CSV output from https://catalog.data.gov/dataset/mta-subway-stations and save it into ./MTA_Subway_Stations.csv.
  3. Run gleam run -m build_st_extra.

Deploying #

Locally:

  1. just build
  2. cd server
  3. gleam export erlang-shipment
  4. rsync -r ./build/erlang-shipment/ user@example.com:/home/user/sbwy

On the server: log_tz_offset="-4" ~/sbwy/entrypoint.sh run

Updating nginx config #

  1. rsync nginx.conf user@example.com:/home/user/nginx.conf
  2. SSH into server
  3. To verify config: sudo nginx -t
  4. To reload config: sudo nginx -s reload