Skip to content
This repository has been archived by the owner on Mar 28, 2024. It is now read-only.

Commit

Permalink
Add Docker support and improve error handling in Application.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziedelth committed Sep 27, 2023
1 parent c219b14 commit 2bbeab8
Show file tree
Hide file tree
Showing 12 changed files with 253 additions and 60 deletions.
27 changes: 22 additions & 5 deletions .github/workflows/pull_requests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,34 @@ on:

jobs:
test:
name: Test
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Set up JDK 11
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: 11
java-version: 17
distribution: 'adopt'

- name: Test
run: mvn clean test
- name: Tests
run: mvn test

build:
runs-on: ubuntu-latest

needs:
- test

steps:
- uses: actions/checkout@v4

- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: 17
distribution: 'adopt'

- name: Build
run: mvn -B package -DskipTests
101 changes: 101 additions & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
name: Push

on:
push:
branches:
- master
- stable

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: 17
distribution: 'adopt'

- name: Tests
run: mvn test

build:
runs-on: ubuntu-latest

needs:
- test

steps:
- uses: actions/checkout@v4

- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: 17
distribution: 'adopt'

- name: Build
run: mvn -B package -DskipTests

docker:
runs-on: ubuntu-latest

needs:
- build

steps:
- uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: registry.ziedelth.fr:5000
username: ziedelth
password: ${{ secrets.REGISTRY_PASSWORD }}

- name: Build and push snapshot
uses: docker/build-push-action@v5
if: github.ref == 'refs/heads/master'
with:
context: .
push: true
tags: registry.ziedelth.fr:5000/jais-api:snapshot

- name: Build and push production
uses: docker/build-push-action@v5
if: github.ref == 'refs/heads/stable'
with:
context: .
push: true
tags: registry.ziedelth.fr:5000/jais-api:latest

release:
if: github.ref == 'refs/heads/stable'
name: Release
runs-on: ubuntu-latest

needs:
- docker

steps:
- name: Install SSH KEY
uses: shimataro/ssh-key-action@v2
with:
key: ${{ secrets.SSH_PRIVATE_KEY }}
known_hosts: 'just-a-placeholder-so-we-dont-get-errors'

- name: Adding Known Hosts
run: ssh-keyscan -p 2222 -H ${{ secrets.SSH_HOST_IP }} >> ~/.ssh/known_hosts

- name: SSH Commands
run: |
ssh -C -p 2222 ${{ secrets.SSH_DESTINATION }} "docker pull localhost:5000/jais-api:latest && cd ${{ secrets.SSH_FOLDER }} && docker compose down jais-api && docker compose up jais-api -d"
40 changes: 0 additions & 40 deletions .github/workflows/release.yml

This file was deleted.

5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ out/
### VS Code ###
.vscode/
/target/
/hibernate.cfg.xml
/test.http
/firebase_key.json
/plugins/
**/firebase_key.json
**/plugins/
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM maven:3.9.4-amazoncorretto-17 AS build
COPY . /app
WORKDIR /app
RUN mvn clean package -DskipTests

FROM amazoncorretto:17-alpine
COPY --from=build /app/target/api-1.0.0-jar-with-dependencies.jar /app/api.jar
COPY --from=build /app/data/ /app/data/

RUN apk update && apk add gcompat opencv-dev
EXPOSE 8080
WORKDIR /app
ENTRYPOINT ["java", "-jar", "api.jar"]
15 changes: 15 additions & 0 deletions data/hibernate.cfg.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.url">jdbc:postgresql://localhost:5432/jais</property>
<property name="connection.username">postgres</property>
<property name="connection.password">mysecretpassword</property>
<property name="show_sql">false</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</property>
</session-factory>
</hibernate-configuration>
60 changes: 60 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
version: '3.8'
services:
jais-db:
image: postgres:16-alpine
restart: always
container_name: jais-db
environment:
POSTGRES_PASSWORD: "mysecretpassword"
POSTGRES_USER: postgres
POSTGRES_DB: jais
healthcheck:
test: ["CMD", "pg_isready", "-U", "postgres"]
volumes:
- ./tmp_data.sql:/docker-entrypoint-initdb.d/dummy_dump.sql
jais-api:
image: localhost:5000/jais-api:latest
ports:
- "8080:8080"
restart: on-failure
container_name: jais-api
depends_on:
jais-db:
condition: service_healthy
environment:
DATABASE_URL: jdbc:postgresql://jais-db:5432/jais
DATABASE_USERNAME: postgres
DATABASE_PASSWORD: "mysecretpassword"
SEND_NOTIFICATIONS: "false"
volumes:
- jais-api-data:/app/data
jais-scraper:
image: localhost:5000/jais-scraper:latest
restart: on-failure
container_name: jais-scraper
depends_on:
jais-api:
condition: service_started
environment:
API_URL: http://jais-api:8080/
volumes:
- jais-scraper-data:/app/data
jais-website:
image: localhost:5000/jais-website:latest
ports:
- "8081:80"
restart: on-failure
container_name: jais-website
depends_on:
jais-api:
condition: service_started
volumes:
- jais-website-data:/usr/local/apache2/htdocs/attachments/

volumes:
jais-api-data:
name: jais-api-data
jais-scraper-data:
name: jais-scraper-data
jais-website-data:
name: jais-website-data
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<ktor_version>2.3.4</ktor_version>
<kotlin.code.style>official</kotlin.code.style>
<kotlin.version>1.9.10</kotlin.version>
<kotlin.compiler.jvmTarget>11</kotlin.compiler.jvmTarget>
<kotlin.compiler.jvmTarget>17</kotlin.compiler.jvmTarget>
<logback_version>1.4.11</logback_version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
Expand Down
16 changes: 10 additions & 6 deletions src/main/kotlin/fr/ziedelth/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ fun main(args: Array<String>) {
val scanner = Scanner(System.`in`)

while (true) {
val line = scanner.nextLine()
try {
val line = scanner.nextLine()

if (line == "reload") {
PluginManager.reload()
ListenerManager()
} else if (line == "invalid-cache") {
ImageCache.invalidCache(database)
if (line == "reload") {
PluginManager.reload()
ListenerManager()
} else if (line == "invalid-cache") {
ImageCache.invalidCache(database)
}
} catch (_: Exception) {
Thread.sleep(1000)
}
}
}.start()
Expand Down
23 changes: 21 additions & 2 deletions src/main/kotlin/fr/ziedelth/utils/Database.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,27 @@ open class Database {

Configuration().let { configuration ->
getEntities().forEach { configuration.addAnnotatedClass(it) }

configuration.configure(file)

val url: String? = System.getenv("DATABASE_URL")
val username: String? = System.getenv("DATABASE_USERNAME")
val password: String? = System.getenv("DATABASE_PASSWORD")

if (url?.isNotBlank() == true) {
println("Bypassing hibernate.cfg.xml with system environment variable DATABASE_URL")
configuration.setProperty("hibernate.connection.url", url)
}

if (username?.isNotBlank() == true) {
println("Bypassing hibernate.cfg.xml with system environment variable DATABASE_USERNAME")
configuration.setProperty("hibernate.connection.username", username)
}

if (password?.isNotBlank() == true) {
println("Bypassing hibernate.cfg.xml with system environment variable DATABASE_PASSWORD")
configuration.setProperty("hibernate.connection.password", password)
}

sessionFactory = configuration.buildSessionFactory(
StandardServiceRegistryBuilder().applySettings(configuration.properties).build()
)
Expand All @@ -35,7 +54,7 @@ open class Database {
}
}

constructor() : this(File("hibernate.cfg.xml"))
constructor() : this(File("data/hibernate.cfg.xml"))

protected fun getEntities(): MutableSet<Class<out Serializable>> =
Reflections("fr.ziedelth.entities").getSubTypesOf(Serializable::class.java)
Expand Down
7 changes: 6 additions & 1 deletion src/main/kotlin/fr/ziedelth/utils/Notifications.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ object Notifications {

init {
println("Initializing Firebase")
val file = File("firebase_key.json")
val file = File("data/firebase_key.json")

if (file.exists()) {
FirebaseApp.initializeApp(
Expand All @@ -29,6 +29,11 @@ object Notifications {
}

fun send(title: String? = null, body: String? = null, topic: String = "all") {
with(System.getenv("SEND_NOTIFICATIONS")) {
println("SEND_NOTIFICATIONS: $this")
if (this.isNullOrBlank() || this == "false") return
}

if (initialized) {
FirebaseMessaging.getInstance().send(
Message.builder().setAndroidConfig(
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/fr/ziedelth/utils/plugins/PluginManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.pf4j.DefaultPluginManager
import java.io.File

object PluginManager {
private var defaultPluginManager = DefaultPluginManager(File("plugins").toPath())
private var defaultPluginManager = DefaultPluginManager(File("data/plugins").toPath())
val listeners = mutableListOf<Listener>()

fun loadPlugins() {
Expand All @@ -23,7 +23,7 @@ object PluginManager {
defaultPluginManager.stopPlugins()
defaultPluginManager.unloadPlugins()
listeners.clear()
defaultPluginManager = DefaultPluginManager(File("plugins").toPath())
defaultPluginManager = DefaultPluginManager(File("data/plugins").toPath())
loadPlugins()
}

Expand Down

0 comments on commit 2bbeab8

Please sign in to comment.