This guide explains how automation agents and human contributors should work with the StationAPI repository so releases stay predictable, auditable, and safe. Update this file whenever you change the workflow or behavior it documents.
stationapi/src/domain/– Entity definitions and repository abstractions. Theentity/module mirrors the gRPC schema,repository/providesasync_trait-based interfaces, andnormalize.rscontains text normalization for search.stationapi/src/use_case/– Application logic.interactor/query.rsimplements theQueryUseCasecontract defined intraits/query.rs.stationapi/src/infrastructure/– SQLx repositories for PostgreSQL.My*Repositorytypes share anArc<PgPool)and integration tests live behind#[cfg_attr(not(feature = "integration-tests"), ignore)].stationapi/src/presentation/– gRPC presentation layer.presentation/controller/grpc.rswiresMyApito the generated server types. Health checks and reflection are enabled throughtonic-healthandtonic-reflection.stationapi/proto/stationapi.proto– The gRPC contract.build.rsusestonic-buildto generate server code andFILE_DESCRIPTOR_SET.data/– Canonical CSV datasets and schema definition (create_table.sql). Files follow theN!table.csvnaming scheme to control import order. Detailed instructions are indata/README.md.data_validator/– CLI that verifies cross-file constraints (cargo run -p data_validator).docker/&compose.yml– Container definitions for the API and PostgreSQL 18.docker/postgres/00_extensions.sqlensurespg_trgmandbtree_gistare installed.Makefile– Convenience targets for running tests (make test-unit,make test-integration,make test-all).
- Rust: Use the stable toolchain (
rustup default stable). Docker images build withrust:1. - Protobuf:
protocmust exist when building locally (the Docker image installs it automatically). - PostgreSQL: Version 15+ with
pg_trgmandbtree_gistextensions. Compose spins up PostgreSQL 18 with these extensions preloaded. - Required environment variables:
DATABASE_URL– SQLx connection string (e.g.,postgres://stationapi:stationapi@localhost/stationapi).DISABLE_GRPC_WEB–falseenables gRPC-Web; set totruefor pure gRPC/HTTP2.HOSTandPORT– listen address (defaults to[::1]:50051; Docker uses0.0.0.0:50051)..env.testexportsTEST_DATABASE_URL,RUST_LOG,RUST_BACKTRACE, andRUST_TEST_THREADS=1for integration tests.
- Recommended: copy
.envto.env.local, override local values, and rely ondotenv::from_filename(".env.local")during startup.
- Local development
- Prepare PostgreSQL and set
DATABASE_URL. cargo run -p stationapirebuilds the schema fromdata/create_table.sql, imports everydata/*.csv, and then boots the gRPC server. If extension creation fails, grant the needed privileges manually.- Health checks respond to
grpc.health.v1.Health/Check; gRPC Reflection is available for tooling such asgrpcurl.
- Prepare PostgreSQL and set
- Docker / Compose
docker compose up --buildlaunches both the API and PostgreSQL containers. Source code mounts into/app, but hot reload is not provided; rebuild after code changes.- Production images rely on
docker/api/Dockerfile, which runscargo build -p stationapi --releaseand copiesdata/.
- gRPC-Web
- The server accepts HTTP/1.1 and wraps handlers via
tonic_web::enable. Disable this behavior by exportingDISABLE_GRPC_WEB=trueto require HTTP/2 clients only.
- The server accepts HTTP/1.1 and wraps handlers via
- CSV import order depends on the numeric prefix (
1!,2!, ...). When adding datasets, choose a prefix that preserves foreign-key dependencies. data/create_table.sqldrops and recreates tables, indexes, and foreign keys. Update this script alongside any schema or CSV column changes.data_validatorcurrently verifies that5!station_station_types.csvreferences valid station and type IDs. Extend the validator when new cross-references are introduced and keep the process fail-fast (panic on invalid data).
- Unit tests –
cargo test --lib --package stationapiormake test-unit; focus on entities and repository mocks without a database. - Integration tests –
source .env.test && cargo test --lib --package stationapi --features integration-testsormake test-integration. Use a dedicated schema behindTEST_DATABASE_URLand clean it up after runs. - Full suite –
make test-allruns unit then integration tests sequentially. SetRUST_LOG=debugto inspect SQL queries during debugging. - Linting and formatting – Run
cargo fmtandcargo clippy --all-targets --all-featuresbefore committing. Resolve new Clippy warnings unless an existing#![allow]covers the case. - Data verification – Execute
cargo run -p data_validatorwhenever CSVs change and record results in pull requests.
- Stations –
GetStationById,GetStationByIdList,GetStationsByGroupId,GetStationsByCoordinates,GetStationsByLineId,GetStationsByName,GetStationsByLineGroupId.QueryInteractorenriches stations with lines, companies, station numbers, and train types. - Lines –
GetLineById,GetLinesByIdList,GetLinesByName. Results include company data and computed line symbols based on repository helpers. - Routes –
GetRoutes,GetRoutesMinimal. The minimal variant returnsRouteMinimalResponsewith deduplicatedLineMinimaldata; paging tokens are currently empty (pagination not implemented). - Train types –
GetTrainTypesByStationId,GetRouteTypes. Train types aggregate by line group and include related lines plus optional train type metadata. - Connected routes –
GetConnectedRoutes.QueryInteractor::get_connected_stationsis not implemented yet and returns an empty vector; update the use-case and infrastructure layers together when adding real logic. - Changes to the service contract require coordinated updates to
proto/stationapi.proto, regenerated code viatonic-build, and corresponding adjustments in both presentation and use-case layers.
- Document the commands you executed (for example,
cargo fmt && cargo clippy --all-targets --all-features && make test-unit) and their outcomes in every pull request. - For database, gRPC, or schema updates, add architectural notes under
docs/and synchronize README references so onboarding materials stay accurate. - When modifying
QueryInteractor, ensure the enrichment steps (companies, train types, line symbols) still behave as expected. Double-check helper methods such asupdate_station_vec_with_attributesandbuild_route_tree_map. - Introducing new tables, endpoints, or feature flags must come with matching updates to this document and any other affected guidance.
Keep this guide aligned with the repository. If a workflow, environment requirement, or endpoint changes, update AGENTS.md in the same pull request so automation agents and contributors work from current instructions.