Skip to content

Commit

Permalink
Merge pull request #49 from mkouba/qute-web-rename
Browse files Browse the repository at this point in the history
Rename quarkus-qute-server-pages to quarkus-qute-web
  • Loading branch information
mkouba authored Nov 10, 2023
2 parents 55ed15b + 31926ae commit 7eb31a0
Show file tree
Hide file tree
Showing 42 changed files with 531 additions and 394 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Quarkus QuteServerPages
# Quarkus Qute Web

[![Version](https://img.shields.io/maven-central/v/io.quarkiverse.quteserverpages/quarkus-qute-server-pages.svg?label=Maven%20Central)](https://search.maven.org/artifact/io.quarkiverse.quteserverpages/quarkus-qute-server-pages)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)

The goal of this simple extension is to expose [Qute](https://quarkus.io/guides/qute-reference) templates located in the `src/main/resource/templates` directory via HTTP. Automatically, no controllers needed. For example, the template `src/main/resource/templates/foo.html` will be served from the paths `/qsp/foo` and `/qsp/foo.html` by default.
The goal of this extension is to expose the [Qute](https://quarkus.io/guides/qute-reference) templates located in the `src/main/resource/templates` directory via HTTP. Automatically, no controllers needed. For example, the template `src/main/resource/templates/web/foo.html` will be served from the paths `/foo` and `/foo.html` by default.

In a template you can access:

Expand All @@ -13,6 +13,6 @@ In a template you can access:
- [Namespace Extension Methods](https://quarkus.io/guides/qute-reference#namespace_extension_methods) in general
- [global variables](https://quarkus.io/guides/qute-reference#global_variables)
- the current `io.vertx.core.http.HttpServerRequest` via the `qsp:` namespace, e.g. `{qsp:request.path}`
- the query parameters via the qsp: namespace, e.g. `{qsp:param('name')}` and `{qsp:param('name','DefaultName'}`
- the query parameters via the `http:` namespace, e.g. `{http:param('name')}` and `{http:param('name','DefaultName'}`

Read the full [documentation](https://quarkiverse.github.io/quarkiverse-docs/quarkus-quteserverpages/dev/index.html).
12 changes: 6 additions & 6 deletions deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.quarkiverse.quteserverpages</groupId>
<artifactId>quarkus-qute-server-pages-parent</artifactId>
<groupId>io.quarkiverse.qute.web</groupId>
<artifactId>quarkus-qute-web-parent</artifactId>
<version>999-SNAPSHOT</version>
</parent>
<artifactId>quarkus-qute-server-pages-deployment</artifactId>
<name>Quarkus QuteServerPages - Deployment</name>
<artifactId>quarkus-qute-web-deployment</artifactId>
<name>Quarkus Qute Web - Deployment</name>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
Expand All @@ -22,8 +22,8 @@
<artifactId>quarkus-vertx-http-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkiverse.quteserverpages</groupId>
<artifactId>quarkus-qute-server-pages</artifactId>
<groupId>io.quarkiverse.qute.web</groupId>
<artifactId>quarkus-qute-web</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package io.quarkiverse.qute.web.deployment;

import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.jboss.logging.Logger;

import io.quarkiverse.qute.web.runtime.QuteWebBuildTimeConfig;
import io.quarkiverse.qute.web.runtime.QuteWebExtensions;
import io.quarkiverse.qute.web.runtime.QuteWebRecorder;
import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.SyntheticBeansRuntimeInitBuildItem;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.Consume;
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.qute.deployment.TemplateFilePathsBuildItem;
import io.quarkus.vertx.http.deployment.HttpRootPathBuildItem;
import io.quarkus.vertx.http.deployment.RouteBuildItem;
import io.quarkus.vertx.http.runtime.HandlerType;

class QuteWebProcessor {

private static final Logger LOG = Logger.getLogger(QuteWebProcessor.class);

private static final String FEATURE = "qute-web";

@BuildStep
FeatureBuildItem feature() {
return new FeatureBuildItem(FEATURE);
}

@BuildStep
AdditionalBeanBuildItem beans() {
// It is not a bean but we need to make it a part of the bean archive index
return new AdditionalBeanBuildItem(QuteWebExtensions.class);
}

@BuildStep
public void collectTemplatePaths(TemplateFilePathsBuildItem templateFilePaths,
QuteWebBuildTimeConfig config, BuildProducer<QuteWebTemplatePathBuildItem> paths) {
String webTemplatesPathPrefix = "";
String webTemplatesDir = config.webTemplatesDir();
if (!webTemplatesDir.equals("/") && !webTemplatesDir.isBlank()) {
webTemplatesPathPrefix = webTemplatesDir + "/";
}
Optional<Pattern> hiddenTemplates = config.hiddenTemplates();
for (String path : templateFilePaths.getFilePaths()) {
if (!path.startsWith(webTemplatesPathPrefix)) {
continue;
}
if (hiddenTemplates.isPresent()
// Match the path relative to the webTemplatesPath
&& hiddenTemplates.get().matcher(path.substring(webTemplatesPathPrefix.length())).matches()) {
LOG.debugf("Template %s is hidden", path);
continue;
}
LOG.debugf("Web template found: %s", path);
paths.produce(new QuteWebTemplatePathBuildItem(path));
}
}

@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
@Consume(SyntheticBeansRuntimeInitBuildItem.class)
public RouteBuildItem produceTemplatesRoute(QuteWebRecorder recorder, List<QuteWebTemplatePathBuildItem> templatePaths,
HttpRootPathBuildItem httpRootPath, QuteWebBuildTimeConfig config) {
return httpRootPath.routeBuilder()
.routeFunction(httpRootPath.relativePath(config.rootPath() + "/*"), recorder.initializeRoute())
.handlerType(config.useBlockingHandler() ? HandlerType.BLOCKING : HandlerType.NORMAL)
.handler(recorder.handler(httpRootPath.relativePath(config.rootPath()),
templatePaths.stream().map(QuteWebTemplatePathBuildItem::getPath).collect(Collectors.toSet())))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package io.quarkiverse.quteserverpages.deployment;
package io.quarkiverse.qute.web.deployment;

import io.quarkus.builder.item.MultiBuildItem;

public final class QspTemplatePathBuildItem extends MultiBuildItem {
public final class QuteWebTemplatePathBuildItem extends MultiBuildItem {

private final String path;

public QspTemplatePathBuildItem(String path) {
public QuteWebTemplatePathBuildItem(String path) {
this.path = path;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package io.quarkiverse.quteserverpages.deployment.devui;
package io.quarkiverse.qute.web.deployment.devui;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import io.quarkiverse.quteserverpages.deployment.QspTemplatePathBuildItem;
import io.quarkiverse.quteserverpages.runtime.QspBuildTimeConfig;
import io.quarkiverse.qute.web.deployment.QuteWebTemplatePathBuildItem;
import io.quarkiverse.qute.web.runtime.QuteWebBuildTimeConfig;
import io.quarkus.deployment.IsDevelopment;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
Expand All @@ -14,22 +14,22 @@
import io.quarkus.vertx.http.deployment.HttpRootPathBuildItem;
import io.vertx.core.json.JsonArray;

public class QspDevUIProcessor {
public class QuteWebDevUIProcessor {

@BuildStep(onlyIf = IsDevelopment.class)
public void pages(List<QspTemplatePathBuildItem> templatePaths, HttpRootPathBuildItem httpRootPath,
QspBuildTimeConfig config, BuildProducer<CardPageBuildItem> cardPages) {
public void pages(List<QuteWebTemplatePathBuildItem> templatePaths, HttpRootPathBuildItem httpRootPath,
QuteWebBuildTimeConfig config, BuildProducer<CardPageBuildItem> cardPages) {

CardPageBuildItem pageBuildItem = new CardPageBuildItem();

JsonArray paths = new JsonArray();
for (String path : templatePaths.stream().map(QspTemplatePathBuildItem::getPath)
for (String path : templatePaths.stream().map(QuteWebTemplatePathBuildItem::getPath)
.sorted(Comparator.comparing(p -> p.toLowerCase())).collect(Collectors.toList())) {
paths.add(path);
}

pageBuildItem.addBuildTimeData("paths", paths);
pageBuildItem.addBuildTimeData("rootPrefix", httpRootPath.relativePath(config.rootPath) + "/");
pageBuildItem.addBuildTimeData("rootPrefix", httpRootPath.relativePath(config.rootPath()) + "/");

pageBuildItem.addPage(Page.webComponentPageBuilder()
.title("Pages")
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.quarkiverse.quteserverpages.test;
package io.quarkiverse.qute.web.test;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
Expand All @@ -20,16 +20,16 @@ public class BlockingHandlerTest {
root
.addAsResource(new StringAsset(
"blocking={cdi:bean.isOnWorkerThread}"),
"templates/blocking.txt")
"templates/web/blocking.txt")
.addAsResource(new StringAsset(
"quarkus.qsp.use-blocking-handler=true"),
"quarkus.qute.web.use-blocking-handler=true"),
"application.properties");
});

@Test
public void testHandler() {
given()
.when().get("/qsp/blocking")
.when().get("/blocking")
.then()
.statusCode(200)
.body(containsString("blocking=true"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.quarkiverse.quteserverpages.test;
package io.quarkiverse.qute.web.test;

import static io.restassured.RestAssured.get;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -17,15 +17,15 @@ public class CompressionDisabledTest {
@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withApplicationRoot(root -> root
.addAsResource(new StringAsset("{cdi:vertxRequest.getParam('id')}"), "templates/file.txt")
.addAsResource(new StringAsset("{cdi:vertxRequest.getParam('id')}"), "templates/document.foo"))
.addAsResource(new StringAsset("{cdi:vertxRequest.getParam('id')}"), "templates/web/file.txt")
.addAsResource(new StringAsset("{cdi:vertxRequest.getParam('id')}"), "templates/web/document.foo"))
.overrideConfigKey("quarkus.http.enable-compression", "false")
.overrideConfigKey("quarkus.qute.suffixes", "txt,foo");

@Test
public void testPages() {
assertUncompressed("/qsp/file?id=1", "1");
assertUncompressed("/qsp/document.foo?id=2", "2");
assertUncompressed("/file?id=1", "1");
assertUncompressed("/document.foo?id=2", "2");
}

private void assertUncompressed(String path, String body) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.quarkiverse.quteserverpages.test;
package io.quarkiverse.qute.web.test;

import static io.restassured.RestAssured.get;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -17,15 +17,15 @@ public class CompressionEnabledTest {
@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withApplicationRoot(root -> root
.addAsResource(new StringAsset("{cdi:vertxRequest.getParam('id')}"), "templates/file.txt")
.addAsResource(new StringAsset("{cdi:vertxRequest.getParam('id')}"), "templates/document.foo"))
.addAsResource(new StringAsset("{cdi:vertxRequest.getParam('id')}"), "templates/web/file.txt")
.addAsResource(new StringAsset("{cdi:vertxRequest.getParam('id')}"), "templates/web/document.foo"))
.overrideConfigKey("quarkus.http.enable-compression", "true")
.overrideConfigKey("quarkus.qute.suffixes", "txt,foo");

@Test
public void testPages() {
assertCompressed("/qsp/file?id=1", "1");
assertUncompressed("/qsp/document.foo?id=2", "2");
assertCompressed("/file?id=1", "1");
assertUncompressed("/document.foo?id=2", "2");
}

private void assertCompressed(String path, String body) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.quarkiverse.quteserverpages.test;
package io.quarkiverse.qute.web.test;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
Expand All @@ -15,15 +15,15 @@ public class CustomRootPathTest {
static final QuarkusUnitTest config = new QuarkusUnitTest().withApplicationRoot(root -> {
root.addAsResource(new StringAsset(
"Hello {name ?: 'world'}!"),
"templates/hello.txt")
"templates/web/hello.txt")
.addAsResource(new StringAsset(
"Index root!"),
"templates/index.html")
"templates/web/index.html")
.addAsResource(new StringAsset(
"Index foo!"),
"templates/foo/index.html")
"templates/web/foo/index.html")
.addAsResource(new StringAsset(
"quarkus.qsp.root-path=ping"),
"quarkus.qute.web.root-path=ping"),
"application.properties");
});

Expand Down
Loading

0 comments on commit 7eb31a0

Please sign in to comment.