Search in sources :

Example 86 with Config

use of io.helidon.config.Config in project helidon by oracle.

the class DeserializationExample method main.

/**
 * Executes the example.
 *
 * @param args arguments
 */
public static void main(String... args) {
    Config config = Config.create(ConfigSources.classpath("application.conf"));
    AppConfig appConfig = config.get("app").as(AppConfig.class).get();
    System.out.println(appConfig);
    assert appConfig.getGreeting().equals("Hello");
    assert appConfig.getPageSize() == 20;
    assert appConfig.getBasicRange().size() == 2;
    assert appConfig.getBasicRange().get(0) == -20;
    assert appConfig.getBasicRange().get(1) == 20;
}
Also used : Config(io.helidon.config.Config)

Example 87 with Config

use of io.helidon.config.Config in project helidon by oracle.

the class FactoryMethodExample method main.

/**
 * Executes the example.
 *
 * @param args arguments
 */
public static void main(String... args) {
    Config config = Config.create(ConfigSources.classpath("application.conf"));
    AppConfig appConfig = config.get("app").as(AppConfig.class).get();
    System.out.println(appConfig);
    assert appConfig.getGreeting().equals("Hello");
    assert appConfig.getPageSize() == 20;
    assert appConfig.getBasicRange().size() == 2;
    assert appConfig.getBasicRange().get(0) == -20;
    assert appConfig.getBasicRange().get(1) == 20;
}
Also used : Config(io.helidon.config.Config)

Example 88 with Config

use of io.helidon.config.Config in project helidon by oracle.

the class Main method corsSupportForGreeting.

private static CorsSupport corsSupportForGreeting(Config config) {
    // The default CorsSupport object (obtained using CorsSupport.create()) allows sharing for any HTTP method and with any
    // origin. Using CorsSupport.create(Config) with a missing config node yields a default CorsSupport, which might not be
    // what you want. This example warns if either expected config node is missing and then continues with the default.
    Config restrictiveConfig = config.get("restrictive-cors");
    if (!restrictiveConfig.exists()) {
        Logger.getLogger(Main.class.getName()).warning("Missing restrictive config; continuing with default CORS support");
    }
    CorsSupport.Builder corsBuilder = CorsSupport.builder();
    // Use possible overrides first.
    config.get("cors").ifExists(c -> {
        Logger.getLogger(Main.class.getName()).info("Using the override configuration");
        corsBuilder.mappedConfig(c);
    });
    corsBuilder.config(// restricted sharing for PUT, DELETE
    restrictiveConfig).addCrossOrigin(// open sharing for other methods
    CrossOriginConfig.create()).build();
    return corsBuilder.build();
}
Also used : Config(io.helidon.config.Config) CrossOriginConfig(io.helidon.webserver.cors.CrossOriginConfig) LogConfig(io.helidon.common.LogConfig) CorsSupport(io.helidon.webserver.cors.CorsSupport)

Example 89 with Config

use of io.helidon.config.Config in project helidon by oracle.

the class AsSupplierExample method run.

/**
 * Executes the example.
 */
public void run() {
    Config config = Config.create(file("conf/dev.yaml").optional().changeWatcher(FileSystemWatcher.create()), file("conf/config.yaml").optional().pollingStrategy(regular(Duration.ofSeconds(2))), classpath("default.yaml"));
    // greeting.get() always return up-to-date value
    final Supplier<String> greeting = config.get("app.greeting").asString().supplier();
    // name.get() always return up-to-date value
    final Supplier<String> name = config.get("app.name").asString().supplier();
    // first greeting
    printIfChanged(greeting.get() + " " + name.get() + ".");
    // use same Supplier instances to get up-to-date value
    executor.scheduleWithFixedDelay(() -> printIfChanged(greeting.get() + " " + name.get() + "."), // check every 1 second for changes
    0, 1, TimeUnit.SECONDS);
}
Also used : Config(io.helidon.config.Config)

Example 90 with Config

use of io.helidon.config.Config in project helidon by oracle.

the class WithSourcesExample method main.

/**
 * Executes the example.
 *
 * @param args arguments
 */
public static void main(String... args) {
    /*
           Creates a config source composed of following sources:
           - conf/dev.yaml - developer specific configuration, should not be placed in VCS;
           - conf/config.yaml - deployment dependent configuration, for example prod, stage, etc;
           - default.yaml - application default values, loaded form classpath;
           with a filter which convert values with keys ending with "level" to upper case
         */
    Config config = Config.builder(file("conf/dev.yaml").optional(), file("conf/config.yaml").optional(), classpath("default.yaml")).addFilter((key, stringValue) -> key.name().equals("level") ? stringValue.toUpperCase() : stringValue).build();
    // Environment type, from dev.yaml:
    ConfigValue<String> env = config.get("meta.env").asString();
    env.ifPresent(e -> System.out.println("Environment: " + e));
    assert env.get().equals("DEV");
    // Default value (default.yaml): Config Sources Example
    String appName = config.get("app.name").asString().get();
    System.out.println("Name: " + appName);
    assert appName.equals("Config Sources Example");
    // Page size, from config.yaml: 10
    int pageSize = config.get("app.page-size").asInt().get();
    System.out.println("Page size: " + pageSize);
    assert pageSize == 10;
    // Applied filter (uppercase logging level), from dev.yaml: finest -> FINEST
    String level = config.get("component.audit.logging.level").asString().get();
    System.out.println("Level: " + level);
    assert level.equals("FINE");
}
Also used : ConfigSources.file(io.helidon.config.ConfigSources.file) Config(io.helidon.config.Config) ConfigValue(io.helidon.config.ConfigValue) ConfigSources.classpath(io.helidon.config.ConfigSources.classpath) Config(io.helidon.config.Config)

Aggregations

Config (io.helidon.config.Config)329 Test (org.junit.jupiter.api.Test)169 LogConfig (io.helidon.common.LogConfig)56 WebServer (io.helidon.webserver.WebServer)54 Routing (io.helidon.webserver.Routing)51 BeforeAll (org.junit.jupiter.api.BeforeAll)24 Security (io.helidon.security.Security)20 HealthSupport (io.helidon.health.HealthSupport)18 Single (io.helidon.common.reactive.Single)17 MetricsSupport (io.helidon.metrics.MetricsSupport)16 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)16 ConfigSources (io.helidon.config.ConfigSources)15 JsonpSupport (io.helidon.media.jsonp.JsonpSupport)15 SecurityContext (io.helidon.security.SecurityContext)15 Optional (java.util.Optional)15 TimeUnit (java.util.concurrent.TimeUnit)15 WebSecurity (io.helidon.security.integration.webserver.WebSecurity)13 HealthChecks (io.helidon.health.checks.HealthChecks)12 WebClient (io.helidon.webclient.WebClient)12 GrpcRouting (io.helidon.grpc.server.GrpcRouting)11