KevanSoon commited on
Commit
29b6132
·
1 Parent(s): 514e1e9

reset space

Browse files
Files changed (4) hide show
  1. DemoApplication.java +3 -9
  2. Dockerfile +19 -34
  3. User.java +0 -9
  4. pom.xml +0 -56
DemoApplication.java CHANGED
@@ -1,18 +1,12 @@
1
  import io.javalin.Javalin;
2
- import com.google.gson.Gson;
3
 
4
  public class DemoApplication {
5
  public static void main(String[] args) {
6
- int port = 8080;
7
- Javalin app = Javalin.create().start(port);
8
 
9
- Gson gson = new Gson();
10
 
11
  app.get("/", ctx -> ctx.result("Hello from Javalin on Hugging Face!"));
12
-
13
- app.get("/user", ctx -> {
14
- User user = new User("Kevan", 21);
15
- ctx.result(gson.toJson(user));
16
- });
17
  }
18
  }
 
1
  import io.javalin.Javalin;
 
2
 
3
  public class DemoApplication {
4
  public static void main(String[] args) {
5
+ int port = Integer.parseInt(System.getenv().getOrDefault("PORT", "7860"));
 
6
 
7
+ Javalin app = Javalin.create().start(port);
8
 
9
  app.get("/", ctx -> ctx.result("Hello from Javalin on Hugging Face!"));
10
+ app.get("/ping", ctx -> ctx.result("pong"));
 
 
 
 
11
  }
12
  }
Dockerfile CHANGED
@@ -1,41 +1,26 @@
1
- FROM eclipse-temurin:17-jdk AS build
2
 
3
  WORKDIR /app
4
 
5
- # Install Maven for dependency fetching
6
- RUN apt-get update && apt-get install -y maven && rm -rf /var/lib/apt/lists/*
 
 
 
 
 
 
 
7
 
8
- # Create minimal pom.xml
9
- RUN echo '<project xmlns="http://maven.apache.org/POM/4.0.0" \
10
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" \
11
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 \
12
- http://maven.apache.org/xsd/maven-4.0.0.xsd"> \
13
- <modelVersion>4.0.0</modelVersion> \
14
- <groupId>com.example</groupId> \
15
- <artifactId>javalin-demo</artifactId> \
16
- <version>1.0</version> \
17
- <dependencies> \
18
- <dependency><groupId>io.javalin</groupId><artifactId>javalin</artifactId><version>5.6.1</version></dependency> \
19
- <dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>2.10.1</version></dependency> \
20
- </dependencies> \
21
- </project>' > pom.xml
22
 
23
- # Download dependencies into libs/
24
- RUN mvn dependency:copy-dependencies -DoutputDirectory=libs
25
 
26
- # Copy sources
27
- COPY DemoApplication.java User.java ./
 
28
 
29
- # Compile including libs and current dir
30
- RUN javac -cp "libs/*:." DemoApplication.java User.java && \
31
- jar cfe app.jar DemoApplication *.class
32
-
33
- FROM eclipse-temurin:17-jre
34
- WORKDIR /app
35
-
36
- COPY --from=build /app/app.jar .
37
- COPY --from=build /app/libs ./libs
38
-
39
- EXPOSE 8080
40
-
41
- CMD ["java", "-cp", "app.jar:libs/*", "DemoApplication"]
 
1
+ FROM openjdk:17-jdk-slim
2
 
3
  WORKDIR /app
4
 
5
+ # Download Javalin + Jetty dependencies
6
+ RUN mkdir libs && \
7
+ curl -L -o libs/javalin.jar https://repo1.maven.org/maven2/io/javalin/javalin/5.6.1/javalin-5.6.1.jar && \
8
+ curl -L -o libs/jetty.jar https://repo1.maven.org/maven2/org/eclipse/jetty/jetty-server/11.0.15/jetty-server-11.0.15.jar && \
9
+ curl -L -o libs/jetty-util.jar https://repo1.maven.org/maven2/org/eclipse/jetty/jetty-util/11.0.15/jetty-util-11.0.15.jar && \
10
+ curl -L -o libs/jetty-http.jar https://repo1.maven.org/maven2/org/eclipse/jetty/jetty-http/11.0.15/jetty-http-11.0.15.jar && \
11
+ curl -L -o libs/jetty-io.jar https://repo1.maven.org/maven2/org/eclipse/jetty/jetty-io/11.0.15/jetty-io-11.0.15.jar && \
12
+ curl -L -o libs/slf4j.jar https://repo1.maven.org/maven2/org/slf4j/slf4j-simple/2.0.7/slf4j-simple-2.0.7.jar && \
13
+ curl -L -o libs/slf4j-api.jar https://repo1.maven.org/maven2/org/slf4j/slf4j-api/2.0.7/slf4j-api-2.0.7.jar
14
 
15
+ # Copy your Java source code
16
+ COPY DemoApplication.java /app/
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
+ # Compile the Java file with dependencies
19
+ RUN javac -cp "libs/*" DemoApplication.java
20
 
21
+ # Expose Hugging Face PORT
22
+ ENV PORT=7860
23
+ EXPOSE 7860
24
 
25
+ # Run the app
26
+ CMD ["java", "-cp", ".:libs/*", "DemoApplication"]
 
 
 
 
 
 
 
 
 
 
 
User.java DELETED
@@ -1,9 +0,0 @@
1
- public class User {
2
- private String name;
3
- private int age;
4
-
5
- public User(String name, int age) {
6
- this.name = name;
7
- this.age = age;
8
- }
9
- }
 
 
 
 
 
 
 
 
 
 
pom.xml DELETED
@@ -1,56 +0,0 @@
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
4
- http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
- <modelVersion>4.0.0</modelVersion>
6
- <groupId>com.example</groupId>
7
- <artifactId>javalin-demo</artifactId>
8
- <version>0.0.1-SNAPSHOT</version>
9
- <properties>
10
- <java.version>17</java.version>
11
- </properties>
12
-
13
- <dependencies>
14
- <!-- Javalin -->
15
- <dependency>
16
- <groupId>io.javalin</groupId>
17
- <artifactId>javalin-bundle</artifactId>
18
- <version>5.6.1</version>
19
- </dependency>
20
-
21
- <!-- JSON serialization -->
22
- <dependency>
23
- <groupId>com.fasterxml.jackson.core</groupId>
24
- <artifactId>jackson-databind</artifactId>
25
- <version>2.16.4</version>
26
- </dependency>
27
- </dependencies>
28
-
29
- <build>
30
- <plugins>
31
- <!-- Package JAR -->
32
- <plugin>
33
- <groupId>org.apache.maven.plugins</groupId>
34
- <artifactId>maven-compiler-plugin</artifactId>
35
- <version>3.11.0</version>
36
- <configuration>
37
- <source>${java.version}</source>
38
- <target>${java.version}</target>
39
- </configuration>
40
- </plugin>
41
-
42
- <plugin>
43
- <groupId>org.apache.maven.plugins</groupId>
44
- <artifactId>maven-jar-plugin</artifactId>
45
- <version>3.3.0</version>
46
- <configuration>
47
- <archive>
48
- <manifest>
49
- <mainClass>DemoApplication</mainClass>
50
- </manifest>
51
- </archive>
52
- </configuration>
53
- </plugin>
54
- </plugins>
55
- </build>
56
- </project>