/** * Everything a route needs, built once and handed in. * * Routes get this object rather than reaching for module-level singletons, which * is what makes the tests able to stand a whole server up against a fake * environment in-process. Each service owns its own cache and its own failure * behaviour; none of them can throw at a route. */ import { createAuth, type AuthService } from "./auth/index.ts"; import { createFlightsService, type FlightsService } from "./flights/index.ts"; import { createMarkerStore, type MarkerStore } from "./markers/store.ts"; import { createOfficeStore, type OfficeStore } from "./offices/store.ts"; import { createWeatherService, type WeatherService } from "./weather/index.ts"; import type { Config } from "./config.ts"; export interface Services { config: Config; weather: WeatherService; flights: FlightsService; markers: MarkerStore; offices: OfficeStore; auth: AuthService; /** Epoch milliseconds, for `uptimeSeconds` on the health body. */ startedAt: number; } /** The minimum a service needs from a logger. Fastify's satisfies it. */ export interface ServiceLog { warn(msg: string): void; } export function createServices(config: Config, log: ServiceLog): Services { return { config, weather: createWeatherService(config, log), flights: createFlightsService(config, log), markers: createMarkerStore(config, log), offices: createOfficeStore(config.offices.dir), auth: createAuth(config.auth), startedAt: Date.now(), }; }