From c0e9148a94882c4afc30d391d6c8d48eb883ded3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Erik=20Hedenstr=C3=B6m?= <erik@hedenstroem.com>
Date: Mon, 21 Mar 2022 11:24:25 +0100
Subject: [PATCH] Added dropwizard sample

---
 .dockerignore                                 |   1 +
 .gitignore                                    |   1 -
 .gitlab-ci.yml                                |   2 +-
 .gitlab.dockerfile                            |   6 +
 Dockerfile                                    |  14 +-
 test/docker/docker-compose.yml                |  41 +++--
 test/docker/dropwizard/Dockerfile             |  22 +++
 test/docker/dropwizard/README.md              |  13 ++
 test/docker/dropwizard/config.yml             |   4 +
 test/docker/dropwizard/mvn.sh                 |   6 +
 test/docker/dropwizard/pom.xml                | 170 ++++++++++++++++++
 .../com/example/FaketimeDemoApplication.java  |  29 +++
 .../example/FaketimeDemoConfiguration.java    |  10 ++
 .../dropwizard/src/main/resources/banner.txt  |   6 +
 test/docker/java/Dockerfile                   |   3 +-
 15 files changed, 310 insertions(+), 18 deletions(-)
 create mode 100644 .dockerignore
 create mode 100644 .gitlab.dockerfile
 create mode 100644 test/docker/dropwizard/Dockerfile
 create mode 100644 test/docker/dropwizard/README.md
 create mode 100644 test/docker/dropwizard/config.yml
 create mode 100755 test/docker/dropwizard/mvn.sh
 create mode 100644 test/docker/dropwizard/pom.xml
 create mode 100644 test/docker/dropwizard/src/main/java/com/example/FaketimeDemoApplication.java
 create mode 100644 test/docker/dropwizard/src/main/java/com/example/FaketimeDemoConfiguration.java
 create mode 100644 test/docker/dropwizard/src/main/resources/banner.txt

diff --git a/.dockerignore b/.dockerignore
new file mode 100644
index 0000000..816131d
--- /dev/null
+++ b/.dockerignore
@@ -0,0 +1 @@
+test/docker
diff --git a/.gitignore b/.gitignore
index 60d377f..548a270 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,7 +4,6 @@
 *.dll
 *.so
 *.dylib
-main
 faketime-api
 .faketimerc
 
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 8e2a138..fc330f0 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -25,7 +25,7 @@ docker push:
     - /^\d+[.]\d+[.]\d+$/
   script:
     - |
-      docker build -t $REGISTRY/$CI_PROJECT_NAME .
+      docker build -f .gitlab.dockerfile -t $REGISTRY/$CI_PROJECT_NAME .
       if [ "$CI_BUILD_REF_NAME" != "main" ]; then
         docker tag $REGISTRY/$CI_PROJECT_NAME $REGISTRY/$CI_PROJECT_NAME:$CI_BUILD_REF_NAME
       fi
diff --git a/.gitlab.dockerfile b/.gitlab.dockerfile
new file mode 100644
index 0000000..2968e49
--- /dev/null
+++ b/.gitlab.dockerfile
@@ -0,0 +1,6 @@
+FROM scratch
+ADD faketime-api /usr/bin/faketime-api
+ENV HTTP_ADDR=:8080
+ENV FAKETIME_TIMESTAMP_FILE=/var/lib/faketime/faketimerc
+EXPOSE 8080
+ENTRYPOINT ["/usr/bin/faketime-api"]
diff --git a/Dockerfile b/Dockerfile
index cc30438..6a5fea5 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,5 +1,17 @@
+##
+## Build
+##
+FROM golang:1.17 AS golang
+COPY . /usr/src/faketime-api
+WORKDIR /usr/src/faketime-api
+RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -tags netgo -ldflags '-s -w -extldflags "-static"' -o faketime-api
+RUN ./upx --brute faketime-api
+##
+## Deploy
+##
 FROM scratch
-ADD faketime-api /usr/bin/faketime-api
+COPY --from=golang /usr/src/faketime-api/faketime-api /usr/bin/faketime-api
 ENV HTTP_ADDR=:8080
 ENV FAKETIME_TIMESTAMP_FILE=/var/lib/faketime/faketimerc
+EXPOSE 8080
 ENTRYPOINT ["/usr/bin/faketime-api"]
diff --git a/test/docker/docker-compose.yml b/test/docker/docker-compose.yml
index 3db6a01..954a166 100644
--- a/test/docker/docker-compose.yml
+++ b/test/docker/docker-compose.yml
@@ -14,17 +14,6 @@ services:
       - faketime:/var/lib/faketime
       - postgresql:/var/lib/postgresql
       - postgresql_data:/var/lib/postgresql/data
-  pgadmin:
-    image: dpage/pgadmin4
-    container_name: pgadmin
-    restart: unless-stopped
-    environment:
-      PGADMIN_DEFAULT_EMAIL: admin@admin.com
-      PGADMIN_DEFAULT_PASSWORD: password
-    volumes:
-      - pgadmin:/var/lib/pgadmin
-    ports:
-      - "5050:80"
   openjdk:
     image: openjdk:faketime
     container_name: openjdk
@@ -34,14 +23,40 @@ services:
       dockerfile: Dockerfile
     volumes:
       - faketime:/var/lib/faketime
+  dropwizard:
+    image: dropwizard:faketime
+    container_name: dropwizard
+    restart: unless-stopped
+    build: 
+      context: dropwizard
+      dockerfile: Dockerfile
+    volumes:
+      - faketime:/var/lib/faketime
+    ports:
+      - "8080:8080"
+      - "8081:8081"
   faketime-api:
-    image: europe-north1-docker.pkg.dev/ehedenst-gitlab/docker/faketime-api
+    image: faketime-api
     container_name: faketime-api
     restart: unless-stopped
+    build: 
+      context: ../..
+      dockerfile: Dockerfile
     volumes:
       - faketime:/var/lib/faketime
     ports:
-      - "8080:8080"
+      - "8082:8080"
+  pgadmin:
+    image: dpage/pgadmin4
+    container_name: pgadmin
+    restart: unless-stopped
+    environment:
+      PGADMIN_DEFAULT_EMAIL: admin@admin.com
+      PGADMIN_DEFAULT_PASSWORD: password
+    volumes:
+      - pgadmin:/var/lib/pgadmin
+    ports:
+      - "5050:80"
 
 volumes:
   faketime:
diff --git a/test/docker/dropwizard/Dockerfile b/test/docker/dropwizard/Dockerfile
new file mode 100644
index 0000000..32dac7b
--- /dev/null
+++ b/test/docker/dropwizard/Dockerfile
@@ -0,0 +1,22 @@
+##
+## Build
+##
+FROM maven:3-openjdk-17-slim AS mvn
+COPY . /usr/src/build
+WORKDIR /usr/src/build
+RUN mvn package
+##
+## Deploy
+##
+FROM openjdk:17-jdk-slim
+ENV DEBIAN_FRONTEND noninteractive
+RUN apt-get update && apt-get install libfaketime
+ENV LD_PRELOAD=/usr/lib/x86_64-linux-gnu/faketime/libfaketime.so.1
+ENV FAKETIME_CACHE_DURATION=1
+ENV FAKETIME_DONT_FAKE_MONOTONIC=1
+ENV FAKETIME_TIMESTAMP_FILE=/var/lib/faketime/faketimerc
+COPY --from=mvn /usr/src/build/target/faketime-demo-1.0-SNAPSHOT.jar /faketime-demo.jar
+EXPOSE 8080
+EXPOSE 8081
+ENTRYPOINT ["java", "-jar", "/faketime-demo.jar"]
+CMD [ "server" ]
diff --git a/test/docker/dropwizard/README.md b/test/docker/dropwizard/README.md
new file mode 100644
index 0000000..69be8c3
--- /dev/null
+++ b/test/docker/dropwizard/README.md
@@ -0,0 +1,13 @@
+# FaketimeDemo
+
+How to start the FaketimeDemo application
+---
+
+1. Run `mvn clean install` to build your application
+1. Start application with `java -jar target/faketime-demo-1.0-SNAPSHOT.jar server config.yml`
+1. To check that your application is running enter url `http://localhost:8080`
+
+Health Check
+---
+
+To see your applications health enter url `http://localhost:8081/healthcheck`
diff --git a/test/docker/dropwizard/config.yml b/test/docker/dropwizard/config.yml
new file mode 100644
index 0000000..3cf4d14
--- /dev/null
+++ b/test/docker/dropwizard/config.yml
@@ -0,0 +1,4 @@
+logging:
+  level: INFO
+  loggers:
+    com.example: DEBUG
diff --git a/test/docker/dropwizard/mvn.sh b/test/docker/dropwizard/mvn.sh
new file mode 100755
index 0000000..5711fd4
--- /dev/null
+++ b/test/docker/dropwizard/mvn.sh
@@ -0,0 +1,6 @@
+#!/bin/sh
+docker run -it --rm \
+  -v "$PWD/.m2":/root/.m2 \
+  -v "$PWD":/root/faketime-demo \
+  -w /root/faketime-demo maven:3-openjdk-17-slim \
+  mvn $@
diff --git a/test/docker/dropwizard/pom.xml b/test/docker/dropwizard/pom.xml
new file mode 100644
index 0000000..8168947
--- /dev/null
+++ b/test/docker/dropwizard/pom.xml
@@ -0,0 +1,170 @@
+<?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/maven-v4_0_0.xsd">
+
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>com.example</groupId>
+    <artifactId>faketime-demo</artifactId>
+    <version>1.0-SNAPSHOT</version>
+    <packaging>jar</packaging>
+
+    <name>FaketimeDemo</name>
+
+    <properties>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+        <dropwizard.version>2.0.28</dropwizard.version>
+        <mainClass>com.example.FaketimeDemoApplication</mainClass>
+    </properties>
+
+    <dependencyManagement>
+        <dependencies>
+            <dependency>
+                <groupId>io.dropwizard</groupId>
+                <artifactId>dropwizard-dependencies</artifactId>
+                <version>${dropwizard.version}</version>
+                <type>pom</type>
+                <scope>import</scope>
+            </dependency>
+        </dependencies>
+    </dependencyManagement>
+
+    <dependencies>
+        <dependency>
+            <groupId>io.dropwizard</groupId>
+            <artifactId>dropwizard-core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.fasterxml.jackson.core</groupId>
+            <artifactId>jackson-annotations</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>jakarta.validation</groupId>
+            <artifactId>jakarta.validation-api</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.hibernate.validator</groupId>
+            <artifactId>hibernate-validator</artifactId>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <artifactId>maven-shade-plugin</artifactId>
+                <version>3.2.4</version>
+                <configuration>
+                    <createDependencyReducedPom>true</createDependencyReducedPom>
+                    <transformers>
+                        <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
+                        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
+                            <mainClass>${mainClass}</mainClass>
+                        </transformer>
+                    </transformers>
+                    <!-- exclude signed Manifests -->
+                    <filters>
+                        <filter>
+                            <artifact>*:*</artifact>
+                            <excludes>
+                                <exclude>META-INF/*.SF</exclude>
+                                <exclude>META-INF/*.DSA</exclude>
+                                <exclude>META-INF/*.RSA</exclude>
+                            </excludes>
+                        </filter>
+                    </filters>
+                </configuration>
+                <executions>
+                    <execution>
+                        <phase>package</phase>
+                        <goals>
+                            <goal>shade</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+            <plugin>
+                <artifactId>maven-jar-plugin</artifactId>
+                <version>3.2.0</version>
+                <configuration>
+                    <archive>
+                        <manifest>
+                            <addClasspath>true</addClasspath>
+                            <mainClass>${mainClass}</mainClass>
+                        </manifest>
+                    </archive>
+                </configuration>
+            </plugin>
+            <plugin>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <version>3.8.1</version>
+                <configuration>
+                    <source>1.8</source>
+                    <target>1.8</target>
+                </configuration>
+            </plugin>
+            <plugin>
+                <artifactId>maven-surefire-plugin</artifactId>
+                <version>2.22.2</version>
+            </plugin>
+            <plugin>
+                <artifactId>maven-source-plugin</artifactId>
+                <version>3.2.1</version>
+                <executions>
+                    <execution>
+                        <id>attach-sources</id>
+                        <goals>
+                            <goal>jar</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+            <plugin>
+                <artifactId>maven-javadoc-plugin</artifactId>
+                <version>3.3.1</version>
+                <executions>
+                    <execution>
+                        <id>attach-javadocs</id>
+                        <goals>
+                            <goal>jar</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+
+    <reporting>
+        <plugins>
+            <plugin>
+                <artifactId>maven-project-info-reports-plugin</artifactId>
+                <version>3.1.2</version>
+                <configuration>
+                    <dependencyLocationsEnabled>false</dependencyLocationsEnabled>
+                    <dependencyDetailsEnabled>false</dependencyDetailsEnabled>
+                </configuration>
+            </plugin>
+            <plugin>
+                <artifactId>maven-javadoc-plugin</artifactId>
+                <version>3.3.1</version>
+            </plugin>
+        </plugins>
+    </reporting>
+    <profiles>
+        <profile>
+            <id>java11+</id>
+            <activation>
+                <jdk>[11,)</jdk>
+            </activation>
+            <properties>
+                <!--
+                Workaround for "javadoc: error - The code being documented uses modules but the packages
+                defined in https://docs.oracle.com/javase/8/docs/api/ are in the unnamed module."
+                -->
+                <maven.javadoc.skip>true</maven.javadoc.skip>
+            </properties>
+        </profile>
+    </profiles>
+</project>
diff --git a/test/docker/dropwizard/src/main/java/com/example/FaketimeDemoApplication.java b/test/docker/dropwizard/src/main/java/com/example/FaketimeDemoApplication.java
new file mode 100644
index 0000000..563d52f
--- /dev/null
+++ b/test/docker/dropwizard/src/main/java/com/example/FaketimeDemoApplication.java
@@ -0,0 +1,29 @@
+package com.example;
+
+import io.dropwizard.Application;
+import io.dropwizard.setup.Bootstrap;
+import io.dropwizard.setup.Environment;
+
+public class FaketimeDemoApplication extends Application<FaketimeDemoConfiguration> {
+
+    public static void main(final String[] args) throws Exception {
+        new FaketimeDemoApplication().run(args);
+    }
+
+    @Override
+    public String getName() {
+        return "FaketimeDemo";
+    }
+
+    @Override
+    public void initialize(final Bootstrap<FaketimeDemoConfiguration> bootstrap) {
+        // TODO: application initialization
+    }
+
+    @Override
+    public void run(final FaketimeDemoConfiguration configuration,
+                    final Environment environment) {
+        // TODO: implement application
+    }
+
+}
diff --git a/test/docker/dropwizard/src/main/java/com/example/FaketimeDemoConfiguration.java b/test/docker/dropwizard/src/main/java/com/example/FaketimeDemoConfiguration.java
new file mode 100644
index 0000000..8dfb44f
--- /dev/null
+++ b/test/docker/dropwizard/src/main/java/com/example/FaketimeDemoConfiguration.java
@@ -0,0 +1,10 @@
+package com.example;
+
+import io.dropwizard.Configuration;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.hibernate.validator.constraints.*;
+import javax.validation.constraints.*;
+
+public class FaketimeDemoConfiguration extends Configuration {
+    // TODO: implement service configuration
+}
diff --git a/test/docker/dropwizard/src/main/resources/banner.txt b/test/docker/dropwizard/src/main/resources/banner.txt
new file mode 100644
index 0000000..ee657d5
--- /dev/null
+++ b/test/docker/dropwizard/src/main/resources/banner.txt
@@ -0,0 +1,6 @@
+================================================================================
+
+                              FaketimeDemo
+
+================================================================================
+
diff --git a/test/docker/java/Dockerfile b/test/docker/java/Dockerfile
index 1930e90..6711f80 100644
--- a/test/docker/java/Dockerfile
+++ b/test/docker/java/Dockerfile
@@ -1,5 +1,4 @@
-FROM openjdk:slim-bullseye
-
+FROM openjdk:17-jdk-slim
 ENV DEBIAN_FRONTEND noninteractive
 RUN apt-get update && apt-get install libfaketime
 ENV LD_PRELOAD=/usr/lib/x86_64-linux-gnu/faketime/libfaketime.so.1
-- 
GitLab