From bf7a290863d519d1d70963ca8d67cd722f5bd0cb Mon Sep 17 00:00:00 2001 From: Thibault Deutsch Date: Sat, 9 Dec 2017 16:26:50 +0100 Subject: [PATCH] Setup scope and run app --- src/main/java/com/epita/guereza/Main.java | 79 ++++++++++++++++++----- 1 file changed, 64 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/epita/guereza/Main.java b/src/main/java/com/epita/guereza/Main.java index 9825e0a..4f71dcb 100644 --- a/src/main/java/com/epita/guereza/Main.java +++ b/src/main/java/com/epita/guereza/Main.java @@ -6,43 +6,92 @@ import com.epita.domain.Indexer; import com.epita.eventbus.EventBusClient; import com.epita.eventbus.NettyEventBusClient; -import com.epita.eventbus.NettyServer; +import com.epita.guereza.eventsourcing.EventStore; import com.epita.guereza.service.CrawlerService; import com.epita.guereza.service.indexer.IndexerService; import com.epita.winter.Scope; import com.epita.winter.provider.LazySingleton; import com.epita.winter.provider.Prototype; import com.epita.winter.provider.Singleton; +import com.sun.security.ntlm.Server; import java.util.Map; import java.util.function.Function; +import static java.lang.System.exit; + public class Main { private static final int NETTY_PORT = 8000; public static void main(String[] args) { - final Crawler crawler = new CrawlerService(); - final Indexer indexer = new IndexerService(); + if (args.length != 1) { + System.out.println("usage: ./bin [ crawler | indexer | store | server"); + exit(1); + } + + final String module = args[0]; + final Scope scope = createScope(); + + switch (module) { + case "crawler": + runCrawler(scope); + break; + case "indexer": + runIndexer(scope); + break; + case "store": + runStore(scope); + break; + case "server": + runServer(); + break; + } + } + + private static Scope createScope() { + return new Scope() + .register(new Singleton<>(Crawler.class, new CrawlerService())) + .register(new Singleton<>(Indexer.class, new IndexerService())) + .register(new Singleton<>(EventBusClient.class, new NettyEventBusClient())); + } + + private static void runCrawler(Scope scope) { + final Function newCrawlerApp = (s) -> new CrawlerApp( + s.instanceOf(EventBusClient.class), s.instanceOf(Crawler.class)); - final Function newEventBus = (s) -> new NettyEventBusClient(); - final Function newCrawlerApp = (s) -> new CrawlerApp(s.instanceOf(EventBusClient.class), s.instanceOf(Crawler.class)); - final Function newIndexerApp = (s) -> new IndexerApp(s.instanceOf(EventBusClient.class), s.instanceOf(Indexer.class), - s.instanceOf(Crawler.class)); + scope.scope() + .register(new Prototype<>(App.class, newCrawlerApp)) + .block(Main::runApp); + } + + private static void runIndexer(Scope scope) { + final Function newIndexerApp = (s) -> new IndexerApp( + s.instanceOf(EventBusClient.class), s.instanceOf(Indexer.class), s.instanceOf(Crawler.class)); + + scope.scope() + .register(new Prototype<>(App.class, newIndexerApp)) + .block(Main::runApp); + } + + private static void runStore(Scope scope) { final Function newUrlStore = (s) -> new UrlStore(s.instanceOf(EventBusClient.class)); + final Function newEventStoreApp = (s) -> new EventStoreApp( + s.instanceOf(EventBusClient.class), s.instanceOf(EventStore.class)); - new Scope() - .register(new Singleton<>(Crawler.class, crawler)) - .register(new Singleton<>(Indexer.class, indexer)) + scope.scope() + .register(new Singleton<>(EventStore.class, new EventStore())) .register(new LazySingleton<>(UrlStore.class, newUrlStore)) - .register(new Prototype<>(EventBusClient.class, newEventBus)) - .register(new Prototype<>(CrawlerApp.class, newCrawlerApp)) - .register(new Prototype<>(IndexerApp.class, newIndexerApp)) + .register(new Prototype<>(App.class, newEventStoreApp)) .block(Main::runApp); } private static void runApp(Scope scope) { - CrawlerApp crawlerApp = scope.instanceOf(CrawlerApp.class); - crawlerApp.run(); + App app = scope.instanceOf(App.class); + app.run(); + } + + private static void runServer() { + new ServerApp(NETTY_PORT).run(); } private static void testSearch(final Index index, final String query) {