package de.komoot.photon.importer;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.ParameterException;
import de.komoot.photon.importer.elasticsearch.Importer;
import de.komoot.photon.importer.elasticsearch.Searcher;
import de.komoot.photon.importer.elasticsearch.Server;
import de.komoot.photon.importer.json.JsonDumper;
import de.komoot.photon.importer.nominatim.NominatimConnector;
import de.komoot.photon.importer.nominatim.NominatimUpdater;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.client.Client;
import spark.Request;
import spark.Response;
import spark.Route;
import java.io.FileNotFoundException;
import static spark.Spark.*;
@Slf4j
public class App {
public static void main(String[] rawArgs) {
// parse command line arguments
CommandLineArgs args = new CommandLineArgs();
final JCommander jCommander = new JCommander(args);
try {
jCommander.parse(rawArgs);
} catch(ParameterException e) {
log.warn("could not start photon: " + e.getMessage());
jCommander.usage();
return;
}
// show help
if(args.isUsage()) {
jCommander.usage();
return;
}
if(args.getJsonDump() != null) {
try {
final String filename = args.getJsonDump();
final JsonDumper jsonDumper = new JsonDumper(filename);
NominatimConnector nominatimConnector = new NominatimConnector(args.getHost(), args.getPort(), args.getDatabase(), args.getUser(), args.getPassword());
nominatimConnector.setImporter(jsonDumper);
nominatimConnector.readEntireDatabase();
log.info("json dump was created: " + filename);
return;
} catch(FileNotFoundException e) {
log.error("cannot create dump", e);
}
}
final Server esServer = new Server(args.getCluster(), args.getDataDirectory());
esServer.start();
Client esNodeClient = esServer.getClient();
if(args.isDeleteIndex()) {
esServer.recreateIndex();
log.info("deleted photon index and created an empty new one.");
return;
}
if(args.isNominatimImport()) {
esServer.recreateIndex(); // dump previous data
Importer importer = new Importer(esNodeClient);
NominatimConnector nominatimConnector = new NominatimConnector(args.getHost(), args.getPort(), args.getDatabase(), args.getUser(), args.getPassword());
nominatimConnector.setImporter(importer);
nominatimConnector.readEntireDatabase();
log.info("imported data from nominatim to photon.");
return;
}
final NominatimUpdater nominatimUpdater = new NominatimUpdater(args.getHost(), args.getPort(), args.getDatabase(), args.getUser(), args.getPassword());
de.komoot.photon.importer.Updater updater = new de.komoot.photon.importer.elasticsearch.Updater(esNodeClient);
nominatimUpdater.setUpdater(updater);
startApi(args, esNodeClient, nominatimUpdater);
}
private static void startApi(CommandLineArgs args, Client esNodeClient, final NominatimUpdater nominatimUpdater) {
setPort(args.getListenPort());
setIpAddress(args.getListenIp());
get(new Route("/nominatim-update") {
@Override
public Object handle(Request request, Response response) {
Thread nominatimUpdaterThread = new Thread() {
@Override
public void run() {
nominatimUpdater.update();
}
};
nominatimUpdaterThread.start();
return "nominatim update started (more information in console output) ...";
}
});
final Searcher searcher = new Searcher(esNodeClient);
get(new RequestHandler("api", searcher));
get(new RequestHandler("api/", searcher));
}
}