Skip to content

Commit

Permalink
fix: Base64 encode docker port response
Browse files Browse the repository at this point in the history
  • Loading branch information
utfunderscore committed Aug 23, 2024
1 parent 6359ad9 commit f9e45fc
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package org.readutf.orchestrator.wrapper

import com.alibaba.fastjson2.JSON
import com.alibaba.fastjson2.TypeReference
import kotlinx.coroutines.runBlocking
import org.jetbrains.annotations.Blocking
import org.readutf.orchestrator.shared.utils.ApiResponse
import org.readutf.orchestrator.wrapper.services.DockerService
import org.readutf.orchestrator.wrapper.services.ServerService
import org.readutf.orchestrator.wrapper.types.ContainerPort
import retrofit2.Retrofit
import retrofit2.converter.fastjson.FastJsonConverterFactory
import java.util.Base64

class OrchestratorApi(
hostname: String,
Expand All @@ -32,6 +37,7 @@ class OrchestratorApi(
@Blocking
fun getPort(shortId: String) =
runBlocking {
dockerService.getPort(shortId)
val json = String(Base64.getDecoder().decode(dockerService.getPort(shortId)))
JSON.parseObject(json, object : TypeReference<ApiResponse<List<ContainerPort>>>() {})
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package org.readutf.orchestrator.wrapper.services

import org.readutf.orchestrator.shared.utils.ApiResponse
import org.readutf.orchestrator.wrapper.types.ContainerPort
import retrofit2.http.GET
import retrofit2.http.Query

interface DockerService {
@GET("/docker/port")
suspend fun getPort(
@Query("shortId") shortId: String,
): ApiResponse<List<ContainerPort>>
): String
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.junit.jupiter.api.Test
* game request tests are executed
*/
class ApiTests {
val orchestratorApi = OrchestratorApi("localhost", 9393)
val orchestratorApi = OrchestratorApi("89.33.85.41", 9393)

@Test
fun testGameRequest() {
Expand All @@ -28,6 +28,13 @@ class ApiTests {
}
}

@Test
fun testGetPort() {
runBlocking {
println(orchestratorApi.getPort("b1666f1bf723"))
}
}

@Test
fun testGetServer() {
runBlocking {
Expand Down
4 changes: 2 additions & 2 deletions Server/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ FROM eclipse-temurin:21-jdk-jammy as deps
VOLUME ["orchestrator"]
WORKDIR /orchestrator

ADD https://github.com/utfunderscore/orchestrator/releases/download/latest/Server-1.4.4-all.jar /orchestrator
ADD https://github.com/utfunderscore/orchestrator/releases/download/latest/Server-1.4.5-all.jar /orchestrator

EXPOSE 2980
EXPOSE 9393


CMD ["java", "-jar", "Server-1.4.4-all.jar"]
CMD ["java", "-jar", "Server-1.4.5-all.jar"]
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import revxrsal.commands.cli.ConsoleCommandHandler
import java.net.SocketException

class Orchestrator(
val settings: Settings,
private val settings: Settings,
) {
private val logger = KotlinLogging.logger { }

Expand Down
20 changes: 20 additions & 0 deletions Server/src/main/kotlin/org/readutf/orchestrator/server/Test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.readutf.orchestrator.server

import com.alibaba.fastjson2.JSON
import com.alibaba.fastjson2.TypeReference
import org.readutf.orchestrator.shared.utils.ApiResponse

fun main() {
Test()
}

class Test {
init {
val json =
"{\"response\":[{\"IP\":\"0.0.0.0\",\"PrivatePort\":2980,\"PublicPort\":2980,\"Type\":\"tcp\"},{\"IP\":\"::\",\"PrivatePort\":2980,\"PublicPort\":2980,\"Type\":\"tcp\"},{\"IP\":\"0.0.0.0\",\"PrivatePort\":9393,\"PublicPort\":9393,\"Type\":\"tcp\"},{\"IP\":\"::\",\"PrivatePort\":9393,\"PublicPort\":9393,\"Type\":\"tcp\"}],\"success\":true}"

val test = JSON.parseObject(json, object : TypeReference<ApiResponse<List<Map<String, String>>>>() {})

println(test)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.javalin.community.routing.annotations.Query
import io.javalin.http.Context
import org.readutf.orchestrator.server.docker.DockerManager
import org.readutf.orchestrator.shared.utils.ApiResponse
import java.util.Base64

@Endpoints("/docker")
class DockerEndpoint(
Expand All @@ -20,9 +21,20 @@ class DockerEndpoint(
val containerByShortId = dockerManager.getContainerByShortId(shortId)

if (containerByShortId.isError()) {
context.json(JSON.toJSONString(ApiResponse.failure<String>(containerByShortId.getError())))
context.json(
Base64.getEncoder().encodeToString(
JSON.toJSONString(ApiResponse.failure<String>(containerByShortId.getError())).toByteArray(),
),
)
return
}

context.json(JSON.toJSONString(ApiResponse.success(containerByShortId.get().getPorts())))
context.json(
Base64.getEncoder().encodeToString(
JSON
.toJSONString(ApiResponse.success(containerByShortId.get().getPorts()))
.toByteArray(),
),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ class ApiResponse<T>(
var failureReason: String?,
val response: T?,
) {
@JSONField(serialize = false)
fun isSuccess() = failureReason == null

@JSONField(serialize = false)
fun getError(): String = failureReason!!

Expand Down
4 changes: 3 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ plugins {
}

group = "org.readutf.orchestrator"
version = "1.4.4"
version = "1.4.5"

dependencies {
testImplementation(kotlin("test"))
}

repositories {
mavenLocal()
maven {
name = "utfunderscoreReleases"
url = uri("https://reposilite.readutf.org/releases")
Expand All @@ -24,6 +25,7 @@ subprojects {
version = rootProject.version

repositories {
mavenLocal()
maven {
name = "utfunderscore"
url = uri("https://reposilite.readutf.org/snapshots")
Expand Down

0 comments on commit f9e45fc

Please sign in to comment.