Skip to content

Commit e92edc4

Browse files
authored
Merge pull request #148 from DefangLabs/82-javalin-sample
javalin sample
2 parents 77d65de + 74d1fcd commit e92edc4

File tree

11 files changed

+266
-0
lines changed

11 files changed

+266
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM mcr.microsoft.com/devcontainers/java:11-bookworm
2+
3+
# Install Maven
4+
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
5+
&& apt-get -y install --no-install-recommends maven
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"build": {
3+
"dockerfile": "Dockerfile",
4+
"context": ".."
5+
},
6+
"features": {
7+
"ghcr.io/defanglabs/devcontainer-feature/defang-cli:1.0.4": {},
8+
"ghcr.io/devcontainers/features/docker-in-docker:2": {}
9+
}
10+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Deploy
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
deploy:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
contents: read
13+
id-token: write
14+
15+
steps:
16+
- name: Checkout Repo
17+
uses: actions/checkout@v4
18+
19+
- name: Deploy
20+
uses: DefangLabs/[email protected]

samples/javalin/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Javalin Sample
2+
3+
[1-click deploy](https://portal.defang.dev/redirect?url=https%3A%2F%2Fgithub.com%2Fnew%3Ftemplate_name%3Dsample-javalin-template%26template_owner%3DDefangSamples)
4+
5+
This sample demonstrates how to deploy a very basic Javalin sample with Defang. The sample simply outputs "Defang x Javalin" on the webpage.
6+
7+
## Prerequisites
8+
9+
1. Download [Defang CLI](https://github.com/DefangLabs/defang)
10+
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
11+
3. (Optional - for local development) [Docker CLI](https://docs.docker.com/engine/install/)
12+
13+
## Deploying
14+
15+
1. Open the terminal and type `defang login`
16+
2. Type `defang compose up` in the CLI.
17+
3. Your app will be running within a few minutes.
18+
19+
## Development
20+
21+
For development, we use a local container. This can be seen in the compose.yaml file. To run the sample locally use the following command:
22+
23+
```bash
24+
docker compose up --build
25+
```
26+
27+
---
28+
29+
Title: Javalin
30+
31+
Short Description: A short hello world application demonstrating how to deploy Javalin onto defang.
32+
33+
Tags: Javalin, Java, Maven
34+
35+
Languages: java

samples/javalin/app/.dockerignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Compiled class files
2+
*.class
3+
4+
# Log files
5+
*.log
6+
7+
# BlueJ files
8+
*.ctxt
9+
10+
# Mobile Tools for Java (J2ME)
11+
.mtj.tmp/
12+
13+
# Package Files #
14+
*.jar
15+
*.war
16+
*.ear
17+
18+
# IntelliJ IDEA files
19+
.idea/
20+
*.iml
21+
22+
# VS Code files
23+
.vscode/
24+
25+
# macOS Finder metadata
26+
.DS_Store
27+
28+
# Maven target directory
29+
target/
30+
31+
# Database files
32+
*.db
33+
*.sqlite
34+
*.sqlite3

samples/javalin/app/.gitignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Compiled class files
2+
*.class
3+
4+
# Log files
5+
*.log
6+
7+
# BlueJ files
8+
*.ctxt
9+
10+
# Mobile Tools for Java (J2ME)
11+
.mtj.tmp/
12+
13+
# Package Files #
14+
*.jar
15+
*.war
16+
*.ear
17+
18+
# IntelliJ IDEA files
19+
.idea/
20+
*.iml
21+
22+
# VS Code files
23+
.vscode/
24+
25+
# macOS Finder metadata
26+
.DS_Store
27+
28+
# Maven target directory
29+
target/
30+
31+
# Database files
32+
*.db
33+
*.sqlite
34+
*.sqlite3

samples/javalin/app/Dockerfile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Stage 1: Build the application
2+
FROM maven:3.8.4-openjdk-17-slim AS build
3+
4+
# Set the working directory
5+
WORKDIR /app
6+
7+
# Copy the pom.xml and download dependencies
8+
COPY pom.xml .
9+
RUN mvn dependency:go-offline
10+
11+
# Copy the source code and build the application
12+
COPY src ./src
13+
RUN mvn clean package
14+
15+
# Stage 2: Create the runtime image
16+
FROM openjdk:17-jdk-slim
17+
18+
# Set the working directory
19+
WORKDIR /app
20+
21+
# Copy the built jar file from the build stage
22+
COPY --from=build /app/target/defang-javalin-1.0.0.jar /app/app.jar
23+
24+
# Expose the port Javalin will run on
25+
EXPOSE 7000
26+
27+
# Command to run the Javalin application
28+
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>io.defang</groupId>
7+
<artifactId>defang-javalin</artifactId>
8+
<version>1.0.0</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>

0 commit comments

Comments
 (0)