com.graphhopper.http.DefaultModule Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of graphhopper-web Show documentation
Show all versions of graphhopper-web Show documentation
Example on how to use GraphHopper in a web-based application
/*
* Licensed to GraphHopper and Peter Karich under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* GraphHopper licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.graphhopper.http;
import com.google.inject.AbstractModule;
import com.google.inject.name.Names;
import com.graphhopper.GraphHopper;
import com.graphhopper.util.CmdArgs;
import com.graphhopper.util.TranslationMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Peter Karich
*/
public class DefaultModule extends AbstractModule
{
private final Logger logger = LoggerFactory.getLogger(getClass());
protected final CmdArgs args;
private GraphHopper graphHopper;
public DefaultModule( CmdArgs args )
{
this.args = CmdArgs.readFromConfigAndMerge(args, "config", "graphhopper.config");
}
public GraphHopper getGraphHopper()
{
if (graphHopper == null)
throw new IllegalStateException("createGraphHopper not called");
return graphHopper;
}
/**
* @return an initialized GraphHopper instance
*/
protected GraphHopper createGraphHopper( CmdArgs args )
{
GraphHopper tmp = new GraphHopper().forServer().init(args);
tmp.importOrLoad();
logger.info("loaded graph at:" + tmp.getGraphHopperLocation()
+ ", source:" + tmp.getOSMFile()
+ ", flagEncoders:" + tmp.getEncodingManager()
+ ", class:" + tmp.getGraphHopperStorage().toDetailsString());
return tmp;
}
@Override
protected void configure()
{
try
{
graphHopper = createGraphHopper(args);
bind(GraphHopper.class).toInstance(graphHopper);
bind(TranslationMap.class).toInstance(graphHopper.getTranslationMap());
long timeout = args.getLong("web.timeout", 3000);
bind(Long.class).annotatedWith(Names.named("timeout")).toInstance(timeout);
boolean jsonpAllowed = args.getBool("web.jsonpAllowed", false);
if (!jsonpAllowed)
logger.info("jsonp disabled");
bind(Boolean.class).annotatedWith(Names.named("jsonpAllowed")).toInstance(jsonpAllowed);
bind(RouteSerializer.class).toInstance(new SimpleRouteSerializer(graphHopper.getGraphHopperStorage().getBounds()));
} catch (Exception ex)
{
throw new IllegalStateException("Couldn't load graph", ex);
}
}
}