Skip to content

update Javalin example #113

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

Merged
merged 3 commits into from
Nov 1, 2024
Merged
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
27 changes: 4 additions & 23 deletions javalin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,12 @@
<dependency>
<groupId>io.javalin</groupId>
<artifactId>javalin</artifactId>
<version>3.7.0</version>
<version>6.3.0</version>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>

<dependency>
<groupId>org.eclipse.jetty.http2</groupId>
<artifactId>http2-server</artifactId>
<version>${jettyVersion}</version>
</dependency>

<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-alpn-conscrypt-server</artifactId>
<version>${jettyVersion}</version>
</dependency>

<dependency>
<groupId>org.conscrypt</groupId>
<artifactId>conscrypt-openjdk-uber</artifactId>
<version>2.4.0</version>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.16</version>
</dependency>


Expand Down
91 changes: 19 additions & 72 deletions javalin/src/main/java/com/networknt/example/ExampleApplication.java
Original file line number Diff line number Diff line change
@@ -1,88 +1,35 @@
package com.networknt.example;

import io.javalin.Javalin;
import io.javalin.http.Context;
import org.eclipse.jetty.alpn.server.ALPNServerConnectionFactory;
import org.eclipse.jetty.http2.HTTP2Cipher;
import org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.SecureRequestCustomizer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.SslConnectionFactory;
import org.eclipse.jetty.util.ssl.SslContextFactory;

public class ExampleApplication {

public static void main(String[] args) {
Javalin app = Javalin.create(config -> {
// Manual Jetty configuration required for SSL/TLS/HTTP2
config.server(ExampleApplication::createServer);
}).start();
app.get("/", ExampleApplication::helloWorld);
}

public static void helloWorld(Context ctx) {
ctx.result("Hello world!");
}

private static Server createServer() {
Server server = new Server();

// HTTP
ServerConnector connector = new ServerConnector(server);
connector.setPort(8080);
server.addConnector(connector);

// HTTPS
ServerConnector sslConnector = new ServerConnector(server, getSslContextFactory());
sslConnector.setPort(8443);
server.addConnector(sslConnector);


// HTTP/2
HttpConfiguration httpsConfig = createHttpsConfig();

SslContextFactory sslContextFactory = getSslContextFactory();
sslContextFactory.setCipherComparator(HTTP2Cipher.COMPARATOR);
sslContextFactory.setProvider("Conscrypt");

HttpConnectionFactory httpConnectionFactory = new HttpConnectionFactory(httpsConfig);

HTTP2ServerConnectionFactory h2 = new HTTP2ServerConnectionFactory(httpsConfig);
ALPNServerConnectionFactory alpn = new ALPNServerConnectionFactory();
alpn.setDefaultProtocol(httpConnectionFactory.getProtocol());

SslConnectionFactory ssl = new SslConnectionFactory(sslContextFactory, alpn.getProtocol());

ServerConnector http2Connector = new ServerConnector(
server,
ssl,
alpn,
h2,
httpConnectionFactory
);
http2Connector.setPort(9443);
server.addConnector(http2Connector);

return server;
}
// Get example from https://github.com/javalin/javalin/blob/master/javalin/src/test/java/io/javalin/examples/HelloWorldSecure.java#L22-L30
Javalin.create(config -> {
config.jetty.addConnector((server, httpConfiguration) -> {
ServerConnector sslConnector = new ServerConnector(server, getSslContextFactory());
sslConnector.setPort(443);
return sslConnector;
});
config.jetty.addConnector((server, httpConfiguration) -> {
ServerConnector connector = new ServerConnector(server);
connector.setPort(80);
return connector;
});

})
.start()
.get("/", ctx -> ctx.result("Hello World"));

private static HttpConfiguration createHttpsConfig() {
HttpConfiguration httpConfig = new HttpConfiguration();
httpConfig.setSendXPoweredBy(false);
httpConfig.setSendServerVersion(false);
httpConfig.setSecureScheme("https");
httpConfig.addCustomizer(new SecureRequestCustomizer());
return httpConfig;
}

private static SslContextFactory getSslContextFactory() {
SslContextFactory sslContextFactory = new SslContextFactory.Server();
sslContextFactory.setKeyStorePath(ExampleApplication.class.getResource("/server.keystore").toExternalForm());
private static SslContextFactory.Server getSslContextFactory() {
SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
sslContextFactory.setKeyStorePath(ExampleApplication.class.getResource("/keystore.jks").toExternalForm());
sslContextFactory.setKeyStorePassword("password");
sslContextFactory.setUseCipherSuitesOrder(true);
return sslContextFactory;
}
}
5 changes: 5 additions & 0 deletions quarkus/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*
!target/*-runner
!target/*-runner.jar
!target/lib/*
!target/quarkus-app/*
45 changes: 45 additions & 0 deletions quarkus/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#Maven
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
release.properties
.flattened-pom.xml

# Eclipse
.project
.classpath
.settings/
bin/

# IntelliJ
.idea
*.ipr
*.iml
*.iws

# NetBeans
nb-configuration.xml

# Visual Studio Code
.vscode
.factorypath

# OSX
.DS_Store

# Vim
*.swp
*.swo

# patch
*.orig
*.rej

# Local environment
.env

# Plugin directory
/.quarkus/cli/plugins/
# TLS Certificates
.certs/
66 changes: 66 additions & 0 deletions quarkus/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# code-with-quarkus

This project uses Quarkus, the Supersonic Subatomic Java Framework.

If you want to learn more about Quarkus, please visit its website: <https://quarkus.io/>.

## Running the application in dev mode

You can run your application in dev mode that enables live coding using:

```shell script
./mvnw compile quarkus:dev
```

> **_NOTE:_** Quarkus now ships with a Dev UI, which is available in dev mode only at <http://localhost:8080/q/dev/>.

## Packaging and running the application

The application can be packaged using:

```shell script
./mvnw package
```

It produces the `quarkus-run.jar` file in the `target/quarkus-app/` directory.
Be aware that it’s not an _über-jar_ as the dependencies are copied into the `target/quarkus-app/lib/` directory.

The application is now runnable using `java -jar target/quarkus-app/quarkus-run.jar`.

If you want to build an _über-jar_, execute the following command:

```shell script
./mvnw package -Dquarkus.package.jar.type=uber-jar
```

The application, packaged as an _über-jar_, is now runnable using `java -jar target/*-runner.jar`.

## Creating a native executable

You can create a native executable using:

```shell script
./mvnw package -Dnative
```

Or, if you don't have GraalVM installed, you can run the native executable build in a container using:

```shell script
./mvnw package -Dnative -Dquarkus.native.container-build=true
```

You can then execute your native executable with: `./target/code-with-quarkus-1.0.0-SNAPSHOT-runner`

If you want to learn more about building native executables, please consult <https://quarkus.io/guides/maven-tooling>.

## Related Guides

- RESTEasy Classic ([guide](https://quarkus.io/guides/resteasy)): REST endpoint framework implementing Jakarta REST and more

## Provided Code

### RESTEasy JAX-RS

Easily start your RESTful Web Services

[Related guide section...](https://quarkus.io/guides/getting-started#the-jax-rs-resources)
Loading