This question was asked elsewhere and I’ll answer it here for all:
There are different ways to achieve this
- If you want at some point that your changes should be merged into the GraphHopper mainline, then just create a separate branch and start GraphHopper Web via GHServer
- If you want to keep certain things separated but reuse the complete GraphHopper JSON API etc then use GHServer in your custom server class and start it with customized settings or guice modules. See attached **
- Or use the Java API to integrate GraphHopper core directly in your Java application as explained in the docs
For all these procedures you can use the config.properties to modify certain aspects of GraphHopper, the command line arguments, the GraphHopper Java API or a mixture of all. Where the command line arguments overwrite the config.properties and if graphHopper.init is called as first method then the Java API overwrites everything else.
The command line arguments and properties in config.properties are identically named e.g. the following command line arguments I hand over to GHServer and set/overwrite certain properties to start the area monaco at port 8989:
jetty.port=8989 config=../config.properties osmreader.osm=../core/files/monaco.osm.gz
**
public class MyServer {
// execute via
// java -jar target/your-project-0.1-jar-with-dependencies.jar osmreader.osm=berlin.pbf graph.location=/somewhere/graph
public static void main(String[] args) throws Exception {
CmdArgs tmpArgs = CmdArgs.readFromConfigAndMerge(CmdArgs.read(args), "config", "graphhopper.config");
System.out.println(tmpArgs.toString());
new MyServer(tmpArgs).start();
}
private final Logger logger = LoggerFactory.getLogger(getClass());
private final CmdArgs cmdArgs;
private GHServer server;
public MyServer(CmdArgs args) {
this.cmdArgs = args;
}
/**
* Starts 'pure' GraphHopper server with the specified injector
*/
public void start(Injector injector) throws Exception {
server = new GHServer(cmdArgs);
server.start(injector);
logger.info("Memory utilization:" + Helper.getMemInfo() + ", " + cmdArgs.get("graph.flagEncoders", ""));
}
/**
* Starts GraphHopper server with routing and custom module features.
*/
public void start() throws Exception {
Injector injector = Guice.createInjector(createModule());
start(injector);
}
protected Module createModule() {
return new AbstractModule() {
@Override
protected void configure() {
binder().requireExplicitBindings();
install(new MyDefaultGuiceModule(cmdArgs));
install(new MyServletGuiceModule(cmdArgs));
bind(GuiceFilter.class);
}
};
}
public void stop() {
if (server != null) {
server.stop();
}
}
}