import { readFileSync } from "fs"; import { ApolloServer } from "@apollo/server"; import { startStandaloneServer } from "@apollo/server/standalone"; import { MergedResolvers } from "./MergedResolvers"; import { ServerContext } from "./ServerContext"; import { UnoptimizedInMemoryShuttleRepository } from "./repositories/UnoptimizedInMemoryShuttleRepository"; import { TimedApiBasedShuttleRepositoryLoader } from "./loaders/TimedApiBasedShuttleRepositoryLoader"; import { ETANotificationScheduler } from "./notifications/schedulers/ETANotificationScheduler"; import { loadShuttleTestData } from "./loaders/loadShuttleTestData"; import { AppleNotificationSender } from "./notifications/senders/AppleNotificationSender"; import { InMemoryNotificationRepository } from "./repositories/InMemoryNotificationRepository"; import { NotificationRepository } from "./repositories/NotificationRepository"; import { RedisNotificationRepository } from "./repositories/RedisNotificationRepository"; const typeDefs = readFileSync("./schema.graphqls", "utf8"); async function main() { const server = new ApolloServer({ typeDefs, resolvers: MergedResolvers, introspection: process.env.NODE_ENV !== "production", }); const shuttleRepository = new UnoptimizedInMemoryShuttleRepository(); let notificationRepository: NotificationRepository; let notificationService: ETANotificationScheduler; if (process.argv.length > 2 && process.argv[2] == "integration-testing") { console.log("Using integration testing setup") await loadShuttleTestData(shuttleRepository); const appleNotificationSender = new AppleNotificationSender(false); notificationRepository = new InMemoryNotificationRepository(); notificationService = new ETANotificationScheduler( shuttleRepository, notificationRepository, appleNotificationSender ); notificationService.startListeningForUpdates(); } else { const repositoryDataUpdater = new TimedApiBasedShuttleRepositoryLoader( shuttleRepository, ); await repositoryDataUpdater.start(); const redisNotificationRepository = new RedisNotificationRepository(); await redisNotificationRepository.connect(); notificationRepository = redisNotificationRepository; notificationService = new ETANotificationScheduler( shuttleRepository, notificationRepository ); notificationService.startListeningForUpdates(); } const { url } = await startStandaloneServer(server, { listen: { port: process.env.PORT ? parseInt(process.env.PORT) : 4000, }, context: async () => { return { shuttleRepository, notificationRepository, } }, }); console.log(`Server ready at: ${url}`); } main();