Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server refactoring #705

Merged
merged 8 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .run/basemap-serve.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="basemap-serve" type="Application" factoryName="Application">
<option name="MAIN_CLASS_NAME" value="org.apache.baremaps.cli.Baremaps" />
<module name="baremaps-cli" />
<option name="PROGRAM_PARAMETERS" value="map serve --tileset tileset.js --style style.js" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/basemap" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="org.apache.baremaps.server.ogcapi.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
</component>
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
import io.servicetalk.http.router.jersey.HttpJerseyRouterBuilder;
import java.nio.file.Path;
import java.util.concurrent.Callable;
import org.apache.baremaps.server.ClassPathResource;
import org.apache.baremaps.server.CorsFilter;
import org.apache.baremaps.server.GeocoderResources;
import org.apache.baremaps.server.GeocoderResource;
import org.apache.lucene.search.SearcherFactory;
import org.apache.lucene.search.SearcherManager;
import org.apache.lucene.store.MMapDirectory;
Expand Down Expand Up @@ -51,11 +52,16 @@ public Integer call() throws Exception {
try (
var directory = MMapDirectory.open(indexDirectory);
var searcherManager = new SearcherManager(directory, new SearcherFactory())) {
// Configure the application
var application = new ResourceConfig().register(CorsFilter.class)
.register(GeocoderResources.class).register(new AbstractBinder() {

var application = new ResourceConfig()
.register(CorsFilter.class)
.register(GeocoderResource.class)
.register(ClassPathResource.class)
.register(new AbstractBinder() {
@Override
protected void configure() {
bind("geocoder").to(String.class).named("directory");
bind("index.html").to(String.class).named("index");
bind(searcherManager).to(SearcherManager.class).named("searcherManager");
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
import java.util.concurrent.Callable;
import javax.sql.DataSource;
import org.apache.baremaps.iploc.IpLocRepository;
import org.apache.baremaps.server.ClassPathResource;
import org.apache.baremaps.server.CorsFilter;
import org.apache.baremaps.server.IpLocResources;
import org.apache.baremaps.server.IpLocResource;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.server.ResourceConfig;
import org.slf4j.Logger;
Expand Down Expand Up @@ -56,16 +57,19 @@ public Integer call() throws Exception {
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
// config.setReadOnly(true);
DataSource dataSource = new HikariDataSource(config);

IpLocRepository ipLocRepository = new IpLocRepository(dataSource);

// Configure the application
var application = new ResourceConfig().register(CorsFilter.class).register(IpLocResources.class)
var application = new ResourceConfig()
.register(CorsFilter.class)
.register(IpLocResource.class)
.register(ClassPathResource.class)
.register(new AbstractBinder() {
@Override
protected void configure() {
bind("iploc").to(String.class).named("directory");
bind("index.html").to(String.class).named("index");
bind(ipLocRepository).to(IpLocRepository.class).named("iplocRepository");
}
});
Expand Down
104 changes: 73 additions & 31 deletions baremaps-cli/src/main/java/org/apache/baremaps/cli/map/Dev.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,25 @@

package org.apache.baremaps.cli.map;

import static io.servicetalk.data.jackson.jersey.ServiceTalkJacksonSerializerFeature.contextResolverFor;
import static io.servicetalk.data.jackson.jersey.ServiceTalkJacksonSerializerFeature.newContextResolver;
import static org.apache.baremaps.utils.ObjectMapperUtils.objectMapper;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.servicetalk.http.netty.HttpServers;
import io.servicetalk.http.router.jersey.HttpJerseyRouterBuilder;
import java.io.IOException;
import java.nio.file.Path;
import java.util.concurrent.Callable;
import javax.sql.DataSource;
import java.util.function.Supplier;
import org.apache.baremaps.cli.Options;
import org.apache.baremaps.config.ConfigReader;
import org.apache.baremaps.postgres.PostgresUtils;
import org.apache.baremaps.server.CorsFilter;
import org.apache.baremaps.server.DevResources;
import org.apache.baremaps.server.*;
import org.apache.baremaps.tilestore.TileStore;
import org.apache.baremaps.tilestore.postgres.PostgresTileStore;
import org.apache.baremaps.vectortile.style.Style;
import org.apache.baremaps.vectortile.tilejson.TileJSON;
import org.apache.baremaps.vectortile.tileset.Tileset;
import org.glassfish.hk2.api.TypeLiteral;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.server.ResourceConfig;
import org.slf4j.Logger;
Expand All @@ -42,16 +46,17 @@ public class Dev implements Callable<Integer> {

@Mixin
private Options options;

@Option(names = {"--cache"}, paramLabel = "CACHE", description = "The caffeine cache directive.")
private String cache = "";

@Option(names = {"--tileset"}, paramLabel = "TILESET", description = "The tileset file.",
required = true)
private Path tileset;
private Path tilesetPath;

@Option(names = {"--style"}, paramLabel = "STYLE", description = "The style file.",
required = true)
private Path style;
private Path stylePath;

@Option(names = {"--host"}, paramLabel = "HOST", description = "The host of the server.")
private String host = "localhost";
Expand All @@ -62,32 +67,69 @@ public class Dev implements Callable<Integer> {
@Override
public Integer call() throws Exception {
var configReader = new ConfigReader();
// Configure serialization
var objectMapper = objectMapper();
var tileset = objectMapper.readValue(configReader.read(this.tilesetPath), Tileset.class);
var datasource = PostgresUtils.dataSource(tileset.getDatabase());

var tileStoreType = new TypeLiteral<Supplier<TileStore>>() {};
var tileStoreSupplier = (Supplier<TileStore>) () -> {
try {
var tilesetObject =
objectMapper.readValue(configReader.read(this.tilesetPath), Tileset.class);
return new PostgresTileStore(datasource, tilesetObject);
} catch (IOException e) {
throw new RuntimeException(e);
}
};

var styleSupplierType = new TypeLiteral<Supplier<Style>>() {};
var styleSupplier = (Supplier<Style>) () -> {
try {
var config = configReader.read(stylePath);
return objectMapper.readValue(config, Style.class);
} catch (IOException e) {
throw new RuntimeException(e);
}
};

var tileJSONSupplierType = new TypeLiteral<Supplier<TileJSON>>() {};
var tileJSONSupplier = (Supplier<TileJSON>) () -> {
try {
var config = configReader.read(tilesetPath);
return objectMapper.readValue(config, TileJSON.class);
} catch (IOException e) {
throw new RuntimeException(e);
}
};

var application = new ResourceConfig()
.register(CorsFilter.class)
.register(ChangeResource.class)
.register(TileResource.class)
.register(StyleResource.class)
.register(TilesetResource.class)
.register(ChangeResource.class)
.register(ClassPathResource.class)
.register(newContextResolver(objectMapper))
.register(new AbstractBinder() {
@Override
protected void configure() {
bind("assets").to(String.class).named("directory");
bind("viewer.html").to(String.class).named("index");
bind(tilesetPath).to(Path.class).named("tileset");
bind(stylePath).to(Path.class).named("style");
bind(tileStoreSupplier).to(tileStoreType);
bind(styleSupplier).to(styleSupplierType);
bind(tileJSONSupplier).to(tileJSONSupplierType);
}
});

var httpService = new HttpJerseyRouterBuilder().buildBlockingStreaming(application);
var serverContext = HttpServers.forPort(port).listenBlockingStreamingAndAwait(httpService);

logger.info("Listening on {}", serverContext.listenAddress());
serverContext.awaitShutdown();

var tileSet = objectMapper.readValue(configReader.read(tileset), Tileset.class);
var database = tileSet.getDatabase();

try (var dataSource = PostgresUtils.dataSource(database)) {
// Configure the application
var application = new ResourceConfig().register(CorsFilter.class).register(DevResources.class)
.register(contextResolverFor(objectMapper)).register(new AbstractBinder() {
@Override
protected void configure() {
bind("viewer").to(String.class).named("assets");
bind(tileset.toAbsolutePath()).to(Path.class).named("tileset");
bind(style.toAbsolutePath()).to(Path.class).named("style");
bind(dataSource).to(DataSource.class);
bind(objectMapper).to(ObjectMapper.class);
}
});

var httpService = new HttpJerseyRouterBuilder().buildBlockingStreaming(application);
var serverContext = HttpServers.forPort(port).listenBlockingStreamingAndAwait(httpService);

logger.info("Listening on {}", serverContext.listenAddress());
serverContext.awaitShutdown();
}
return 0;
}
}
46 changes: 32 additions & 14 deletions baremaps-cli/src/main/java/org/apache/baremaps/cli/map/Serve.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,26 @@

package org.apache.baremaps.cli.map;

import static io.servicetalk.data.jackson.jersey.ServiceTalkJacksonSerializerFeature.contextResolverFor;
import static io.servicetalk.data.jackson.jersey.ServiceTalkJacksonSerializerFeature.newContextResolver;
import static org.apache.baremaps.utils.ObjectMapperUtils.objectMapper;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.benmanes.caffeine.cache.CaffeineSpec;
import io.servicetalk.http.netty.HttpServers;
import io.servicetalk.http.router.jersey.HttpJerseyRouterBuilder;
import java.nio.file.Path;
import java.util.concurrent.Callable;
import java.util.function.Supplier;
import org.apache.baremaps.cli.Options;
import org.apache.baremaps.config.ConfigReader;
import org.apache.baremaps.postgres.PostgresUtils;
import org.apache.baremaps.server.CorsFilter;
import org.apache.baremaps.server.ServerResources;
import org.apache.baremaps.server.*;
import org.apache.baremaps.tilestore.TileCache;
import org.apache.baremaps.tilestore.TileStore;
import org.apache.baremaps.tilestore.postgres.PostgresTileStore;
import org.apache.baremaps.vectortile.style.Style;
import org.apache.baremaps.vectortile.tilejson.TileJSON;
import org.apache.baremaps.vectortile.tileset.Tileset;
import org.glassfish.hk2.api.TypeLiteral;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.server.ResourceConfig;
import org.slf4j.Logger;
Expand All @@ -51,11 +53,11 @@ public class Serve implements Callable<Integer> {

@Option(names = {"--tileset"}, paramLabel = "TILESET", description = "The tileset file.",
required = true)
private Path tileset;
private Path tilesetPath;

@Option(names = {"--style"}, paramLabel = "STYLE", description = "The style file.",
required = true)
private Path style;
private Path stylePath;

@Option(names = {"--host"}, paramLabel = "HOST", description = "The host of the server.")
private String host = "localhost";
Expand All @@ -67,23 +69,39 @@ public class Serve implements Callable<Integer> {
public Integer call() throws Exception {
var objectMapper = objectMapper();
var configReader = new ConfigReader();
var tileset = objectMapper.readValue(configReader.read(this.tileset), Tileset.class);
var tileset = objectMapper.readValue(configReader.read(this.tilesetPath), Tileset.class);
var caffeineSpec = CaffeineSpec.parse(cache);
var datasource = PostgresUtils.dataSource(tileset.getDatabase());

var tileStoreSupplierType = new TypeLiteral<Supplier<TileStore>>() {};
var tileStore = new PostgresTileStore(datasource, tileset);
var tileCache = new TileCache(tileStore, caffeineSpec);
var tileStoreSupplier = (Supplier<TileStore>) () -> tileCache;

var styleSupplierType = new TypeLiteral<Supplier<Style>>() {};
var style = objectMapper.readValue(configReader.read(stylePath), Style.class);
var styleSupplier = (Supplier<Style>) () -> style;

var tileJSONSupplierType = new TypeLiteral<Supplier<TileJSON>>() {};
var tileJSON = objectMapper.readValue(configReader.read(tilesetPath), TileJSON.class);
var tileJSONSupplier = (Supplier<TileJSON>) () -> tileJSON;

// Configure the application
var application =
new ResourceConfig().register(CorsFilter.class).register(ServerResources.class)
.register(contextResolverFor(objectMapper)).register(new AbstractBinder() {
new ResourceConfig()
.register(CorsFilter.class)
.register(TileResource.class)
.register(StyleResource.class)
.register(TilesetResource.class)
.register(ClassPathResource.class)
.register(newContextResolver(objectMapper))
.register(new AbstractBinder() {
@Override
protected void configure() {
bind(Serve.this.tileset).to(Path.class).named("tileset");
bind(style).to(Path.class).named("style");
bind(tileCache).to(TileStore.class);
bind(objectMapper).to(ObjectMapper.class);
bind("assets").to(String.class).named("directory");
bind("server.html").to(String.class).named("index");
bind(tileStoreSupplier).to(tileStoreSupplierType);
bind(styleSupplier).to(styleSupplierType);
bind(tileJSONSupplier).to(tileJSONSupplierType);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.ext.Provider;

/**
* A filter that sets the base URI of the request.
*/
@Provider
@PreMatching
@Singleton
Expand Down
Loading