Skip to content

Commit

Permalink
Update examples to not use io.helidon.common.config and to explicital…
Browse files Browse the repository at this point in the history
…ly call Config.global(config)
  • Loading branch information
barchetta committed Sep 22, 2023
1 parent 6097186 commit 2e6baf3
Show file tree
Hide file tree
Showing 30 changed files with 118 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public class GreetService implements HttpService {

private static final JsonBuilderFactory JSON_BF = Json.createBuilderFactory(Collections.emptyMap());

GreetService(Config config) {
GreetService() {
Config config = Config.global();
this.greeting = config.get("app.greeting").asString().orElse("Ciao");
}

Expand Down
13 changes: 6 additions & 7 deletions examples/cors/src/main/java/io/helidon/examples/cors/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ public static void main(final String[] args) {
// load logging configuration
LogConfig.configureRuntime();

// By default, this will pick up application.yaml from the classpath
// initialize global config from default configuration
Config config = Config.create();
Config.global(config);

// Get webserver config from the "server" section of application.yaml
WebServerConfig.Builder builder = WebServer.builder();
WebServer server = builder
.config(config.get("server"))
.routing(it -> routing(it, config))
.routing(Main::routing)
.build()
.start();

Expand All @@ -65,14 +66,12 @@ public static void main(final String[] args) {
* Setup routing.
*
* @param routing routing builder
* @param config configuration of this server
*/
static void routing(HttpRouting.Builder routing, Config config) {

GreetService greetService = new GreetService(config);
static void routing(HttpRouting.Builder routing) {
Config config = Config.global();

// Note: Add the CORS routing *before* registering the GreetService routing.
routing.register("/greet", corsSupportForGreeting(config), greetService)
routing.register("/greet", corsSupportForGreeting(config), new GreetService())
.addFeature(ObserveFeature.create());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class MainTest {

@SetUpServer
public static void setup(WebServerConfig.Builder server) {
server.routing(routing -> Main.routing(routing, Config.create()));
server.routing(routing -> Main.routing(routing));
}

@Order(1) // Make sure this runs before the greeting message changes so responses are deterministic.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public class GreetService implements HttpService {

private static final JsonBuilderFactory JSON_BF = Json.createBuilderFactory(Collections.emptyMap());

GreetService(Config config, Timer getTimer, Counter personalizedGetCounter) {
GreetService(Timer getTimer, Counter personalizedGetCounter) {
Config config = Config.global();
this.greeting = config.get("app.greeting").asString().orElse("Ciao");
this.getTimer = getTimer;
this.personalizedGetCounter = personalizedGetCounter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ static WebServer startServer() {
LogConfig.configureRuntime();

// By default, this will pick up application.yaml from the classpath
// and initialize global config
Config config = Config.create();
Config.global(config);

WebServer server = WebServer.builder()
.config(config.get("server"))
.routing(r -> setupRouting(r, config))
.routing(Main::setupRouting)
.build()
.start();

Expand All @@ -73,17 +75,19 @@ static WebServer startServer() {
* Setup routing.
*
* @param routing routing builder
* @param config config
*/
static void setupRouting(HttpRouting.Builder routing, Config config) {
static void setupRouting(HttpRouting.Builder routing) {

Config config = Config.global();

MicrometerFeature micrometerSupport = MicrometerFeature.create(config);
Counter personalizedGetCounter = micrometerSupport.registry()
.counter(PERSONALIZED_GETS_COUNTER_NAME);
Timer getTimer = Timer.builder(ALL_GETS_TIMER_NAME)
.publishPercentileHistogram()
.register(micrometerSupport.registry());

GreetService greetService = new GreetService(config, getTimer, personalizedGetCounter);
GreetService greetService = new GreetService(getTimer, personalizedGetCounter);

routing.register("/greet", greetService)
.addFeature(micrometerSupport);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public MainTest(Http1Client client) {

@SetUpServer
public static void setup(Builder builder) {
builder.routing(r -> Main.setupRouting(r, Config.create()));
builder.routing(Main::setupRouting);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public class GreetService implements HttpService {
private final Timer timerForGets;
private final Counter personalizedGreetingsCounter;

GreetService(Config config) {
GreetService() {
Config config = Config.global();
greeting.set(config.get("app.greeting").asString().orElse("Ciao"));

MeterRegistry meterRegistry = Metrics.globalRegistry();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ static void setup(WebServerConfig.Builder server) {

// By default, this will pick up application.yaml from the classpath
Config config = Config.create();
Config.global(config);

server.routing(r -> routing(r, config))
server.routing(Main::routing)
.config(config.get("server"));

}
Expand All @@ -70,12 +71,12 @@ static void setup(WebServerConfig.Builder server) {
* Setup routing.
*
* @param routing routing builder
* @param config configuration of this server
*/
static void routing(HttpRouting.Builder routing, Config config) {
static void routing(HttpRouting.Builder routing) {
Config config = Config.global();
Tracer tracer = TracerBuilder.create(config.get("tracing")).build();
routing.addFeature(ObserveFeature.create())
.addFeature(TracingFeature.create(tracer))
.register("/greet", new GreetService(config));
.register("/greet", new GreetService());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public MainTest(Http1Client client) {

@SetUpServer
public static void setup(WebServerConfig.Builder server) {
server.routing(it -> Main.routing(it, Config.create()));
Config.global(Config.create());
server.routing(Main::routing);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.Collections;
import java.util.concurrent.atomic.AtomicReference;

import io.helidon.common.config.Config;
import io.helidon.config.Config;
import io.helidon.http.Status;
import io.helidon.metrics.api.Counter;
import io.helidon.metrics.api.MeterRegistry;
Expand Down Expand Up @@ -63,7 +63,8 @@ public class GreetService implements HttpService {
private final Timer timerForGets;
private final Counter personalizedGreetingsCounter;

GreetService(Config config, MeterRegistry meterRegistry) {
GreetService(MeterRegistry meterRegistry) {
Config config = Config.global();
greeting.set(config.get("app.greeting").asString().orElse("Ciao"));
timerForGets = meterRegistry.getOrCreate(Timer.builder(TIMER_FOR_GETS));
personalizedGreetingsCounter = meterRegistry.getOrCreate(Counter.builder(COUNTER_FOR_PERSONALIZED_GREETINGS));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@

import java.util.regex.Pattern;

import io.helidon.common.config.Config;
import io.helidon.common.config.GlobalConfig;
import io.helidon.config.Config;
import io.helidon.logging.common.LogConfig;
import io.helidon.metrics.api.Meter;
import io.helidon.metrics.api.MeterRegistry;
Expand Down Expand Up @@ -67,7 +66,8 @@ static void setup(WebServerConfig.Builder server) {
LogConfig.configureRuntime();

// By default, this will pick up application.yaml from the classpath
Config config = GlobalConfig.config();
Config config = Config.create();
Config.global(config);

// Programmatically (not through config), tell the metrics feature to ignore the "gets" timer.
// To do so, create the scope config, then add it to the metrics config that ultimately
Expand Down Expand Up @@ -100,7 +100,7 @@ static void routing(HttpRouting.Builder routing, Config config, MetricsConfig.Bu
MetricsObserver metrics = MetricsObserver.builder()
.metricsConfig(metricsConfigBuilder)
.build();
GreetService greetService = new GreetService(config, meterRegistry);
GreetService greetService = new GreetService(meterRegistry);

routing.addFeature(ObserveFeature.just(metrics))
.register("/greet", greetService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import static org.hamcrest.Matchers.not;

@ServerTest
@Disabled
public class MainTest {

private static final JsonBuilderFactory JSON_BUILDER = Json.createBuilderFactory(Collections.emptyMap());
Expand Down Expand Up @@ -79,6 +78,7 @@ public void testHelloWorld() {
}

@Test
@Disabled // application metrics returns 404
public void testMetrics() {
try (Http1ClientResponse response = client.get("/greet").request()) {
assertThat(response.as(String.class), containsString("Hello World!"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public class GreetService implements HttpService {

private static final JsonBuilderFactory JSON = Json.createBuilderFactory(Collections.emptyMap());

GreetService(Config config) {
GreetService() {
Config config = Config.global();
greeting.set(config.get("app.greeting").asString().orElse("Ciao"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ static void setup(WebServerConfig.Builder server) {

// By default, this will pick up application.yaml from the classpath
Config config = Config.create();
Config.global(config);

server.routing(r -> routing(r, config))
server.routing(Main::routing)
.config(config.get("server"));

}
Expand All @@ -68,12 +69,11 @@ static void setup(WebServerConfig.Builder server) {
* @param routing routing builder
* @param config configuration of this server
*/
static void routing(HttpRouting.Builder routing, Config config) {
SimpleGreetService simpleGreetService = new SimpleGreetService(config);
GreetService greetService = new GreetService(config);
static void routing(HttpRouting.Builder routing) {
Config config = Config.global();
routing.addFeature(ObserveFeature.create())
.register(HttpStatusMetricService.create()) // no endpoint, just metrics updates
.register("/simple-greet", simpleGreetService)
.register("/greet", greetService);
.register("/simple-greet", new SimpleGreetService())
.register("/greet", new GreetService());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public class SimpleGreetService implements HttpService {

private final String greeting;

SimpleGreetService(Config config) {
SimpleGreetService() {
Config config = Config.global();
greeting = config.get("app.greeting").asString().orElse("Ciao");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import static org.junit.jupiter.api.Assertions.fail;

@ServerTest
@Disabled
public class StatusTest {

private final Counter[] STATUS_COUNTERS = new Counter[6];
Expand All @@ -55,8 +54,9 @@ public StatusTest(Http1Client client) {

@SetUpServer
public static void setup(WebServerConfig.Builder server) {
Config.global(Config.create());
server.routing(r -> {
Main.routing(r, Config.create());
Main.routing(r);
r.register("/status", new StatusService());
});
}
Expand All @@ -71,6 +71,7 @@ void findStatusMetrics() {
}

@Test
@Disabled
void checkStatusMetrics() throws InterruptedException {
checkAfterStatus(Status.create(171));
checkAfterStatus(Status.OK_200);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public class GreetService implements HttpService {

private final Counter personalizedGreetingsCounter;

GreetService(Config config) {
GreetService() {
Config config = Config.global();
greeting.set(config.get("app.greeting").asString().orElse("Ciao"));
MeterRegistry meterRegistry = Metrics.globalRegistry();
timerForGets = meterRegistry.getOrCreate(Timer.builder(TIMER_FOR_GETS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ static void setup(WebServerConfig.Builder server) {

// By default, this will pick up application.yaml from the classpath
Config config = Config.create();
Config.global(config);

server.routing(r -> routing(r, config))
server.routing(Main::routing)
.config(config.get("server"));

}
Expand All @@ -76,9 +77,10 @@ static void setup(WebServerConfig.Builder server) {
* Setup routing.
*
* @param routing routing builder
* @param config configuration of this server
*/
private static void routing(HttpRouting.Builder routing, Config config) {
private static void routing(HttpRouting.Builder routing) {
Config config = Config.global();

/*
* For purposes of illustration, the key performance indicator settings for the
* MetricsSupport instance are set up according to a system property, so you can see,
Expand All @@ -89,10 +91,8 @@ private static void routing(HttpRouting.Builder routing, Config config) {
? metricsSupportWithConfig(config.get("metrics"))
: metricsSupportWithoutConfig();

GreetService greetService = new GreetService(config);

routing.addFeature(ObserveFeature.just(metricsSupport))
.register("/greet", greetService);
.register("/greet", new GreetService());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public class GreetService implements HttpService {

private static final JsonBuilderFactory JSON_BF = Json.createBuilderFactory(Map.of());

GreetService(Config config) {
GreetService() {
Config config = Config.global();
this.greeting = config.get("app.greeting").asString().orElse("Ciao");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,23 @@ static void setup(WebServerConfig.Builder server) {

// By default, this will pick up application.yaml from the classpath
Config config = Config.create();
Config.global(config);

server.config(config.get("server"))
.routing(r -> routing(r, config));
.routing(Main::routing);
}

/**
* Set up routing.
*
* @param routing routing builder
* @param config configuration of this server
*/
static void routing(HttpRouting.Builder routing, Config config) {
static void routing(HttpRouting.Builder routing) {
Config config = Config.global();
routing.addFeature(ObserveFeature.create())
.addFeature(OpenApiFeature.builder()
.config(config.get(OpenApiFeature.Builder.CONFIG_KEY)))
.register("/greet", new GreetService(config));
.register("/greet", new GreetService());
}

}
Loading

0 comments on commit 2e6baf3

Please sign in to comment.