Compare commits

...

3 commits

Author SHA1 Message Date
56a68bc867 ci: Add new monolith module
All checks were successful
ci/woodpecker/push/java/1 Pipeline was successful
ci/woodpecker/push/java/2 Pipeline was successful
ci/woodpecker/push/java/3 Pipeline was successful
ci/woodpecker/push/java/4 Pipeline was successful
ci/woodpecker/push/java/5 Pipeline was successful
ci/woodpecker/push/nodejs Pipeline was successful
ci/woodpecker/push/oci-image-build/1 Pipeline was successful
ci/woodpecker/push/oci-image-build/2 Pipeline was successful
ci/woodpecker/push/oci-image-build/3 Pipeline was successful
ci/woodpecker/push/oci-image-build/4 Pipeline was successful
ci/woodpecker/push/oci-image-build/5 Pipeline was successful
ci/woodpecker/tag/java/1 Pipeline was successful
ci/woodpecker/tag/java/2 Pipeline was successful
ci/woodpecker/tag/java/3 Pipeline was successful
ci/woodpecker/tag/java/4 Pipeline was successful
ci/woodpecker/tag/java/5 Pipeline was successful
ci/woodpecker/tag/nodejs Pipeline was successful
ci/woodpecker/tag/oci-image-build/1 Pipeline was successful
ci/woodpecker/tag/oci-image-build/2 Pipeline was successful
ci/woodpecker/tag/oci-image-build/3 Pipeline was successful
ci/woodpecker/tag/oci-image-build/4 Pipeline was successful
ci/woodpecker/tag/oci-image-build/5 Pipeline was successful
2024-02-17 16:25:20 +01:00
f0d6c40e65 build: Add new monolith module
Includes a small refactoring of the Dockerfile to make the build
easier to read.
Use the new monolith in the local Docker Compose setup.
2024-02-17 16:22:16 +01:00
4135d7e57e monolith: Add new module
To run the bot and web projects in one process.
2024-02-17 16:19:26 +01:00
8 changed files with 166 additions and 67 deletions

View file

@ -7,6 +7,8 @@
!migration/pom.xml
!web/src/
!web/pom.xml
!monolith/src/
!monolith/pom.xml
!pom.xml
!ui/
ui/node_modules

View file

@ -4,6 +4,7 @@ matrix:
- bot
- web
- migration
- monolith
variables:
- &image 'docker.io/maven:3.9-eclipse-temurin-21'

View file

@ -4,6 +4,7 @@ matrix:
- web
- migration
- ui
- monolith
PLATFORM:
# - linux/amd64
- linux/arm64

View file

@ -5,63 +5,33 @@ ARG MAVEN_CLI_OPTS="--batch-mode --no-transfer-progress -Dmaven.test.skip"
# Copy all project files
FROM docker.io/maven:3.9-eclipse-temurin-21 AS setup
WORKDIR /app
# Copy the dependency specifications
COPY pom.xml .
COPY db/pom.xml db/
COPY bot/pom.xml bot/
COPY migration/pom.xml migration/
COPY web/pom.xml web/
# Resolve dependencies for shared libraries
COPY . .
RUN --mount=type=cache,target=/root/.m2/ \
mvn ${MAVEN_CLI_OPTS} -pl db -am dependency:go-offline
# Install the shared libraries in the local Maven repo (`.m2`)
# This will also install the `root` module.
mvn ${MAVEN_CLI_OPTS} package
# Build db dependency
FROM setup AS db-build
COPY db db
RUN --mount=type=cache,target=/root/.m2/ \
mvn ${MAVEN_CLI_OPTS} -pl db -am install
# Resolve dependencies for modules without dependencies to each other
RUN --mount=type=cache,target=/root/.m2/ \
mvn ${MAVEN_CLI_OPTS} dependency:go-offline
# Build modules
FROM db-build AS web-build
COPY web web
RUN --mount=type=cache,target=/root/.m2/ \
--mount=type=cache,target=/app/web/src/ui/dist/ \
--mount=type=cache,target=/app/web/src/ui/node/ \
--mount=type=cache,target=/app/web/src/ui/node_modules/ \
mvn ${MAVEN_CLI_OPTS} -pl web package
FROM db-build AS bot-build
COPY bot bot
RUN --mount=type=cache,target=/root/.m2/ \
mvn ${MAVEN_CLI_OPTS} -pl bot package
FROM db-build AS migration-build
COPY migration migration
RUN --mount=type=cache,target=/root/.m2/ \
mvn ${MAVEN_CLI_OPTS} -pl migration package
# Create final monolith
FROM docker.io/eclipse-temurin:21-jdk-jammy AS monolith
WORKDIR /app
COPY --from=setup /app/monolith/target/monolith-*-fat.jar /app/monolith.jar
CMD ["java", "-jar", "/app/monolith.jar"]
# Create final web
FROM docker.io/eclipse-temurin:21-jdk-jammy AS web
WORKDIR /app
COPY --from=web-build /app/web/target/web-*-fat.jar /app/web.jar
COPY --from=setup /app/web/target/web-*-fat.jar /app/web.jar
EXPOSE 8080
CMD ["java", "-jar", "/app/web.jar"]
# Create final migration
FROM docker.io/eclipse-temurin:21-jdk-jammy AS migration
WORKDIR /app
COPY --from=migration-build /app/migration/target/migration-*-fat.jar /app/migration.jar
COPY --from=setup /app/migration/target/migration-*-fat.jar /app/migration.jar
CMD ["java", "-jar", "/app/migration.jar"]
# Create final bot
FROM docker.io/eclipse-temurin:21-jdk-jammy AS bot
WORKDIR /app
COPY --from=bot-build /app/bot/target/bot-*-fat.jar /app/bot.jar
COPY --from=setup /app/bot/target/bot-*-fat.jar /app/bot.jar
CMD ["java", "-jar", "/app/bot.jar"]
# Build the ui
@ -86,7 +56,7 @@ RUN <<EOF cat > /etc/caddy/Caddyfile
:8080 {
route {
reverse_proxy /api/* web:8080
reverse_proxy /api/* monolith:8080
reverse_proxy ui:80
}
}

View file

@ -1,40 +1,24 @@
version: "3"
services:
bot:
image: git.hhhammer.de/hamburghammer/dchat/bot:latest
monolith:
image: git.hhhammer.de/hamburghammer/dchat/monolith:latest
env_file:
- .env
build:
dockerfile: Dockerfile
context: .
target: bot
target: monolith
restart: unless-stopped
networks:
- db
user: 1000:1000
depends_on:
- db
- migration
web:
image: git.hhhammer.de/hamburghammer/dchat/web:latest
env_file:
- .env
build:
dockerfile: Dockerfile
context: .
target: web
restart: unless-stopped
user: 1000:1000
depends_on:
- db
- migration
ports:
- 8080:8080
networks:
- db
- web
ports:
- 8080:8080
user: 1000:1000
depends_on:
- db
- migration
migration:
image: git.hhhammer.de/hamburghammer/dchat/migration:latest
@ -72,7 +56,7 @@ services:
user: 1000:1000
depends_on:
- ui
- web
- monolith
ports:
- 8082:8080
networks:

65
monolith/pom.xml Normal file
View file

@ -0,0 +1,65 @@
<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>
<parent>
<groupId>de.hhhammer.dchat</groupId>
<artifactId>dchat</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>monolith</artifactId>
<packaging>jar</packaging>
<name>monolith</name>
<dependencies>
<dependency>
<groupId>de.hhhammer.dchat</groupId>
<artifactId>bot</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>de.hhhammer.dchat</groupId>
<artifactId>web</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>fat</shadedClassifierName>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>de.hhhammer.dchat.monolith.App</Main-Class>
</manifestEntries>
</transformer>
</transformers>
<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>
</plugins>
</build>
</project>

View file

@ -0,0 +1,75 @@
package de.hhhammer.dchat.monolith;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import de.hhhammer.dchat.bot.DiscordBot;
import de.hhhammer.dchat.bot.openai.ChatGPTService;
import de.hhhammer.dchat.db.PostgresServerDBService;
import de.hhhammer.dchat.db.PostgresUserDBService;
import de.hhhammer.dchat.web.AppConfig;
import de.hhhammer.dchat.web.WebAPI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.http.HttpClient;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
public class App {
private static final Logger logger = LoggerFactory.getLogger(App.class);
public static void main(String[] args) {
final String discordApiKey = System.getenv("DISCORD_API_KEY");
if (discordApiKey == null) {
logger.error("Missing environment variables: DISCORD_API_KEY");
System.exit(1);
}
final String openaiApiKey = System.getenv("OPENAI_API_KEY");
if (openaiApiKey == null) {
logger.error("Missing environment variables: OPENAI_API_KEY");
System.exit(1);
}
final String postgresUser = System.getenv("POSTGRES_USER");
final String postgresPassword = System.getenv("POSTGRES_PASSWORD");
final String postgresUrl = System.getenv("POSTGRES_URL");
if (postgresUser == null || postgresPassword == null || postgresUrl == null) {
logger.error("Missing environment variables: POSTGRES_USER and/or POSTGRES_PASSWORD and/or POSTGRES_URL");
System.exit(1);
}
final String apiPortStr = System.getenv("API_PORT") != null ? System.getenv("API_PORT") : "8080";
final int apiPort = Integer.parseInt(apiPortStr);
final boolean debug = "true".equals(System.getenv("API_DEBUG"));
final var chatGPTService = new ChatGPTService(openaiApiKey, HttpClient.newHttpClient(), new ObjectMapper());
final var config = new HikariConfig();
config.setJdbcUrl(postgresUrl);
config.setUsername(postgresUser);
config.setPassword(postgresPassword);
try (final var ds = new HikariDataSource(config)) {
final var serverDBService = new PostgresServerDBService(ds);
final var userDBService = new PostgresUserDBService(ds);
final var discordBot = new DiscordBot(serverDBService, userDBService, chatGPTService, discordApiKey);
final var appConfig = new AppConfig(apiPort, debug);
final var webApi = new WebAPI(serverDBService, userDBService, appConfig);
run(discordBot, webApi);
} catch (InterruptedException e) {
logger.error("Application failed", e);
}
}
private static void run(Runnable... apps) throws InterruptedException {
List<Callable<Void>> callableApps = Arrays.stream(apps).sequential().map(runnable -> (Callable<Void>) () -> {
runnable.run();
return null;
}).toList();
try (var executorService = Executors.newFixedThreadPool(apps.length)) {
executorService.invokeAll(callableApps);
}
}
}

View file

@ -176,5 +176,6 @@
<module>db</module>
<module>web</module>
<module>migration</module>
<module>monolith</module>
</modules>
</project>