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

Add cut-down route object database #398

Merged
merged 13 commits into from
Apr 25, 2019
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright (C) 2013-2019 Expedia Inc.

Licensed 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.hotels.styx.api.configuration;

import java.util.Optional;

/**
*
* A repository for storing named objects.
*
* @param <T> Stored object type
*/
public interface ObjectStore<T> {

/**
* Gets an object with given key.
*
* @param key
* @return
*/
Optional<T> get(String key);

/**
* Inserts an object against key.
*
* If a key already exists, the object registered is replaced.
*
* @param key
* @param payload
*/
void insert(String key, T payload);

}
9 changes: 9 additions & 0 deletions components/proxy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/com.natpryce/hamkrest -->
<dependency>
<groupId>com.natpryce</groupId>
<artifactId>hamkrest</artifactId>
<version>1.7.0.0</version>
<scope>test</scope>
</dependency>


</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright (C) 2013-2019 Expedia Inc.

Licensed 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.hotels.styx;

import com.google.common.collect.ImmutableList;
import com.hotels.styx.api.HttpInterceptor;
import com.hotels.styx.proxy.interceptors.ConfigurationContextResolverInterceptor;
import com.hotels.styx.proxy.interceptors.HopByHopHeadersRemovingInterceptor;
import com.hotels.styx.proxy.interceptors.HttpMessageLoggingInterceptor;
import com.hotels.styx.proxy.interceptors.RequestEnrichingInterceptor;
import com.hotels.styx.proxy.interceptors.TcpTunnelRequestRejector;
import com.hotels.styx.proxy.interceptors.UnexpectedRequestContentLengthRemover;
import com.hotels.styx.proxy.interceptors.ViaHeaderAppendingInterceptor;

import java.util.List;

import static com.hotels.styx.api.configuration.ConfigurationContextResolver.EMPTY_CONFIGURATION_CONTEXT_RESOLVER;

/**
* Provides a list of interceptors that are required by the Styx HTTP pipeline for core functionality.
*/
final class BuiltInInterceptors {
private BuiltInInterceptors() {
}

static List<HttpInterceptor> internalStyxInterceptors(StyxConfig config) {
ImmutableList.Builder<HttpInterceptor> builder = ImmutableList.builder();

boolean loggingEnabled = config.get("request-logging.inbound.enabled", Boolean.class)
.orElse(false);

boolean longFormatEnabled = config.get("request-logging.inbound.longFormat", Boolean.class)
.orElse(false);

if (loggingEnabled) {
builder.add(new HttpMessageLoggingInterceptor(longFormatEnabled));
}

builder.add(new TcpTunnelRequestRejector())
.add(new ConfigurationContextResolverInterceptor(EMPTY_CONFIGURATION_CONTEXT_RESOLVER))
.add(new UnexpectedRequestContentLengthRemover())
.add(new ViaHeaderAppendingInterceptor())
.add(new HopByHopHeadersRemovingInterceptor())
.add(new RequestEnrichingInterceptor(config.styxHeaderConfig()));

return builder.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2013-2018 Expedia Inc.
Copyright (C) 2013-2019 Expedia Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -146,7 +146,8 @@ final class ServerConfigSchema {
optional("httpPipeline", object(opaque())),
optional("logFormat", string()),
optional("userDefined", object(opaque())),
optional("requestTracking", bool())
optional("requestTracking", bool()),
optional("routingObjects", object(opaque()))
))
.build();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
Copyright (C) 2013-2019 Expedia Inc.

Licensed 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.hotels.styx;

import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.collect.ImmutableList;
import com.hotels.styx.api.HttpHandler;
import com.hotels.styx.api.extension.service.BackendService;
import com.hotels.styx.api.extension.service.spi.Registry;
import com.hotels.styx.api.extension.service.spi.StyxService;
import com.hotels.styx.proxy.plugin.NamedPlugin;
import com.hotels.styx.routing.HttpPipelineFactory;
import com.hotels.styx.routing.RoutingObjectRecord;
import com.hotels.styx.routing.StaticPipelineFactory;
import com.hotels.styx.routing.config.RoutingObjectConfiguration;
import com.hotels.styx.routing.config.RoutingObjectFactory;
import com.hotels.styx.routing.db.StyxObjectStore;
import com.hotels.styx.routing.handlers.HttpInterceptorPipeline;
import com.hotels.styx.startup.PipelineFactory;
import com.hotels.styx.startup.StyxServerComponents;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.hotels.styx.BuiltInInterceptors.internalStyxInterceptors;
import static com.hotels.styx.routing.config.RoutingConfigParser.toRoutingConfigNode;
import static java.util.Objects.requireNonNull;

/**
* Produces the pipeline for the Styx proxy server.
*/
public final class StyxPipelineFactory implements PipelineFactory {

private final StyxObjectStore<RoutingObjectRecord> routeDb;
private final RoutingObjectFactory routingObjectFactory;
private final Environment environment;
private final Map<String, StyxService> services;
private final List<NamedPlugin> plugins;

public StyxPipelineFactory(
StyxObjectStore<RoutingObjectRecord> routeDb,
RoutingObjectFactory routingObjectFactory,
Environment environment,
Map<String, StyxService> services,
List<NamedPlugin> plugins) {
this.routeDb = requireNonNull(routeDb);
this.routingObjectFactory = requireNonNull(routingObjectFactory);
this.environment = requireNonNull(environment);
this.services = requireNonNull(services);
this.plugins = requireNonNull(plugins);
}

@Override
public HttpHandler create(StyxServerComponents config) {
boolean requestTracking = environment.configuration().get("requestTracking", Boolean.class).orElse(false);

return new HttpInterceptorPipeline(
internalStyxInterceptors(environment.styxConfig()),
configuredPipeline(routingObjectFactory),
requestTracking);
}

private HttpHandler configuredPipeline(RoutingObjectFactory routingObjectFactory) {
boolean requestTracking = environment.configuration().get("requestTracking", Boolean.class).orElse(false);

Optional<JsonNode> rootHandlerNode = environment.configuration().get("httpPipeline", JsonNode.class);

HttpPipelineFactory pipelineBuilder = rootHandlerNode
.map(jsonNode -> {
RoutingObjectConfiguration node = toRoutingConfigNode(jsonNode);
return (HttpPipelineFactory) () -> routingObjectFactory.build(ImmutableList.of("httpPipeline"), node);
})
.orElseGet(() -> {
Registry<BackendService> backendServicesRegistry = (Registry<BackendService>) services.get("backendServiceRegistry");
return new StaticPipelineFactory(environment, backendServicesRegistry, plugins, requestTracking);
});

return pipelineBuilder.build();
}
}
16 changes: 8 additions & 8 deletions components/proxy/src/main/java/com/hotels/styx/StyxServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.hotels.styx.infrastructure.configuration.yaml.YamlConfiguration;
import com.hotels.styx.server.HttpServer;
import com.hotels.styx.startup.ProxyServerSetUp;
import com.hotels.styx.startup.StyxPipelineFactory;
import com.hotels.styx.startup.StyxServerComponents;
import io.netty.util.ResourceLeakDetector;
import org.slf4j.Logger;
Expand All @@ -39,7 +38,6 @@
import java.io.Reader;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Map;

import static com.hotels.styx.ServerConfigSchema.validateServerConfiguration;
import static com.hotels.styx.infrastructure.configuration.ConfigurationSource.configSource;
Expand Down Expand Up @@ -146,11 +144,13 @@ public StyxServer(StyxServerComponents components, Stopwatch stopwatch) {

registerCoreMetrics(components.environment().buildInfo(), components.environment().metricRegistry());

Map<String, StyxService> servicesFromConfig = components.services();

ProxyServerSetUp proxyServerSetUp = new ProxyServerSetUp(new StyxPipelineFactory());

components.plugins().forEach(plugin -> components.environment().configStore().set("plugins." + plugin.name(), plugin));
ProxyServerSetUp proxyServerSetUp = new ProxyServerSetUp(
new StyxPipelineFactory(
components.routeDatabase(),
components.routingObjectFactory(),
components.environment(),
components.services(),
components.plugins()));

this.proxyServer = proxyServerSetUp.createProxyServer(components);
this.adminServer = createAdminServer(components);
Expand All @@ -159,7 +159,7 @@ public StyxServer(StyxServerComponents components, Stopwatch stopwatch) {
{
add(proxyServer);
add(adminServer);
servicesFromConfig.values().stream()
components.services().values().stream()
.map(StyxServer::toGuavaService)
.forEach(this::add);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2013-2018 Expedia Inc.
Copyright (C) 2013-2019 Expedia Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -36,7 +36,7 @@ public RouteHandlerAdapter(HttpRouter router) {
@Override
public Eventual<LiveHttpResponse> handle(LiveHttpRequest request, HttpInterceptor.Context context) {
return router.route(request, context)
.map(pipeline -> pipeline.handle(request, context))
.map(handler -> handler.handle(request, context))
.orElseGet(() -> Eventual.error(new NoServiceConfiguredException(request.path())));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2013-2018 Expedia Inc.
Copyright (C) 2013-2019 Expedia Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -15,8 +15,11 @@
*/
package com.hotels.styx.routing.config;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.hotels.styx.api.HttpInterceptor;
import com.hotels.styx.routing.interceptors.RewriteInterceptor;

import java.util.Map;

Expand All @@ -31,18 +34,26 @@
*
*/
public class BuiltinInterceptorsFactory {
private final Map<String, HttpInterceptorFactory> builders;
public static final ImmutableMap<String, HttpInterceptorFactory> INTERCEPTOR_FACTORIES =
ImmutableMap.of("Rewrite", new RewriteInterceptor.Factory());

public BuiltinInterceptorsFactory(Map<String, HttpInterceptorFactory> builders) {
this.builders = requireNonNull(builders);
private final Map<String, HttpInterceptorFactory> interceptorFactories;

public BuiltinInterceptorsFactory() {
this(INTERCEPTOR_FACTORIES);
}

@VisibleForTesting
public BuiltinInterceptorsFactory(Map<String, HttpInterceptorFactory> interceptorFactories) {
this.interceptorFactories = requireNonNull(interceptorFactories);
}

public HttpInterceptor build(RouteHandlerConfig configBlock) {
if (configBlock instanceof RouteHandlerDefinition) {
RouteHandlerDefinition block = (RouteHandlerDefinition) configBlock;
public HttpInterceptor build(RoutingObjectConfiguration configBlock) {
if (configBlock instanceof RoutingObjectDefinition) {
RoutingObjectDefinition block = (RoutingObjectDefinition) configBlock;
String type = block.type();

HttpInterceptorFactory constructor = builders.get(type);
HttpInterceptorFactory constructor = interceptorFactories.get(type);
Preconditions.checkArgument(constructor != null, format("Unknown handler type '%s'", type));

return constructor.build(block);
Expand Down
Loading