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

Decouple configuration parsing #109

Merged
merged 24 commits into from
Mar 16, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Move old YamlConfig class now that it is just a wrapper
  • Loading branch information
kvosper committed Mar 14, 2018
commit 8dfbeabb1c37878a652cbb34e1ee53ae8f3df3bf
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import com.google.common.annotations.VisibleForTesting;
import com.hotels.styx.infrastructure.configuration.ExtensibleConfiguration.PlaceholderResolutionResult;
import com.hotels.styx.infrastructure.configuration.yaml.YamlConfig;
import org.slf4j.Logger;

import java.util.Map;
Expand Down Expand Up @@ -86,7 +85,7 @@ private C parent(String includePath) {
}

private static ConfigurationProvider includeProvider(String includePath) {
getLogger(YamlConfig.class).info("Including config file: path={}", sanitise(includePath));
LOGGER.info("Including config file: path={}", sanitise(includePath));
return ConfigurationProvider.from(newResource(includePath));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,9 @@
import static com.hotels.styx.infrastructure.configuration.yaml.YamlConfigurationFormat.YAML;
import static java.util.Collections.emptyMap;

/**
* Yaml-based configuration object.
*/
public class YamlConfig implements Configuration {
private final YamlConfiguration config;

/**
* Constructs an instance by parsing YAML from a string.
*
* @param yaml a YAML string
*/
public YamlConfig(String yaml) {
this(yaml, emptyMap());
}
Expand All @@ -50,22 +42,10 @@ public YamlConfig(String yaml, Map<String, String> overrides) {
.parse(ConfigurationProvider.from(yaml));
}

/**
* Constructs an instance by parsing YAML from a resource.
*
* @param resource resource
*/
public YamlConfig(Resource resource) {
this(resource, emptyMap());
}

/**
* Constructs an instance by parsing YAML from a resource. Parsed properties can be overridden by
* supplying alternative values in the overrides map.
*
* @param resource resource
* @param overrides overrides
*/
public YamlConfig(Resource resource, Map overrides) {
this.config = new ConfigurationParser.Builder<YamlConfiguration>()
.format(YAML)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2013-2017 Expedia Inc.
* Copyright (C) 2013-2018 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 @@ -18,9 +18,12 @@

import com.hotels.styx.StartupConfig;
import com.hotels.styx.StyxConfig;
import com.hotels.styx.api.Resource;
import com.hotels.styx.api.client.Origin;
import com.hotels.styx.client.applications.BackendServices;
import com.hotels.styx.infrastructure.configuration.yaml.YamlConfig;
import com.hotels.styx.infrastructure.configuration.ConfigurationParser;
import com.hotels.styx.infrastructure.configuration.ConfigurationProvider;
import com.hotels.styx.infrastructure.configuration.yaml.YamlConfiguration;
import com.hotels.styx.server.HttpConnectorConfig;
import com.hotels.styx.server.HttpServer;
import com.hotels.styx.server.ServerEventLoopFactory;
Expand All @@ -31,9 +34,11 @@
import org.slf4j.Logger;

import java.util.List;
import java.util.Properties;

import static com.hotels.styx.StartupConfig.newStartupConfigBuilder;
import static com.hotels.styx.applications.yaml.YamlApplicationsProvider.loadApplicationsFrom;
import static com.hotels.styx.infrastructure.configuration.yaml.YamlConfigurationFormat.YAML;
import static com.hotels.styx.server.netty.eventloop.ServerEventLoopFactories.memoize;
import static java.util.stream.Collectors.toList;
import static java.util.stream.StreamSupport.stream;
Expand Down Expand Up @@ -78,7 +83,7 @@ public static void main(String[] args) {
.build();

if (args.length < 4) {
YamlConfig yamlConfig = new YamlConfig(startupConfig.configFileLocation(), System.getProperties());
YamlConfiguration yamlConfig = config(startupConfig.configFileLocation(), System.getProperties());

StyxConfig config = new StyxConfig(startupConfig, yamlConfig);

Expand All @@ -91,6 +96,14 @@ public static void main(String[] args) {
}
}

private static YamlConfiguration config(Resource resource, Properties properties) {
return new ConfigurationParser.Builder<YamlConfiguration>()
.overrides(properties)
.format(YAML)
.build()
.parse(ConfigurationProvider.from(resource));
}

public void run() {
originsServers.forEach(server -> server.startAsync().awaitRunning());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright (C) 2013-2018 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.config;

import com.hotels.styx.infrastructure.configuration.ConfigurationParser;
import com.hotels.styx.infrastructure.configuration.ConfigurationProvider;
import com.hotels.styx.infrastructure.configuration.yaml.YamlConfiguration;

import java.util.Map;

import static com.hotels.styx.infrastructure.configuration.yaml.YamlConfigurationFormat.YAML;
import static java.util.Collections.emptyMap;

public final class Config {
private Config() {
}

public static YamlConfiguration config(String string) {
return parser(emptyMap())
.parse(ConfigurationProvider.from(string));
}

private static ConfigurationParser<YamlConfiguration> parser(Map overrides) {
return new ConfigurationParser.Builder<YamlConfiguration>()
.overrides(overrides)
.format(YAML)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import com.hotels.styx.api.configuration.Configuration.MapBackedConfiguration
import com.hotels.styx.api.plugins.spi.Plugin
import com.hotels.styx.api.service.spi.StyxService
import com.hotels.styx.api.{HttpHandler, HttpRequest, HttpResponse}
import com.hotels.styx.infrastructure.configuration.yaml.YamlConfig
import com.hotels.styx.config.Config
import com.hotels.styx.metrics.StyxMetrics
import com.hotels.styx.proxy.ProxyServerConfig
import com.hotels.styx.proxy.plugin.NamedPlugin
Expand Down Expand Up @@ -73,7 +73,7 @@ object StyxServerSupport {
}

def newStyxConfig(yaml: String, proxyServerConfigBuilder: ProxyServerConfig.Builder, adminServerConfigBuilder: AdminServerConfig.Builder) = {
new StyxConfig(new MapBackedConfiguration(new YamlConfig(yaml))
new StyxConfig(new MapBackedConfiguration(Config.config(yaml))
.set("proxy", proxyServerConfigBuilder.build())
.set("admin", adminServerConfigBuilder.build()))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import java.util

import com.hotels.styx.StyxServerSupport._
import com.hotels.styx.api.service.spi.StyxService
import com.hotels.styx.infrastructure.configuration.yaml.YamlConfig
import com.hotels.styx.config.Config
import com.hotels.styx.infrastructure.configuration.yaml.YamlConfiguration
import com.hotels.styx.proxy.ProxyServerConfig
import com.hotels.styx.proxy.plugin.NamedPlugin
import com.hotels.styx.support.ResourcePaths
Expand Down Expand Up @@ -151,7 +152,7 @@ case class StyxYamlConfig(yamlConfig: String,
) extends StyxBaseConfig {

override def startServer(backendsRegistry: StyxService): StyxServer = {
val config: YamlConfig = new YamlConfig(yamlConfig)
val config: YamlConfiguration = Config.config(yamlConfig)
val styxConfig = new com.hotels.styx.StyxConfig(yamlConfig)

val styxServer = new StyxServerBuilder(styxConfig)
Expand Down