Skip to content

Commit 8b3e174

Browse files
authored
update Javalin example (#113)
* update Javalin example * add HTTS * add Quarkus example
1 parent c7fee3c commit 8b3e174

File tree

16 files changed

+1068
-95
lines changed

16 files changed

+1068
-95
lines changed

javalin/pom.xml

+4-23
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,12 @@
1818
<dependency>
1919
<groupId>io.javalin</groupId>
2020
<artifactId>javalin</artifactId>
21-
<version>3.7.0</version>
21+
<version>6.3.0</version>
2222
</dependency>
23-
24-
<dependency>
25-
<groupId>ch.qos.logback</groupId>
26-
<artifactId>logback-classic</artifactId>
27-
<version>1.2.3</version>
28-
</dependency>
29-
30-
<dependency>
31-
<groupId>org.eclipse.jetty.http2</groupId>
32-
<artifactId>http2-server</artifactId>
33-
<version>${jettyVersion}</version>
34-
</dependency>
35-
36-
<dependency>
37-
<groupId>org.eclipse.jetty</groupId>
38-
<artifactId>jetty-alpn-conscrypt-server</artifactId>
39-
<version>${jettyVersion}</version>
40-
</dependency>
41-
4223
<dependency>
43-
<groupId>org.conscrypt</groupId>
44-
<artifactId>conscrypt-openjdk-uber</artifactId>
45-
<version>2.4.0</version>
24+
<groupId>org.slf4j</groupId>
25+
<artifactId>slf4j-simple</artifactId>
26+
<version>2.0.16</version>
4627
</dependency>
4728

4829

Original file line numberDiff line numberDiff line change
@@ -1,88 +1,35 @@
11
package com.networknt.example;
22

33
import io.javalin.Javalin;
4-
import io.javalin.http.Context;
5-
import org.eclipse.jetty.alpn.server.ALPNServerConnectionFactory;
6-
import org.eclipse.jetty.http2.HTTP2Cipher;
7-
import org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory;
8-
import org.eclipse.jetty.server.HttpConfiguration;
9-
import org.eclipse.jetty.server.HttpConnectionFactory;
10-
import org.eclipse.jetty.server.SecureRequestCustomizer;
11-
import org.eclipse.jetty.server.Server;
124
import org.eclipse.jetty.server.ServerConnector;
13-
import org.eclipse.jetty.server.SslConnectionFactory;
145
import org.eclipse.jetty.util.ssl.SslContextFactory;
156

167
public class ExampleApplication {
178

189
public static void main(String[] args) {
19-
Javalin app = Javalin.create(config -> {
20-
// Manual Jetty configuration required for SSL/TLS/HTTP2
21-
config.server(ExampleApplication::createServer);
22-
}).start();
23-
app.get("/", ExampleApplication::helloWorld);
24-
}
25-
26-
public static void helloWorld(Context ctx) {
27-
ctx.result("Hello world!");
28-
}
29-
30-
private static Server createServer() {
31-
Server server = new Server();
32-
33-
// HTTP
34-
ServerConnector connector = new ServerConnector(server);
35-
connector.setPort(8080);
36-
server.addConnector(connector);
37-
38-
// HTTPS
39-
ServerConnector sslConnector = new ServerConnector(server, getSslContextFactory());
40-
sslConnector.setPort(8443);
41-
server.addConnector(sslConnector);
42-
43-
44-
// HTTP/2
45-
HttpConfiguration httpsConfig = createHttpsConfig();
46-
47-
SslContextFactory sslContextFactory = getSslContextFactory();
48-
sslContextFactory.setCipherComparator(HTTP2Cipher.COMPARATOR);
49-
sslContextFactory.setProvider("Conscrypt");
50-
51-
HttpConnectionFactory httpConnectionFactory = new HttpConnectionFactory(httpsConfig);
52-
53-
HTTP2ServerConnectionFactory h2 = new HTTP2ServerConnectionFactory(httpsConfig);
54-
ALPNServerConnectionFactory alpn = new ALPNServerConnectionFactory();
55-
alpn.setDefaultProtocol(httpConnectionFactory.getProtocol());
56-
57-
SslConnectionFactory ssl = new SslConnectionFactory(sslContextFactory, alpn.getProtocol());
58-
59-
ServerConnector http2Connector = new ServerConnector(
60-
server,
61-
ssl,
62-
alpn,
63-
h2,
64-
httpConnectionFactory
65-
);
66-
http2Connector.setPort(9443);
67-
server.addConnector(http2Connector);
68-
69-
return server;
70-
}
10+
// Get example from https://github.com/javalin/javalin/blob/master/javalin/src/test/java/io/javalin/examples/HelloWorldSecure.java#L22-L30
11+
Javalin.create(config -> {
12+
config.jetty.addConnector((server, httpConfiguration) -> {
13+
ServerConnector sslConnector = new ServerConnector(server, getSslContextFactory());
14+
sslConnector.setPort(443);
15+
return sslConnector;
16+
});
17+
config.jetty.addConnector((server, httpConfiguration) -> {
18+
ServerConnector connector = new ServerConnector(server);
19+
connector.setPort(80);
20+
return connector;
21+
});
22+
23+
})
24+
.start()
25+
.get("/", ctx -> ctx.result("Hello World"));
7126

72-
private static HttpConfiguration createHttpsConfig() {
73-
HttpConfiguration httpConfig = new HttpConfiguration();
74-
httpConfig.setSendXPoweredBy(false);
75-
httpConfig.setSendServerVersion(false);
76-
httpConfig.setSecureScheme("https");
77-
httpConfig.addCustomizer(new SecureRequestCustomizer());
78-
return httpConfig;
7927
}
8028

81-
private static SslContextFactory getSslContextFactory() {
82-
SslContextFactory sslContextFactory = new SslContextFactory.Server();
83-
sslContextFactory.setKeyStorePath(ExampleApplication.class.getResource("/server.keystore").toExternalForm());
29+
private static SslContextFactory.Server getSslContextFactory() {
30+
SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
31+
sslContextFactory.setKeyStorePath(ExampleApplication.class.getResource("/keystore.jks").toExternalForm());
8432
sslContextFactory.setKeyStorePassword("password");
85-
sslContextFactory.setUseCipherSuitesOrder(true);
8633
return sslContextFactory;
8734
}
8835
}

quarkus/.dockerignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*
2+
!target/*-runner
3+
!target/*-runner.jar
4+
!target/lib/*
5+
!target/quarkus-app/*

quarkus/.gitignore

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#Maven
2+
target/
3+
pom.xml.tag
4+
pom.xml.releaseBackup
5+
pom.xml.versionsBackup
6+
release.properties
7+
.flattened-pom.xml
8+
9+
# Eclipse
10+
.project
11+
.classpath
12+
.settings/
13+
bin/
14+
15+
# IntelliJ
16+
.idea
17+
*.ipr
18+
*.iml
19+
*.iws
20+
21+
# NetBeans
22+
nb-configuration.xml
23+
24+
# Visual Studio Code
25+
.vscode
26+
.factorypath
27+
28+
# OSX
29+
.DS_Store
30+
31+
# Vim
32+
*.swp
33+
*.swo
34+
35+
# patch
36+
*.orig
37+
*.rej
38+
39+
# Local environment
40+
.env
41+
42+
# Plugin directory
43+
/.quarkus/cli/plugins/
44+
# TLS Certificates
45+
.certs/

quarkus/README.md

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# code-with-quarkus
2+
3+
This project uses Quarkus, the Supersonic Subatomic Java Framework.
4+
5+
If you want to learn more about Quarkus, please visit its website: <https://quarkus.io/>.
6+
7+
## Running the application in dev mode
8+
9+
You can run your application in dev mode that enables live coding using:
10+
11+
```shell script
12+
./mvnw compile quarkus:dev
13+
```
14+
15+
> **_NOTE:_** Quarkus now ships with a Dev UI, which is available in dev mode only at <http://localhost:8080/q/dev/>.
16+
17+
## Packaging and running the application
18+
19+
The application can be packaged using:
20+
21+
```shell script
22+
./mvnw package
23+
```
24+
25+
It produces the `quarkus-run.jar` file in the `target/quarkus-app/` directory.
26+
Be aware that it’s not an _über-jar_ as the dependencies are copied into the `target/quarkus-app/lib/` directory.
27+
28+
The application is now runnable using `java -jar target/quarkus-app/quarkus-run.jar`.
29+
30+
If you want to build an _über-jar_, execute the following command:
31+
32+
```shell script
33+
./mvnw package -Dquarkus.package.jar.type=uber-jar
34+
```
35+
36+
The application, packaged as an _über-jar_, is now runnable using `java -jar target/*-runner.jar`.
37+
38+
## Creating a native executable
39+
40+
You can create a native executable using:
41+
42+
```shell script
43+
./mvnw package -Dnative
44+
```
45+
46+
Or, if you don't have GraalVM installed, you can run the native executable build in a container using:
47+
48+
```shell script
49+
./mvnw package -Dnative -Dquarkus.native.container-build=true
50+
```
51+
52+
You can then execute your native executable with: `./target/code-with-quarkus-1.0.0-SNAPSHOT-runner`
53+
54+
If you want to learn more about building native executables, please consult <https://quarkus.io/guides/maven-tooling>.
55+
56+
## Related Guides
57+
58+
- RESTEasy Classic ([guide](https://quarkus.io/guides/resteasy)): REST endpoint framework implementing Jakarta REST and more
59+
60+
## Provided Code
61+
62+
### RESTEasy JAX-RS
63+
64+
Easily start your RESTful Web Services
65+
66+
[Related guide section...](https://quarkus.io/guides/getting-started#the-jax-rs-resources)

0 commit comments

Comments
 (0)