Signed-off-by: Anca Iordache <anca.iordache@docker.com>pull/2/head
parent
49463cdfdf
commit
2919af7ea3
@ -0,0 +1,10 @@ |
||||
FROM maven:3.5-jdk-8-alpine AS build |
||||
COPY pom.xml . |
||||
RUN mvn --batch-mode dependency:resolve |
||||
COPY src/ src |
||||
RUN mvn --batch-mode clean compile assembly:single |
||||
|
||||
FROM openjdk:8-jre-alpine3.7 |
||||
EXPOSE 8080 |
||||
COPY --from=build target/app.jar /app.jar |
||||
CMD java -jar /app.jar |
@ -0,0 +1,59 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" |
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
<modelVersion>4.0.0</modelVersion> |
||||
|
||||
<groupId>com.company</groupId> |
||||
<artifactId>project</artifactId> |
||||
<version>1.0-SNAPSHOT</version> |
||||
|
||||
<build> |
||||
<plugins> |
||||
<plugin> |
||||
<groupId>org.apache.maven.plugins</groupId> |
||||
<artifactId>maven-compiler-plugin</artifactId> |
||||
<configuration> |
||||
<source>8</source> |
||||
<target>8</target> |
||||
</configuration> |
||||
</plugin> |
||||
<plugin> |
||||
<artifactId>maven-assembly-plugin</artifactId> |
||||
<configuration> |
||||
<archive> |
||||
<manifest> |
||||
<mainClass>App</mainClass> |
||||
</manifest> |
||||
</archive> |
||||
<descriptorRefs> |
||||
<descriptorRef>jar-with-dependencies</descriptorRef> |
||||
</descriptorRefs> |
||||
<finalName>app</finalName> |
||||
<appendAssemblyId>false</appendAssemblyId> |
||||
</configuration> |
||||
</plugin> |
||||
</plugins> |
||||
</build> |
||||
|
||||
<dependencies> |
||||
<dependency> |
||||
<groupId>com.sparkjava</groupId> |
||||
<artifactId>spark-core</artifactId> |
||||
<version>2.5</version> |
||||
</dependency> |
||||
|
||||
<dependency> |
||||
<groupId>mysql</groupId> |
||||
<artifactId>mysql-connector-java</artifactId> |
||||
<version>8.0.11</version> |
||||
</dependency> |
||||
<dependency> |
||||
<groupId>com.google.code.gson</groupId> |
||||
<artifactId>gson</artifactId> |
||||
<version>2.8.4</version> |
||||
</dependency> |
||||
|
||||
</dependencies> |
||||
|
||||
</project> |
@ -0,0 +1,78 @@ |
||||
import com.google.gson.Gson; |
||||
import com.mysql.cj.jdbc.exceptions.CommunicationsException; |
||||
|
||||
import static spark.Spark.*; |
||||
|
||||
import java.nio.file.Files; |
||||
import java.nio.file.Paths; |
||||
import java.sql.*; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
public class App { |
||||
|
||||
public static void main(String[] args) throws Exception { |
||||
Class.forName("com.mysql.cj.jdbc.Driver"); |
||||
|
||||
prepare(); |
||||
|
||||
port(8080); |
||||
|
||||
get("/", (req, res) -> new Gson().toJson(titles())); |
||||
} |
||||
|
||||
private static List<String> titles() { |
||||
try (Connection conn = connect()) { |
||||
List<String> titles = new ArrayList<>(); |
||||
try (Statement stmt = conn.createStatement()) { |
||||
try (ResultSet rs = stmt.executeQuery("SELECT title FROM blog")) { |
||||
while (rs.next()) { |
||||
titles.add(rs.getString("title")); |
||||
} |
||||
} |
||||
} |
||||
return titles; |
||||
} catch (Exception ex) { |
||||
return new ArrayList<>(); |
||||
} |
||||
} |
||||
|
||||
public static void prepare() throws Exception { |
||||
try (Connection conn = connect()) { |
||||
recreateTable(conn); |
||||
insertData(conn); |
||||
} |
||||
} |
||||
|
||||
private static void insertData(Connection conn) throws SQLException { |
||||
for (int i = 0; i < 5; i++) { |
||||
try (PreparedStatement stmt = conn.prepareStatement("INSERT INTO blog (title) VALUES (?);")) { |
||||
stmt.setString(1, "Blog post #" + i); |
||||
stmt.execute(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
private static void recreateTable(Connection conn) throws SQLException { |
||||
try (Statement stmt = conn.createStatement()) { |
||||
stmt.execute("DROP TABLE IF EXISTS blog"); |
||||
stmt.execute("CREATE TABLE IF NOT EXISTS blog (id int NOT NULL AUTO_INCREMENT, title varchar(255), PRIMARY KEY (id))"); |
||||
} |
||||
} |
||||
|
||||
|
||||
private static Connection connect() throws Exception { |
||||
for (int i = 0; i < 60; i++) { |
||||
try { |
||||
return DriverManager.getConnection("jdbc:mysql://db/example?useSSL=false", "root", Files.lines(Paths.get("/run/secrets/db-password")).findFirst().get()); |
||||
} catch (CommunicationsException ex) { |
||||
Thread.sleep(1000L); |
||||
continue; |
||||
} catch (Exception ex) { |
||||
throw ex; |
||||
} |
||||
} |
||||
throw new RuntimeException("Unable to connect to MySQL"); |
||||
} |
||||
} |
||||
|
@ -0,0 +1 @@ |
||||
db-vp2lf |
@ -0,0 +1,24 @@ |
||||
version: "3.7" |
||||
services: |
||||
backend: |
||||
build: backend |
||||
image: docker.io/docker/back |
||||
ports: |
||||
- 80:8080 |
||||
secrets: |
||||
- db-password |
||||
db: |
||||
environment: |
||||
MYSQL_DATABASE: example |
||||
MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db-password |
||||
image: mysql:5.7 |
||||
restart: always |
||||
secrets: |
||||
- db-password |
||||
volumes: |
||||
- db-data:/var/lib/mysql |
||||
volumes: |
||||
db-data: {} |
||||
secrets: |
||||
db-password: |
||||
file: db/password.txt |
Loading…
Reference in new issue