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

Config viewer as part of vertx-http in dev mode #7430

Closed
wants to merge 3 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
import java.util.Optional;
import java.util.stream.Collectors;

import javax.inject.Singleton;

import org.eclipse.microprofile.config.ConfigProvider;
import org.jboss.jandex.DotName;

import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.BeanContainerBuildItem;
import io.quarkus.builder.BuildException;
import io.quarkus.deployment.IsDevelopment;
import io.quarkus.deployment.IsNormal;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
Expand All @@ -30,11 +34,14 @@
import io.quarkus.vertx.core.deployment.EventLoopCountBuildItem;
import io.quarkus.vertx.core.deployment.InternalWebVertxBuildItem;
import io.quarkus.vertx.http.runtime.CurrentVertxRequest;
import io.quarkus.vertx.http.runtime.HandlerType;
import io.quarkus.vertx.http.runtime.HttpBuildTimeConfig;
import io.quarkus.vertx.http.runtime.HttpConfiguration;
import io.quarkus.vertx.http.runtime.RouterProducer;
import io.quarkus.vertx.http.runtime.VertxHttpRecorder;
import io.quarkus.vertx.http.runtime.cors.CORSRecorder;
import io.quarkus.vertx.http.runtime.devmode.ConfigHolder;
import io.quarkus.vertx.http.runtime.devmode.ConfigViewerHandler;
import io.quarkus.vertx.http.runtime.filters.Filter;
import io.vertx.core.Handler;
import io.vertx.core.impl.VertxImpl;
Expand Down Expand Up @@ -166,4 +173,17 @@ ServiceStartBuildItem finalizeRouter(
RuntimeInitializedClassBuildItem configureNativeCompilation() {
return new RuntimeInitializedClassBuildItem("io.vertx.ext.web.handler.sockjs.impl.XhrTransport");
}

@BuildStep(onlyIf = IsDevelopment.class)
public void configViewerRoute(HttpBuildTimeConfig httpBuildTimeConfig,
BuildProducer<AdditionalBeanBuildItem> beans,
BuildProducer<RouteBuildItem> routes) {
beans.produce(AdditionalBeanBuildItem.builder()
.addBeanClass(ConfigHolder.class)
.setDefaultScope(DotName.createSimple(Singleton.class.getName()))
.setUnremovable()
.build());
routes.produce(new RouteBuildItem(httpBuildTimeConfig.configPath,
new ConfigViewerHandler(), HandlerType.BLOCKING));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.quarkus.vertx.http.configviewer;

import static io.restassured.RestAssured.when;
import static org.hamcrest.Matchers.hasItem;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusDevModeTest;
import io.restassured.http.ContentType;

public class ShowConfigTest {

@RegisterExtension
static final QuarkusDevModeTest test = new QuarkusDevModeTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addAsResource(new StringAsset("foo=bar"), "application.properties"));

@Test
void testConfig() {
when().get("/config").then()
.statusCode(200)
.contentType(ContentType.JSON)
.body("sources.properties.foo", hasItem("bar"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ public class HttpBuildTimeConfig {
*/
@ConfigItem
public boolean virtual;

/**
* The path for the config viewer.
*/
@ConfigItem(defaultValue = "/config")
public String configPath;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.quarkus.vertx.http.runtime.devmode;

import javax.inject.Inject;

import org.eclipse.microprofile.config.Config;

public class ConfigHolder {

@Inject
Config config;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package io.quarkus.vertx.http.runtime.devmode;

import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.spi.ConfigSource;
import org.jboss.logging.Logger;

import io.quarkus.runtime.configuration.ExpandingConfigSource;

/**
* Turns a {@link Config} into a JSON object with all config sources and properties as JSON. The config sources are
* sorted descending by ordinal, the properties by name. If no config is defined an empty JSON object is returned.
*
* <p>
* A typical output might look like:
* </p>
*
* <pre>
* {
* "sources": [
* {
* "source": "source0",
* "ordinal": 200,
* "properties": {
* "key": "value"
* }
* },
* {
* "source": "source1",
* "ordinal": 100,
* "properties": {
* "key": "value"
* }
* }
* ]
* }
* </pre>
*/
class ConfigViewer {

private static final Logger LOGGER = Logger.getLogger(ConfigViewer.class.getName());

String dump(Config config) {
JsonObject json = new JsonObject();
if (config != null) {
boolean old = ExpandingConfigSource.setExpanding(false);
try {
if (config.getConfigSources().iterator().hasNext()) {
JsonArray jsonSources = new JsonArray();
for (ConfigSource source : config.getConfigSources()) {
JsonObject jsonSource = new JsonObject();
jsonSource.put("source", source.getName())
.put("ordinal", source.getOrdinal());
Set<String> propertyNames = source.getPropertyNames();
if (!propertyNames.isEmpty()) {
SortedSet<String> sortedPropertyNames = new TreeSet<>(propertyNames);
JsonObject jsonProperties = new JsonObject();
for (String propertyName : sortedPropertyNames) {
try {
jsonProperties.put(propertyName, source.getValue(propertyName));
} catch (Throwable t) {
LOGGER.errorf("Cannot get configuration value for '%s': %s",
propertyName, t.getMessage());
}
}
jsonSource.put("properties", jsonProperties);
}
jsonSources.put(jsonSource);
}
json.put("sources", jsonSources);
}
} finally {
ExpandingConfigSource.setExpanding(old);
}
}
return json.toString(2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.quarkus.vertx.http.runtime.devmode;

import javax.enterprise.inject.spi.CDI;

import io.vertx.core.Handler;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.RoutingContext;

public class ConfigViewerHandler implements Handler<RoutingContext> {

@Override
public void handle(RoutingContext routingContext) {
ConfigHolder configHolder = CDI.current().select(ConfigHolder.class).get();
String json = new ConfigViewer().dump(configHolder.config);
HttpServerResponse resp = routingContext.response();
resp.putHeader("content-type", "application/json");
resp.end(Buffer.buffer(json));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Copyright (c) 2002 JSON.org

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

The Software shall be used for Good, not Evil.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package io.quarkus.vertx.http.runtime.devmode;

import java.io.StringWriter;
import java.util.ArrayList;

class JsonArray {

private final ArrayList<Object> list;

JsonArray() {
this.list = new ArrayList<Object>();
}

JsonArray put(JsonObject value) {
list.add(value);
return this;
}

void write(StringWriter writer, int indentFactor, int indent) {
boolean needsComma = false;
int length = list.size();
writer.write('[');

if (length == 1) {
JsonObject.writeValue(writer, list.get(0), indentFactor, indent);
} else if (length != 0) {
int newIndent = indent + indentFactor;
for (int i = 0; i < length; i += 1) {
if (needsComma) {
writer.write(',');
}
if (indentFactor > 0) {
writer.write('\n');
}
JsonObject.indent(writer, newIndent);
JsonObject.writeValue(writer, list.get(i), indentFactor, newIndent);
needsComma = true;
}
if (indentFactor > 0) {
writer.write('\n');
}
JsonObject.indent(writer, indent);
}
writer.write(']');
}
}
Loading