Rename helidon-3rdparty to helidon-dependencies. (#1031)

* Rename helidon-3rdparty to helidon-world.
Add standalone quickstart examples.

* rename helidon-world to helidon-dependencies ; update helidon-standalone-quickstart-mp readme to fix the title

* Update helidon-dependencies pom name

* fix typo in SE quickstarts: routine -> routing
This commit is contained in:
Romain Grecourt
2019-09-18 15:16:51 -07:00
committed by GitHub
parent 39b4c59e53
commit df30a8bb92
38 changed files with 1937 additions and 16 deletions

View File

@@ -22,9 +22,9 @@
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.helidon</groupId>
<artifactId>helidon-3rdparty</artifactId>
<artifactId>helidon-dependencies</artifactId>
<version>1.3.1-SNAPSHOT</version>
<relativePath>../3rdparty/pom.xml</relativePath>
<relativePath>../dependencies/pom.xml</relativePath>
</parent>
<groupId>io.helidon.applications</groupId>
<artifactId>helidon-applications-project</artifactId>
@@ -151,7 +151,6 @@
<addClasspath>true</addClasspath>
<classpathPrefix>libs</classpathPrefix>
<mainClass>${mainClass}</mainClass>
<useUniqueVersions>true</useUniqueVersions>
</manifest>
</archive>
</configuration>

View File

@@ -26,11 +26,11 @@
<version>1.3.1-SNAPSHOT</version>
<relativePath>../bom/pom.xml</relativePath>
</parent>
<artifactId>helidon-3rdparty</artifactId>
<artifactId>helidon-dependencies</artifactId>
<packaging>pom</packaging>
<name>Helidon 3rd Party Management</name>
<description>Distributed 3party dependency management</description>
<name>Helidon Dependencies</name>
<description>Maven dependency management for Helidon applications</description>
<properties>
<!--

View File

@@ -1,10 +1,8 @@
# Helidon Quickstart Examples
These are the examples used by the Helidon Getting Started guide, and
the quickstart Maven archetypes. Both quickstarts implement the same
simple greeting service, one usine Helidon MP and one using Helidon SE.
the quickstart Maven archetypes. All examples implement the same
simple greeting service, one using Helidon MP and one using Helidon SE.
The `archetypes` directory contains scripts for building the Maven
archetypes from these examples.

View File

@@ -1,6 +1,6 @@
# Helidon Quickstart MP Example
This example implements a simple Hello World REST service using MicroProfile
This example implements a simple Hello World REST service using MicroProfile.
## Build and run

View File

@@ -64,7 +64,7 @@ public class GreetService implements Service {
}
/**
* A service registers itself by updating the routine rules.
* A service registers itself by updating the routing rules.
* @param rules the routing rules.
*/
@Override

View File

@@ -0,0 +1 @@
target/*

View File

@@ -0,0 +1,16 @@
hs_err_pid*
target/
.DS_Store
.idea/
*.iws
*.ipr
*.iml
atlassian-ide-plugin.xml
nbactions.xml
nb-configuration.xml
.settings
.settings/
.project
.classpath
*.swp
*~

View File

@@ -0,0 +1,44 @@
#
# Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
#
# 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.
#
# 1st stage, build the app
FROM maven:3.5.4-jdk-9 as build
WORKDIR /helidon
# Create a first layer to cache the "Maven World" in the local repository.
# Incremental docker builds will always resume after that, unless you update
# the pom
ADD pom.xml .
RUN mvn package -DskipTests
# Do the Maven build!
# Incremental docker builds will resume here when you change sources
ADD src src
RUN mvn package -DskipTests
RUN echo "done!"
# 2nd stage, build the runtime image
FROM openjdk:8-jre-slim
WORKDIR /helidon
# Copy the binary built in the 1st stage
COPY --from=build /helidon/target/helidon-standalone-quickstart-mp.jar ./
COPY --from=build /helidon/target/libs ./libs
CMD ["java", "-jar", "helidon-standalone-quickstart-mp.jar"]
EXPOSE 8080

View File

@@ -0,0 +1,69 @@
# Helidon Standalone Quickstart MP Example
This example implements a simple Hello World REST service using MicroProfile
with a standalone Maven pom.
## Build and run
With JDK8+
```bash
mvn package
java -jar target/helidon-standalone-quickstart-mp.jar
```
## Exercise the application
```
curl -X GET http://localhost:8080/greet
{"message":"Hello World!"}
curl -X GET http://localhost:8080/greet/Joe
{"message":"Hello Joe!"}
curl -X PUT -H "Content-Type: application/json" -d '{"greeting" : "Hola"}' http://localhost:8080/greet/greeting
curl -X GET http://localhost:8080/greet/Jose
{"message":"Hola Jose!"}
```
## Try health and metrics
```
curl -s -X GET http://localhost:8080/health
{"outcome":"UP",...
. . .
# Prometheus Format
curl -s -X GET http://localhost:8080/metrics
# TYPE base:gc_g1_young_generation_count gauge
. . .
# JSON Format
curl -H 'Accept: application/json' -X GET http://localhost:8080/metrics
{"base":...
. . .
```
## Build the Docker Image
```
docker build -t helidon-standalone-quickstart-mp .
```
## Start the application with Docker
```
docker run --rm -p 8080:8080 helidon-standalone-quickstart-mp:latest
```
Exercise the application as described above
## Deploy the application to Kubernetes
```
kubectl cluster-info # Verify which cluster
kubectl get pods # Verify connectivity to cluster
kubectl create -f app.yaml # Deploy application
kubectl get service helidon-standalone-quickstart-mp # Verify deployed service
```

View File

@@ -0,0 +1,50 @@
#
# Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
#
# 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.
#
kind: Service
apiVersion: v1
metadata:
name: helidon-standalone-quickstart-mp
labels:
app: helidon-standalone-quickstart-mp
spec:
type: NodePort
selector:
app: helidon-standalone-quickstart-mp
ports:
- port: 8080
targetPort: 8080
name: http
---
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
name: helidon-standalone-quickstart-mp
spec:
replicas: 1
template:
metadata:
labels:
app: helidon-standalone-quickstart-mp
version: v1
spec:
containers:
- name: helidon-standalone-quickstart-mp
image: helidon-standalone-quickstart-mp
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
---

View File

@@ -0,0 +1,222 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
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.
-->
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.helidon.examples.quickstarts</groupId>
<artifactId>helidon-standalone-quickstart-mp</artifactId>
<version>1.3.1-SNAPSHOT</version>
<name>Helidon Standalone Quickstart MP Example</name>
<properties>
<helidon.version>1.3.1-SNAPSHOT</helidon.version>
<mainClass>io.helidon.examples.quickstart.mp.Main</mainClass>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- plugin dependencies versions -->
<version.lib.junit>5.1.0</version.lib.junit>
<!-- plugin versions -->
<version.plugin.compiler>3.8.1</version.plugin.compiler>
<version.plugin.dependency>3.0.0</version.plugin.dependency>
<version.plugin.eclipselink>2.7.1.1</version.plugin.eclipselink>
<version.plugin.exec>1.6.0</version.plugin.exec>
<version.plugin.failsafe>2.19.1</version.plugin.failsafe>
<version.plugin.jandex>1.0.6</version.plugin.jandex>
<version.plugin.jar>3.0.2</version.plugin.jar>
<version.plugin.os>1.5.0.Final</version.plugin.os>
<version.plugin.protobuf>0.5.1</version.plugin.protobuf>
<version.plugin.resources>2.7</version.plugin.resources>
<version.plugin.surefire.provider.junit>1.0.3</version.plugin.surefire.provider.junit>
<version.plugin.surefire>2.19.1</version.plugin.surefire>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.helidon</groupId>
<artifactId>helidon-dependencies</artifactId>
<version>${helidon.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.helidon.microprofile.bundles</groupId>
<artifactId>helidon-microprofile-3.0</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
</dependency>
<dependency>
<groupId>org.jboss</groupId>
<artifactId>jandex</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>javax.activation-api</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${version.plugin.compiler}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${version.plugin.surefire}</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${version.plugin.surefire.provider.junit}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${version.lib.junit}</version>
</dependency>
</dependencies>
<configuration>
<systemPropertyVariables>
<java.util.logging.config.file>${project.build.outputDirectory}/logging.properties</java.util.logging.config.file>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${version.plugin.failsafe}</version>
<configuration>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
</configuration>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${version.plugin.surefire.provider.junit}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${version.lib.junit}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${version.plugin.dependency}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>${version.plugin.resources}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${version.plugin.jar}</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>libs</classpathPrefix>
<mainClass>${mainClass}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.jboss.jandex</groupId>
<artifactId>jandex-maven-plugin</artifactId>
<version>${version.plugin.jandex}</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>${version.plugin.exec}</version>
<configuration>
<mainClass>${mainClass}</mainClass>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-libs</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/libs</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<overWriteIfNewer>true</overWriteIfNewer>
<includeScope>runtime</includeScope>
<excludeScope>test</excludeScope>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jboss.jandex</groupId>
<artifactId>jandex-maven-plugin</artifactId>
<executions>
<execution>
<id>make-index</id>
<goals>
<goal>jandex</goal>
</goals>
<phase>process-classes</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,38 @@
/*
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
*
* 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 io.helidon.examples.quickstart.mp;
import java.util.Set;
import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import io.helidon.common.CollectionsHelper;
/**
* Simple Application that produces a greeting message.
*/
@ApplicationScoped
@ApplicationPath("/")
public class GreetApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
return CollectionsHelper.setOf(GreetResource.class);
}
}

View File

@@ -0,0 +1,130 @@
/*
* Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
*
* 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 io.helidon.examples.quickstart.mp;
import java.util.Collections;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.json.Json;
import javax.json.JsonBuilderFactory;
import javax.json.JsonObject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
/**
* A simple JAX-RS resource to greet you. Examples:
*
* Get default greeting message:
* curl -X GET http://localhost:8080/greet
*
* Get greeting message for Joe:
* curl -X GET http://localhost:8080/greet/Joe
*
* Change greeting
* curl -X PUT -H "Content-Type: application/json" -d '{"greeting" : "Howdy"}' http://localhost:8080/greet/greeting
*
* The message is returned as a JSON object.
*/
@Path("/greet")
@RequestScoped
public class GreetResource {
private static final JsonBuilderFactory JSON = Json.createBuilderFactory(Collections.emptyMap());
/**
* The greeting message provider.
*/
private final GreetingProvider greetingProvider;
/**
* Using constructor injection to get a configuration property.
* By default this gets the value from META-INF/microprofile-config
*
* @param greetingConfig the configured greeting message
*/
@Inject
public GreetResource(GreetingProvider greetingConfig) {
this.greetingProvider = greetingConfig;
}
/**
* Return a wordly greeting message.
*
* @return {@link JsonObject}
*/
@SuppressWarnings("checkstyle:designforextension")
@GET
@Produces(MediaType.APPLICATION_JSON)
public JsonObject getDefaultMessage() {
return createResponse("World");
}
/**
* Return a greeting message using the name that was provided.
*
* @param name the name to greet
* @return {@link JsonObject}
*/
@SuppressWarnings("checkstyle:designforextension")
@Path("/{name}")
@GET
@Produces(MediaType.APPLICATION_JSON)
public JsonObject getMessage(@PathParam("name") String name) {
return createResponse(name);
}
/**
* Set the greeting to use in future messages.
*
* @param jsonObject JSON containing the new greeting
* @return {@link Response}
*/
@SuppressWarnings("checkstyle:designforextension")
@Path("/greeting")
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response updateGreeting(JsonObject jsonObject) {
if (!jsonObject.containsKey("greeting")) {
JsonObject entity = JSON.createObjectBuilder()
.add("error", "No greeting provided")
.build();
return Response.status(Response.Status.BAD_REQUEST).entity(entity).build();
}
String newGreeting = jsonObject.getString("greeting");
greetingProvider.setMessage(newGreeting);
return Response.status(Response.Status.NO_CONTENT).build();
}
private JsonObject createResponse(String who) {
String msg = String.format("%s %s!", greetingProvider.getMessage(), who);
return JSON.createObjectBuilder()
.add("message", msg)
.build();
}
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
*
* 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 io.helidon.examples.quickstart.mp;
import java.util.concurrent.atomic.AtomicReference;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import org.eclipse.microprofile.config.inject.ConfigProperty;
/**
* Provider for greeting message.
*/
@ApplicationScoped
public class GreetingProvider {
private final AtomicReference<String> message = new AtomicReference<>();
/**
* Create a new greeting provider, reading the message from configuration.
*
* @param message greeting to use
*/
@Inject
public GreetingProvider(@ConfigProperty(name = "app.greeting") String message) {
this.message.set(message);
}
String getMessage() {
return message.get();
}
void setMessage(String message) {
this.message.set(message);
}
}

View File

@@ -0,0 +1,69 @@
/*
* Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
*
* 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 io.helidon.examples.quickstart.mp;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.LogManager;
import io.helidon.microprofile.server.Server;
/**
* The application main class.
*/
public final class Main {
/**
* Cannot be instantiated.
*/
private Main() { }
/**
* Application main entry point.
* @param args command line arguments
* @throws IOException if there are problems reading logging properties
*/
public static void main(final String[] args) throws IOException {
// load logging configuration
setupLogging();
// start the server
Server server = startServer();
System.out.println("http://localhost:" + server.port() + "/greet");
}
/**
* Start the server.
* @return the created {@link Server} instance
*/
static Server startServer() {
// Server will automatically pick up configuration from
// microprofile-config.properties
// and Application classes annotated as @ApplicationScoped
return Server.create().start();
}
/**
* Configure logging from logging.properties file.
*/
private static void setupLogging() throws IOException {
try (InputStream is = Main.class.getResourceAsStream("/logging.properties")) {
LogManager.getLogManager().readConfiguration(is);
}
}
}

View File

@@ -0,0 +1,20 @@
/*
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
*
* 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.
*/
/**
* Quickstart MicroProfile example.
*/
package io.helidon.examples.quickstart.mp;

View File

@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
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.
-->
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
version="2.0"
bean-discovery-mode="annotated">
</beans>

View File

@@ -0,0 +1,22 @@
#
# Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
#
# 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.
#
# Application properties. This is the default greeting
app.greeting=Hello
# Microprofile server properties
server.port=8080
server.host=0.0.0.0

View File

@@ -0,0 +1,40 @@
#
# Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
#
# 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.
#
# Example Logging Configuration File
# For more information see $JAVA_HOME/jre/lib/logging.properties
# Send messages to the console
handlers=java.util.logging.ConsoleHandler
# Global default logging level. Can be overriden by specific handlers and loggers
.level=INFO
# Helidon Web Server has a custom log formatter that extends SimpleFormatter.
# It replaces "!thread!" with the current thread name
java.util.logging.ConsoleHandler.level=INFO
java.util.logging.ConsoleHandler.formatter=io.helidon.webserver.WebServerLogFormatter
java.util.logging.SimpleFormatter.format=%1$tY.%1$tm.%1$td %1$tH:%1$tM:%1$tS %4$s %3$s !thread!: %5$s%6$s%n
#Component specific log levels
#io.helidon.webserver.level=INFO
#io.helidon.config.level=INFO
#io.helidon.security.level=INFO
#io.helidon.microprofile.level=INFO
#io.helidon.common.level=INFO
#io.netty.level=INFO
#org.glassfish.jersey.level=INFO
#org.jboss.weld=INFO

View File

@@ -0,0 +1,96 @@
/*
* Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
*
* 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 io.helidon.examples.quickstart.mp;
import javax.enterprise.inject.se.SeContainer;
import javax.enterprise.inject.spi.CDI;
import javax.json.JsonObject;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import io.helidon.microprofile.server.Server;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
class MainTest {
private static Server server;
@BeforeAll
public static void startTheServer() throws Exception {
server = Main.startServer();
}
@Test
void testHelloWorld() {
Client client = ClientBuilder.newClient();
JsonObject jsonObject = client
.target(getConnectionString("/greet"))
.request()
.get(JsonObject.class);
Assertions.assertEquals("Hello World!", jsonObject.getString("message"),
"default message");
jsonObject = client
.target(getConnectionString("/greet/Joe"))
.request()
.get(JsonObject.class);
Assertions.assertEquals("Hello Joe!", jsonObject.getString("message"),
"hello Joe message");
Response r = client
.target(getConnectionString("/greet/greeting"))
.request()
.put(Entity.entity("{\"greeting\" : \"Hola\"}", MediaType.APPLICATION_JSON));
Assertions.assertEquals(204, r.getStatus(), "PUT status code");
jsonObject = client
.target(getConnectionString("/greet/Jose"))
.request()
.get(JsonObject.class);
Assertions.assertEquals("Hola Jose!", jsonObject.getString("message"),
"hola Jose message");
r = client
.target(getConnectionString("/metrics"))
.request()
.get();
Assertions.assertEquals(200, r.getStatus(), "GET metrics status code");
r = client
.target(getConnectionString("/health"))
.request()
.get();
Assertions.assertEquals(200, r.getStatus(), "GET health status code");
}
@AfterAll
static void destroyClass() {
CDI<Object> current = CDI.current();
((SeContainer) current).close();
}
private String getConnectionString(String path) {
return "http://localhost:" + server.port() + path;
}
}

View File

@@ -0,0 +1,22 @@
#
# Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
#
# 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.
#
# Override configuration to use a random port for the unit tests
config_ordinal=1000
# Microprofile server properties
server.port=-1
server.host=0.0.0.0

View File

@@ -0,0 +1 @@
target/*

View File

@@ -0,0 +1,16 @@
hs_err_pid*
target/
.DS_Store
.idea/
*.iws
*.ipr
*.iml
atlassian-ide-plugin.xml
nbactions.xml
nb-configuration.xml
.settings
.settings/
.project
.classpath
*.swp
*~

View File

@@ -0,0 +1,45 @@
#
# Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
#
# 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.
#
# 1st stage, build the app
FROM maven:3.5.4-jdk-9 as build
WORKDIR /helidon
# Create a first layer to cache the "Maven World" in the local repository.
# Incremental docker builds will always resume after that, unless you update
# the pom
ADD pom.xml .
RUN mvn package -DskipTests
# Do the Maven build!
# Incremental docker builds will resume here when you change sources
ADD src src
RUN mvn package -DskipTests
RUN echo "done!"
# 2nd stage, build the runtime image
FROM openjdk:8-jre-slim
WORKDIR /helidon
# Copy the binary built in the 1st stage
COPY --from=build /helidon/target/helidon-standalone-quickstart-se.jar ./
COPY --from=build /helidon/target/libs ./libs
CMD ["java", "-jar", "helidon-standalone-quickstart-se.jar"]
EXPOSE 8080

View File

@@ -0,0 +1,44 @@
#
# Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
#
# 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.
#
# 1st stage, build the app
FROM helidon/jdk8-graalvm-maven:19.2.0 as build
WORKDIR /helidon
# Create a first layer to cache the "Maven World" in the local repository.
# Incremental docker builds will always resume after that, unless you update
# the pom
ADD pom.xml .
RUN mvn package -Pnative-image -Dnative.image.skip -DskipTests
# Do the Maven build!
# Incremental docker builds will resume here when you change sources
ADD src src
RUN mvn package -Pnative-image -Dnative.image.buildStatic -DskipTests
RUN echo "done!"
# 2nd stage, build the runtime image
FROM scratch
WORKDIR /helidon
# Copy the binary built in the 1st stage
COPY --from=build /helidon/target/helidon-standalone-quickstart-se .
ENTRYPOINT ["./helidon-standalone-quickstart-se"]
EXPOSE 8080

View File

@@ -0,0 +1,117 @@
# Helidon Standalone Quickstart SE Example
This project implements a simple Hello World REST service using Helidon SE with
a standalone Maven pom.
## Build and run
With JDK8+
```bash
mvn package
java -jar target/helidon-standalone-quickstart-se.jar
```
## Exercise the application
```
curl -X GET http://localhost:8080/greet
{"message":"Hello World!"}
curl -X GET http://localhost:8080/greet/Joe
{"message":"Hello Joe!"}
curl -X PUT -H "Content-Type: application/json" -d '{"greeting" : "Hola"}' http://localhost:8080/greet/greeting
curl -X GET http://localhost:8080/greet/Jose
{"message":"Hola Jose!"}
```
## Try health and metrics
```
curl -s -X GET http://localhost:8080/health
{"outcome":"UP",...
. . .
# Prometheus Format
curl -s -X GET http://localhost:8080/metrics
# TYPE base:gc_g1_young_generation_count gauge
. . .
# JSON Format
curl -H 'Accept: application/json' -X GET http://localhost:8080/metrics
{"base":...
. . .
```
## Build the Docker Image
```
docker build -t helidon-standalone-quickstart-se .
```
## Start the application with Docker
```
docker run --rm -p 8080:8080 helidon-standalone-quickstart-se:latest
```
Exercise the application as described above
## Deploy the application to Kubernetes
```
kubectl cluster-info # Verify which cluster
kubectl get pods # Verify connectivity to cluster
kubectl create -f app.yaml # Deply application
kubectl get service helidon-standalone-quickstart-se # Get service info
```
## Native image with GraalVM
GraalVM allows you to compile your programs ahead-of-time into a native
executable. See https://www.graalvm.org/docs/reference-manual/aot-compilation/
for more information.
You can build a native executable in 2 different ways:
* With a local installation of GraalVM
* Using Docker
### Local build
Download Graal VM at https://github.com/oracle/graal/releases, the versions
currently supported for Helidon is `19.1.1` and `19.2.0`.
```
# Setup the environment
export GRAALVM_HOME=/path
# build the native executable
mvn package -Pnative-image
```
You can also put the Graal VM `bin` directory in your PATH, or pass
`-DgraalVMHome=/path` to the Maven command.
See https://github.com/oracle/helidon-build-tools/tree/master/helidon-maven-plugin
for more information.
Start the application:
```
./target/helidon-standalone-quickstart-se
```
### Multi-stage Docker build
Build the "native" Docker Image
```
docker build -t helidon-standalone-quickstart-se-native -f Dockerfile.native .
```
Start the application:
```
docker run --rm -p 8080:8080 helidon-standalone-quickstart-se-native:latest
```

View File

@@ -0,0 +1,50 @@
#
# Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
#
# 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.
#
kind: Service
apiVersion: v1
metadata:
name: helidon-standalone-quickstart-se
labels:
app: helidon-standalone-quickstart-se
spec:
type: NodePort
selector:
app: helidon-standalone-quickstart-se
ports:
- port: 8080
targetPort: 8080
name: http
---
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
name: helidon-standalone-quickstart-se
spec:
replicas: 1
template:
metadata:
labels:
app: helidon-standalone-quickstart-se
version: v1
spec:
containers:
- name: helidon-standalone-quickstart-se
image: helidon-standalone-quickstart-se
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
---

View File

@@ -0,0 +1,236 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
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.
-->
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.helidon.examples.quickstarts</groupId>
<artifactId>helidon-standalone-quickstart-se</artifactId>
<version>1.3.1-SNAPSHOT</version>
<name>Helidon Standalone Quickstart SE Example</name>
<properties>
<helidon.version>1.3.1-SNAPSHOT</helidon.version>
<mainClass>io.helidon.examples.quickstart.se.Main</mainClass>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- plugin dependencies versions -->
<version.lib.junit>5.1.0</version.lib.junit>
<!-- plugin versions -->
<version.plugin.compiler>3.8.1</version.plugin.compiler>
<version.plugin.dependency>3.0.0</version.plugin.dependency>
<version.plugin.exec>1.6.0</version.plugin.exec>
<version.plugin.failsafe>2.19.1</version.plugin.failsafe>
<version.plugin.helidon>1.0.10</version.plugin.helidon>
<version.plugin.jar>3.0.2</version.plugin.jar>
<version.plugin.os>1.5.0.Final</version.plugin.os>
<version.plugin.protobuf>0.5.1</version.plugin.protobuf>
<version.plugin.resources>2.7</version.plugin.resources>
<version.plugin.surefire.provider.junit>1.0.3</version.plugin.surefire.provider.junit>
<version.plugin.surefire>2.19.1</version.plugin.surefire>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.helidon</groupId>
<artifactId>helidon-dependencies</artifactId>
<version>${helidon.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.helidon.bundles</groupId>
<artifactId>helidon-bundles-webserver</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-yaml</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.health</groupId>
<artifactId>helidon-health</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.health</groupId>
<artifactId>helidon-health-checks</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.metrics</groupId>
<artifactId>helidon-metrics</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${version.plugin.compiler}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${version.plugin.surefire}</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${version.plugin.surefire.provider.junit}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${version.lib.junit}</version>
</dependency>
</dependencies>
<configuration>
<systemPropertyVariables>
<java.util.logging.config.file>${project.build.outputDirectory}/logging.properties</java.util.logging.config.file>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${version.plugin.failsafe}</version>
<configuration>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
</configuration>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${version.plugin.surefire.provider.junit}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${version.lib.junit}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${version.plugin.dependency}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>${version.plugin.resources}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${version.plugin.jar}</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>libs</classpathPrefix>
<mainClass>${mainClass}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>${version.plugin.exec}</version>
<configuration>
<mainClass>${mainClass}</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>io.helidon.build-tools</groupId>
<artifactId>helidon-maven-plugin</artifactId>
<version>${version.plugin.helidon}</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-libs</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/libs</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<overWriteIfNewer>true</overWriteIfNewer>
<includeScope>runtime</includeScope>
<excludeScope>test</excludeScope>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>native-image</id>
<build>
<plugins>
<plugin>
<groupId>io.helidon.build-tools</groupId>
<artifactId>helidon-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>native-image</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>io.helidon.integrations.graal</groupId>
<artifactId>helidon-graal-native-image-extension</artifactId>
</dependency>
</dependencies>
</profile>
</profiles>
</project>

View File

@@ -0,0 +1,156 @@
/*
* Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
*
* 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 io.helidon.examples.quickstart.se;
import java.util.Collections;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.json.Json;
import javax.json.JsonBuilderFactory;
import javax.json.JsonException;
import javax.json.JsonObject;
import io.helidon.common.http.Http;
import io.helidon.config.Config;
import io.helidon.webserver.Routing;
import io.helidon.webserver.ServerRequest;
import io.helidon.webserver.ServerResponse;
import io.helidon.webserver.Service;
/**
* A simple service to greet you. Examples:
*
* Get default greeting message:
* curl -X GET http://localhost:8080/greet
*
* Get greeting message for Joe:
* curl -X GET http://localhost:8080/greet/Joe
*
* Change greeting
* curl -X PUT -H "Content-Type: application/json" -d '{"greeting" : "Howdy"}' http://localhost:8080/greet/greeting
*
* The message is returned as a JSON object
*/
public class GreetService implements Service {
/**
* The config value for the key {@code greeting}.
*/
private final AtomicReference<String> greeting = new AtomicReference<>();
private static final JsonBuilderFactory JSON = Json.createBuilderFactory(Collections.emptyMap());
private static final Logger LOGGER = Logger.getLogger(GreetService.class.getName());
GreetService(Config config) {
greeting.set(config.get("app.greeting").asString().orElse("Ciao"));
}
/**
* A service registers itself by updating the routing rules.
* @param rules the routing rules.
*/
@Override
public void update(Routing.Rules rules) {
rules
.get("/", this::getDefaultMessageHandler)
.get("/{name}", this::getMessageHandler)
.put("/greeting", this::updateGreetingHandler);
}
/**
* Return a wordly greeting message.
* @param request the server request
* @param response the server response
*/
private void getDefaultMessageHandler(ServerRequest request,
ServerResponse response) {
sendResponse(response, "World");
}
/**
* Return a greeting message using the name that was provided.
* @param request the server request
* @param response the server response
*/
private void getMessageHandler(ServerRequest request,
ServerResponse response) {
String name = request.path().param("name");
sendResponse(response, name);
}
private void sendResponse(ServerResponse response, String name) {
String msg = String.format("%s %s!", greeting.get(), name);
JsonObject returnObject = JSON.createObjectBuilder()
.add("message", msg)
.build();
response.send(returnObject);
}
private static <T> T processErrors(Throwable ex, ServerRequest request, ServerResponse response) {
if (ex.getCause() instanceof JsonException){
LOGGER.log(Level.FINE, "Invalid JSON", ex);
JsonObject jsonErrorObject = JSON.createObjectBuilder()
.add("error", "Invalid JSON")
.build();
response.status(Http.Status.BAD_REQUEST_400).send(jsonErrorObject);
} else {
LOGGER.log(Level.FINE, "Internal error", ex);
JsonObject jsonErrorObject = JSON.createObjectBuilder()
.add("error", "Internal error")
.build();
response.status(Http.Status.INTERNAL_SERVER_ERROR_500).send(jsonErrorObject);
}
return null;
}
private void updateGreetingFromJson(JsonObject jo, ServerResponse response) {
if (!jo.containsKey("greeting")) {
JsonObject jsonErrorObject = JSON.createObjectBuilder()
.add("error", "No greeting provided")
.build();
response.status(Http.Status.BAD_REQUEST_400)
.send(jsonErrorObject);
return;
}
greeting.set(jo.getString("greeting"));
response.status(Http.Status.NO_CONTENT_204).send();
}
/**
* Set the greeting to use in future messages.
* @param request the server request
* @param response the server response
*/
private void updateGreetingHandler(ServerRequest request,
ServerResponse response) {
request.content().as(JsonObject.class)
.thenAccept(jo -> updateGreetingFromJson(jo, response))
.exceptionally(ex -> processErrors(ex, request, response));
}
}

View File

@@ -0,0 +1,121 @@
/*
* Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
*
* 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 io.helidon.examples.quickstart.se;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.LogManager;
import io.helidon.config.Config;
import io.helidon.health.HealthSupport;
import io.helidon.health.checks.HealthChecks;
import io.helidon.media.jsonp.server.JsonSupport;
import io.helidon.metrics.MetricsSupport;
import io.helidon.webserver.Routing;
import io.helidon.webserver.ServerConfiguration;
import io.helidon.webserver.WebServer;
/**
* The application main class.
*/
public final class Main {
/**
* Cannot be instantiated.
*/
private Main() { }
/**
* Application main entry point.
* @param args command line arguments.
* @throws IOException if there are problems reading logging properties
*/
public static void main(final String[] args) throws IOException {
startServer();
}
/**
* Start the server.
* @return the created {@link WebServer} instance
* @throws IOException if there are problems reading logging properties
*/
static WebServer startServer() throws IOException {
// load logging configuration
setupLogging();
// By default this will pick up application.yaml from the classpath
Config config = Config.create();
// Get webserver config from the "server" section of application.yaml
ServerConfiguration serverConfig =
ServerConfiguration.create(config.get("server"));
WebServer server = WebServer.create(serverConfig, createRouting(config));
// Try to start the server. If successful, print some info and arrange to
// print a message at shutdown. If unsuccessful, print the exception.
server.start()
.thenAccept(ws -> {
System.out.println(
"WEB server is up! http://localhost:" + ws.port() + "/greet");
ws.whenShutdown().thenRun(()
-> System.out.println("WEB server is DOWN. Good bye!"));
})
.exceptionally(t -> {
System.err.println("Startup failed: " + t.getMessage());
t.printStackTrace(System.err);
return null;
});
// Server threads are not daemon. No need to block. Just react.
return server;
}
/**
* Creates new {@link Routing}.
*
* @return routing configured with JSON support, a health check, and a service
* @param config configuration of this server
*/
private static Routing createRouting(Config config) {
MetricsSupport metrics = MetricsSupport.create();
GreetService greetService = new GreetService(config);
HealthSupport health = HealthSupport.builder()
.addLiveness(HealthChecks.healthChecks()) // Adds a convenient set of checks
.build();
return Routing.builder()
.register(JsonSupport.create())
.register(health) // Health at "/health"
.register(metrics) // Metrics at "/metrics"
.register("/greet", greetService)
.build();
}
/**
* Configure logging from logging.properties file.
*/
private static void setupLogging() throws IOException {
try (InputStream is = Main.class.getResourceAsStream("/logging.properties")) {
LogManager.getLogManager().readConfiguration(is);
}
}
}

View File

@@ -0,0 +1,24 @@
/*
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
*
* 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.
*/
/**
* Quickstart demo application
* <p>
* Start with {@link io.helidon.examples.quickstart.se.Main} class.
*
* @see io.helidon.examples.quickstart.se.Main
*/
package io.helidon.examples.quickstart.se;

View File

@@ -0,0 +1,17 @@
#
# Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
#
# 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.
#
Args=-H:ReflectionConfigurationResources=${.}/helidon-example-reflection-config.json

View File

@@ -0,0 +1,26 @@
#
# Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
#
# 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.
#
app:
greeting: "Hello"
server:
port: 8080
host: 0.0.0.0
# experimental:
# http2:
# enable: true
# max-content-length: 16384

View File

@@ -0,0 +1,37 @@
#
# Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
#
# 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.
#
# Example Logging Configuration File
# For more information see $JAVA_HOME/jre/lib/logging.properties
# Send messages to the console
handlers=java.util.logging.ConsoleHandler
# Global default logging level. Can be overriden by specific handlers and loggers
.level=INFO
# Helidon Web Server has a custom log formatter that extends SimpleFormatter.
# It replaces "!thread!" with the current thread name
java.util.logging.ConsoleHandler.level=INFO
java.util.logging.ConsoleHandler.formatter=io.helidon.webserver.WebServerLogFormatter
java.util.logging.SimpleFormatter.format=%1$tY.%1$tm.%1$td %1$tH:%1$tM:%1$tS %4$s %3$s !thread!: %5$s%6$s%n
#Component specific log levels
#io.helidon.webserver.level=INFO
#io.helidon.config.level=INFO
#io.helidon.security.level=INFO
#io.helidon.common.level=INFO
#io.netty.level=INFO

View File

@@ -0,0 +1,114 @@
/*
* Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
*
* 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 io.helidon.examples.quickstart.se;
import java.io.OutputStream;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
import java.net.URL;
import java.net.HttpURLConnection;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonReaderFactory;
import io.helidon.webserver.WebServer;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class MainTest {
private static WebServer webServer;
private static final JsonReaderFactory JSON = Json.createReaderFactory(Collections.emptyMap());
@BeforeAll
public static void startTheServer() throws Exception {
webServer = Main.startServer();
long timeout = 2000; // 2 seconds should be enough to start the server
long now = System.currentTimeMillis();
while (!webServer.isRunning()) {
Thread.sleep(100);
if ((System.currentTimeMillis() - now) > timeout) {
Assertions.fail("Failed to start webserver");
}
}
}
@AfterAll
public static void stopServer() throws Exception {
if (webServer != null) {
webServer.shutdown()
.toCompletableFuture()
.get(10, TimeUnit.SECONDS);
}
}
@Test
public void testHelloWorld() throws Exception {
HttpURLConnection conn;
conn = getURLConnection("GET","/greet");
Assertions.assertEquals(200, conn.getResponseCode(), "HTTP response1");
JsonReader jsonReader = JSON.createReader(conn.getInputStream());
JsonObject jsonObject = jsonReader.readObject();
Assertions.assertEquals("Hello World!", jsonObject.getString("message"),
"default message");
conn = getURLConnection("GET", "/greet/Joe");
Assertions.assertEquals(200, conn.getResponseCode(), "HTTP response2");
jsonReader = JSON.createReader(conn.getInputStream());
jsonObject = jsonReader.readObject();
Assertions.assertEquals("Hello Joe!", jsonObject.getString("message"),
"hello Joe message");
conn = getURLConnection("PUT", "/greet/greeting");
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
os.write("{\"greeting\" : \"Hola\"}".getBytes());
os.close();
Assertions.assertEquals(204, conn.getResponseCode(), "HTTP response3");
conn = getURLConnection("GET", "/greet/Jose");
Assertions.assertEquals(200, conn.getResponseCode(), "HTTP response4");
jsonReader = JSON.createReader(conn.getInputStream());
jsonObject = jsonReader.readObject();
Assertions.assertEquals("Hola Jose!", jsonObject.getString("message"),
"hola Jose message");
conn = getURLConnection("GET", "/health");
Assertions.assertEquals(200, conn.getResponseCode(), "HTTP response2");
conn = getURLConnection("GET", "/metrics");
Assertions.assertEquals(200, conn.getResponseCode(), "HTTP response2");
}
private HttpURLConnection getURLConnection(String method, String path) throws Exception {
URL url = new URL("http://localhost:" + webServer.port() + path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(method);
conn.setRequestProperty("Accept", "application/json");
System.out.println("Connecting: " + method + " " + url);
return conn;
}
}

View File

@@ -15,7 +15,10 @@
See the License for the specific language governing permissions and
limitations under the License.
--><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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-->
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.helidon.examples</groupId>
@@ -30,5 +33,7 @@
<modules>
<module>helidon-quickstart-se</module>
<module>helidon-quickstart-mp</module>
<module>helidon-standalone-quickstart-mp</module>
<module>helidon-standalone-quickstart-se</module>
</modules>
</project>

View File

@@ -22,9 +22,9 @@
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.helidon</groupId>
<artifactId>helidon-3rdparty</artifactId>
<artifactId>helidon-dependencies</artifactId>
<version>1.3.1-SNAPSHOT</version>
<relativePath>./3rdparty/pom.xml</relativePath>
<relativePath>./dependencies/pom.xml</relativePath>
</parent>
<artifactId>helidon-project</artifactId>
<packaging>pom</packaging>
@@ -144,7 +144,7 @@
<modules>
<module>parent</module>
<module>3rdparty</module>
<module>dependencies</module>
<module>bom</module>
<module>applications</module>
<module>common</module>