Skip to content
This repository has been archived by the owner on Jun 2, 2019. It is now read-only.

Commit

Permalink
Setup scope and run app
Browse files Browse the repository at this point in the history
  • Loading branch information
dethi committed Dec 9, 2017
1 parent 982897b commit bf7a290
Showing 1 changed file with 64 additions and 15 deletions.
79 changes: 64 additions & 15 deletions src/main/java/com/epita/guereza/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Scope, App> newCrawlerApp = (s) -> new CrawlerApp(
s.instanceOf(EventBusClient.class), s.instanceOf(Crawler.class));

final Function<Scope, EventBusClient> newEventBus = (s) -> new NettyEventBusClient();
final Function<Scope, CrawlerApp> newCrawlerApp = (s) -> new CrawlerApp(s.instanceOf(EventBusClient.class), s.instanceOf(Crawler.class));
final Function<Scope, IndexerApp> 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<Scope, App> 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<Scope, UrlStore> newUrlStore = (s) -> new UrlStore(s.instanceOf(EventBusClient.class));
final Function<Scope, App> 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) {
Expand Down

0 comments on commit bf7a290

Please sign in to comment.