Skip to content

Commit

Permalink
feat: Standalone Base
Browse files Browse the repository at this point in the history
  • Loading branch information
CoasterFreakDE committed Apr 30, 2024
1 parent fd7a966 commit 30adab3
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 27 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group = "de.themeparkcraft.audioserver"
version = "0.1-Snapshot"
version = "0.2-Snapshot"

repositories {
maven("https://nexus.flawcra.cc/repository/maven-mirrors/")
Expand All @@ -28,6 +28,6 @@ subprojects {
}

group = "de.themeparkcraft.audioserver"
version = "0.1-Snapshot"
version = "0.2-Snapshot"

}
2 changes: 2 additions & 0 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ plugins {
}

val protobufVersion: String by project
val coroutinesVersion: String by project

dependencies {
testImplementation(kotlin("test"))

api("com.rabbitmq:amqp-client:5.21.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-protobuf:$protobufVersion")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
}

publishing {
Expand Down
3 changes: 2 additions & 1 deletion common/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
protobufVersion=1.6.3
protobufVersion=1.6.3
coroutinesVersion=1.8.1-Beta
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package de.themeparkcraft.audioserver.common.rabbit

import com.rabbitmq.client.BuiltinExchangeType
import com.rabbitmq.client.Channel
import com.rabbitmq.client.Connection
import com.rabbitmq.client.ConnectionFactory
import com.rabbitmq.client.*
import de.themeparkcraft.audioserver.common.data.RabbitConfiguration
import de.themeparkcraft.audioserver.common.extensions.getLogger
import de.themeparkcraft.audioserver.common.interfaces.RabbitSendable
import kotlinx.serialization.protobuf.ProtoBuf
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
* The RabbitClient class represents a client for connecting to a RabbitMQ server.
Expand Down Expand Up @@ -36,8 +35,8 @@ class RabbitClient(rabbitConfiguration: RabbitConfiguration) {
this.connection = connectionFactory.newConnection()
this.channel = connection.createChannel()

this.queue = this.channel.queueDeclare(ROUTIING_KEY, false, true, true, null).queue
this.channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC, false, true, null)
this.queue = this.channel.queueDeclare(ROUTIING_KEY, false, false, true, null).queue
this.channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC, false, false, null)
this.channel.queueBind(this.queue, EXCHANGE_NAME, ROUTIING_KEY)

getLogger().info("Connected to RabbitMQ server.")
Expand All @@ -48,7 +47,7 @@ class RabbitClient(rabbitConfiguration: RabbitConfiguration) {
*
* @param messageListener The listener for message consumption.
*/
fun withListener(messageListener: RabbitMQListener) {
fun withListener(messageListener: DeliverCallback) {
this.channel.basicConsume(this.queue, true, messageListener) { consumerTag -> getLogger().error("Consumer $consumerTag cancelled.") }
}

Expand All @@ -58,8 +57,10 @@ class RabbitClient(rabbitConfiguration: RabbitConfiguration) {
* @param rabbitSendable The object to be sent as a message.
*/
fun sendMessage(rabbitSendable: RabbitSendable) {
val message = rabbitSendable.encode()
this.channel.basicPublish(EXCHANGE_NAME, ROUTIING_KEY, null, message)
CoroutineScope(Dispatchers.Default).launch {
val message = rabbitSendable.encode()
channel.basicPublish(EXCHANGE_NAME, ROUTIING_KEY, null, message)
}
}

/**
Expand Down

This file was deleted.

6 changes: 6 additions & 0 deletions standalone/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# RabbitMQ
RABBITMQ_HOST=localhost
RABBITMQ_PORT=5672
RABBITMQ_VHOST=/
RABBITMQ_USER=guest
RABBITMQ_PASSWORD=guest
19 changes: 19 additions & 0 deletions standalone/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,24 @@ plugins {
id("com.github.johnrengelman.shadow") version "8.1.1"
}

val dotenvVersion: String by project
val fruxzAscendVersion: String by project
val logbackVersion: String by project

val deps = listOf(
"dev.fruxz:ascend:$fruxzAscendVersion",
"io.github.cdimascio:dotenv-kotlin:$dotenvVersion",
"ch.qos.logback:logback-classic:$logbackVersion"
)

dependencies {
implementation(project(":common"))
shadow(project(":common"))

deps.forEach {
implementation(it)
shadow(it)
}
}

tasks {
Expand All @@ -19,5 +34,9 @@ tasks {
mergeServiceFiles()
configurations = listOf(project.configurations.shadow.get())
archiveFileName.set("AudioServerStandalone.jar")

manifest {
attributes["Main-Class"] = "de.themeparkcraft.audioserver.StartKt"
}
}
}
3 changes: 3 additions & 0 deletions standalone/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dotenvVersion=6.4.1
fruxzAscendVersion=2024.1.2
logbackVersion=1.5.6
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package de.themeparkcraft.audioserver

import de.themeparkcraft.audioserver.common.data.RabbitConfiguration
import de.themeparkcraft.audioserver.common.extensions.getLogger
import de.themeparkcraft.audioserver.common.rabbit.RabbitClient
import de.themeparkcraft.audioserver.utils.Environment
import kotlin.system.exitProcess

class AudioServerStandalone {

private val rabbitClient: RabbitClient

init {
val rabbitConfiguration = RabbitConfiguration(
host = Environment.getEnv("RABBITMQ_HOST") ?: "localhost",
port = Environment.getEnv("RABBITMQ_PORT")?.toInt() ?: 5672,
virtualHost = Environment.getEnv("RABBITMQ_VIRTUAL_HOST") ?: "/",
username = Environment.getEnv("RABBITMQ_USER") ?: "guest",
password = Environment.getEnv("RABBITMQ_PASSWORD") ?: "guest"
)

getLogger().info("Starting AudioServer standalone at ${rabbitConfiguration.host}:${rabbitConfiguration.port} - (vhost: ${rabbitConfiguration.virtualHost}) with user ${rabbitConfiguration.username}...")

try {
rabbitClient = RabbitClient(rabbitConfiguration)

rabbitClient.withListener { message, delivery ->
getLogger().info("Received message: $message")
}

getLogger().info("AudioServer standalone started.")
} catch (exception: Exception) {
getLogger().error("Failed to start AudioServer standalone due to the following exception: ", exception)
exitProcess(1)
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package de.themeparkcraft.audioserver

fun main() {
AudioServerStandalone()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package de.themeparkcraft.audioserver.utils

import io.github.cdimascio.dotenv.dotenv

object Environment {

private val env = System.getenv()
private val dotEnv = dotenv {
ignoreIfMissing = true
}

/**
* Retrieves the value of the environment variable with the specified key.
*
* @param key The key of the environment variable.
* @return The value of the environment variable, or null if the variable is not found.
*/
fun getEnv(key: String): String? {
return dotEnv[key] ?: env[key]
}

}
14 changes: 14 additions & 0 deletions standalone/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<configuration>
<!-- Configure the Console appender -->
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<!-- Enable the Console and Sentry appenders, Console is provided as an example
of a non-Sentry logger that is set to a different logging threshold -->
<root level="INFO">
<appender-ref ref="Console"/>
</root>
</configuration>

0 comments on commit 30adab3

Please sign in to comment.