Skip to content

Commit 192541f

Browse files
committed
javalin sample
1 parent 0d71d2a commit 192541f

File tree

13 files changed

+176
-0
lines changed

13 files changed

+176
-0
lines changed

samples/javalin/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Javalin Sample
2+
3+
This sample demonstrates how to deploy a very basic Javalin sample with Defang. The sample simply outputs "Defang x Javalin" on the webpage.
4+
5+
## Prerequisites
6+
7+
1. Download [Defang CLI](https://github.com/DefangLabs/defang)
8+
2. (Optional) If you are using [Defang BYOC](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html) authenticated with your AWS account
9+
3. (Optional - for local development) [Docker CLI](https://docs.docker.com/engine/install/)
10+
11+
## Deploying
12+
13+
1. Open the terminal and type `defang login`
14+
2. Type `defang compose up` in the CLI.
15+
3. Your app will be running within a few minutes.
16+
17+
## Development
18+
19+
For development, we use a local container. This can be seen in the compose.yaml document. To make changes to this sample, make sure that you have `openjdk@17` and `maven` installed. To run the sample locally after cloning the repository, you can run it on Docker by using the following command:
20+
21+
1. Navigate to the `app` directory and build the project:
22+
23+
```bash
24+
cd app
25+
mvn clean package
26+
cd ../
27+
```
28+
29+
2. From the project root directory, run:
30+
31+
```bash
32+
docker compose up --build
33+
```
34+
35+
---
36+
37+
Title: Javalin
38+
39+
Short Description: A short hello world application demonstrating how to deploy Javalin onto defang.
40+
41+
Tags: Javalin & Java & Maven
42+
43+
Languages: java, javalin

samples/javalin/app/Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Use a base image with OpenJDK
2+
FROM openjdk:17-jdk-slim
3+
4+
# Set the working directory
5+
WORKDIR /app
6+
7+
# Copy the compiled Java classes and resources to the container
8+
COPY target/defang-javalin-1.0-SNAPSHOT.jar /app/app.jar
9+
10+
# Expose the port Javalin will run on
11+
EXPOSE 7000
12+
13+
# Command to run the Javalin application
14+
CMD ["java", "-jar", "/app/app.jar"]

samples/javalin/app/pom.xml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://www.maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.defang</groupId>
7+
<artifactId>defang-javalin</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
10+
<dependencies>
11+
<dependency>
12+
<groupId>io.javalin</groupId>
13+
<artifactId>javalin</artifactId>
14+
<version>4.6.1</version>
15+
</dependency>
16+
<dependency>
17+
<groupId>org.slf4j</groupId>
18+
<artifactId>slf4j-simple</artifactId>
19+
<version>1.7.31</version>
20+
</dependency>
21+
</dependencies>
22+
23+
<build>
24+
<outputDirectory>${project.basedir}/target</outputDirectory>
25+
<resources>
26+
<resource>
27+
<directory>src/main/resources</directory>
28+
<includes>
29+
<include>**/*</include>
30+
</includes>
31+
</resource>
32+
</resources>
33+
<plugins>
34+
<plugin>
35+
<groupId>org.apache.maven.plugins</groupId>
36+
<artifactId>maven-compiler-plugin</artifactId>
37+
<version>3.8.1</version>
38+
<configuration>
39+
<source>17</source>
40+
<target>17</target>
41+
</configuration>
42+
</plugin>
43+
<plugin>
44+
<groupId>org.apache.maven.plugins</groupId>
45+
<artifactId>maven-shade-plugin</artifactId>
46+
<version>3.2.4</version>
47+
<executions>
48+
<execution>
49+
<phase>package</phase>
50+
<goals>
51+
<goal>shade</goal>
52+
</goals>
53+
<configuration>
54+
<createDependencyReducedPom>false</createDependencyReducedPom>
55+
<transformers>
56+
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
57+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
58+
<mainClass>App</mainClass>
59+
</transformer>
60+
</transformers>
61+
</configuration>
62+
</execution>
63+
</executions>
64+
</plugin>
65+
</plugins>
66+
</build>
67+
</project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import io.javalin.Javalin;
2+
3+
public class App {
4+
public static void main(String[] args) {
5+
Javalin app = Javalin.create(config -> {
6+
config.addStaticFiles("/public", io.javalin.http.staticfiles.Location.CLASSPATH);
7+
}).start(7000);
8+
9+
app.get("/", ctx -> {
10+
ctx.contentType("text/html");
11+
ctx.result(App.class.getResourceAsStream("/public/index.html"));
12+
});
13+
}
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Defang x Javalin</title>
7+
</head>
8+
<body>
9+
<h1>Defang x Javalin</h1>
10+
</body>
11+
</html>
2.14 KB
Binary file not shown.
5.11 MB
Binary file not shown.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
artifactId=defang-javalin
2+
groupId=com.defang
3+
version=1.0-SNAPSHOT
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
App.class
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/Users/chris/Documents/GitHub/samples/samples/javalin/app/src/main/java/App.java

0 commit comments

Comments
 (0)