diff --git a/src/Java/Maaj/build.gradle.kts b/src/Java/Maaj/build.gradle.kts index 74c688ef9f..344ea85f9e 100644 --- a/src/Java/Maaj/build.gradle.kts +++ b/src/Java/Maaj/build.gradle.kts @@ -30,6 +30,8 @@ dependencies { implementation("io.ktor:ktor-server-cors:$ktor_version") implementation("io.ktor:ktor-server-websockets:$ktor_version") implementation("ch.qos.logback:logback-classic:$logback_version") + implementation("io.ktor:ktor-server-call-logging:$ktor_version") + implementation("io.ktor:ktor-server-double-receive:$ktor_version") testImplementation("io.ktor:ktor-server-tests-jvm:$ktor_version") testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version") implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.3") diff --git a/src/Java/Maaj/src/main/java/com/iguigui/maaj/Application.kt b/src/Java/Maaj/src/main/java/com/iguigui/maaj/Application.kt index 73026406be..fb8d51edbf 100644 --- a/src/Java/Maaj/src/main/java/com/iguigui/maaj/Application.kt +++ b/src/Java/Maaj/src/main/java/com/iguigui/maaj/Application.kt @@ -2,13 +2,22 @@ package com.iguigui.maaj import com.iguigui.maaj.routing.httpRouting import com.iguigui.maaj.routing.wsRouting +import io.ktor.http.* import io.ktor.serialization.kotlinx.json.* import io.ktor.server.netty.* import io.ktor.server.application.* +import io.ktor.server.plugins.callloging.* import io.ktor.server.plugins.contentnegotiation.* import io.ktor.server.plugins.cors.routing.* +import io.ktor.server.plugins.doublereceive.* +import io.ktor.server.request.* import io.ktor.server.websocket.* +import io.ktor.util.logging.* +import kotlinx.coroutines.runBlocking import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonElement +import org.slf4j.event.Level + fun main(args: Array): Unit = EngineMain.main(args) @@ -18,7 +27,12 @@ fun Application.module() { wsRouting() } -fun Application.configure(){ + +lateinit var logger : Logger + +fun Application.configure() { + logger = log + //Input and output serialize install(ContentNegotiation) { json(Json { encodeDefaults = true @@ -39,4 +53,28 @@ fun Application.configure(){ maxFrameSize = Long.MAX_VALUE masking = false } + //Provides the ability to receive a request body several times. + install(DoubleReceive) { + } + //Logging request body. + install(CallLogging) { + level = Level.INFO + format { call -> + val status = call.response.status() + val httpMethod = call.request.httpMethod + val queryParameters = call.request.rawQueryParameters + when (httpMethod) { + HttpMethod.Get -> "Status: $status, HTTP method: $httpMethod, Uri: ${call.request.uri}, QueryParameters: $queryParameters " + HttpMethod.Post -> runBlocking { + "Status: $status, HTTP method: $httpMethod, Uri: ${call.request.uri} ,Body: ${ + call.receive().toString() + } " + } + else -> { + "Status: $status, HTTP method: $httpMethod, Uri: ${call.request.uri}, QueryParameters: $queryParameters " + } + } + } + } + } diff --git a/src/Java/Maaj/src/main/java/com/iguigui/maaj/routing/WsRouting.kt b/src/Java/Maaj/src/main/java/com/iguigui/maaj/routing/WsRouting.kt index f32b8b94ec..780080b85d 100644 --- a/src/Java/Maaj/src/main/java/com/iguigui/maaj/routing/WsRouting.kt +++ b/src/Java/Maaj/src/main/java/com/iguigui/maaj/routing/WsRouting.kt @@ -1,6 +1,7 @@ package com.iguigui.maaj.routing import com.iguigui.maaj.dto.* +import com.iguigui.maaj.logger import com.iguigui.maaj.service.Connection import com.iguigui.maaj.service.MaaService import com.iguigui.maaj.service.MaaService.addConnection @@ -20,13 +21,14 @@ import kotlin.collections.LinkedHashSet fun Application.wsRouting() { routing { webSocket("/API/V1") { - println("Adding user!") val connection = Connection(this) + logger.info("Ws connection connected ! connection : ${connection.session} ") addConnection(connection) try { for (frame in incoming) { frame as? Frame.Text ?: continue val receivedText = frame.readText() + logger.info(receivedText) val wsRequest = Json.decodeFromString(WsRequest.serializer(), receivedText) var response: BaseData? = null when (wsRequest.command) { @@ -86,10 +88,10 @@ fun Application.wsRouting() { } } catch (e: Exception) { - e.printStackTrace() - println(e.message) + logger.warn("Ws Exception $e.message") + logger.warn(e.stackTraceToString()) } finally { - println("Removing $connection!") + logger.info("Ws connection disconnected ! connection : $connection ") removeConnection(connection) } } diff --git a/src/Java/Maaj/src/main/java/com/iguigui/maaj/service/MaaInstance.kt b/src/Java/Maaj/src/main/java/com/iguigui/maaj/service/MaaInstance.kt index 4da79fd265..6a32fabba7 100644 --- a/src/Java/Maaj/src/main/java/com/iguigui/maaj/service/MaaInstance.kt +++ b/src/Java/Maaj/src/main/java/com/iguigui/maaj/service/MaaInstance.kt @@ -3,8 +3,10 @@ package com.iguigui.maaj.service import com.iguigui.maaj.dto.CallBackLog import com.iguigui.maaj.dto.* import com.iguigui.maaj.easySample.MeoAssistant +import com.iguigui.maaj.logger import com.iguigui.maaj.util.Json import com.sun.jna.Pointer +import io.ktor.server.application.* import java.util.concurrent.atomic.AtomicLong import kotlin.reflect.KFunction1 @@ -48,9 +50,9 @@ class MaaInstance( override fun callback(msg: Int, detail_json: String, custom_arg: String) { - System.out.printf("回调msg : %s , 回调 detail_json : %s ,回调 custom_arg : %s \n", msg, detail_json, custom_arg) val callBackLog = CallBackLog(id, msgId.getAndIncrement(), msg, Json.parseToJsonElement(detail_json)) - callBackAction?.call(callBackLog) + logger.info("callBackLog = $callBackLog") + callBackAction.call(callBackLog) when (msg) { INTERNAL_ERROR, INIT_FAILED, diff --git a/src/Java/Maaj/src/main/resources/logback.xml b/src/Java/Maaj/src/main/resources/logback.xml index a81a812abd..8cadd778e5 100644 --- a/src/Java/Maaj/src/main/resources/logback.xml +++ b/src/Java/Maaj/src/main/resources/logback.xml @@ -8,4 +8,25 @@ + + + log/maa-http.log + + + maa-http-%d{yyyy-MM-dd}.%i.txt + + 10MB + 10 + 100MB + + + true + + %d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + diff --git a/src/Java/Maaj/src/test/java/com/iguigui/maaj/routing/HttpRoutingKtTest.kt b/src/Java/Maaj/src/test/java/com/iguigui/maaj/routing/HttpRoutingKtTest.kt index e25e0e9098..80599e3743 100644 --- a/src/Java/Maaj/src/test/java/com/iguigui/maaj/routing/HttpRoutingKtTest.kt +++ b/src/Java/Maaj/src/test/java/com/iguigui/maaj/routing/HttpRoutingKtTest.kt @@ -20,78 +20,76 @@ class HttpRoutingKtTest { } client.get("/API/V1/getVersion").apply { Assert.assertEquals(status,OK) - val body :GetVersionResponse = body() - println(body.version) - } - } - - @Test - fun testPostApiV1Connect() = testApplication { - application { - httpRouting() - } - client.post("/API/V1/connect").apply { - - } - } - - @Test - fun testPostApiV1Appendtask() = testApplication { - application { - httpRouting() - } - client.post("/API/V1/appendTask").apply { - TODO("Please write your test here") - } - } - - @Test - fun testPostApiV1Settaskparams() = testApplication { - application { - httpRouting() - } - client.post("/API/V1/setTaskParams").apply { - TODO("Please write your test here") - } - } - - @Test - fun testPostApiV1Start() = testApplication { - application { - httpRouting() - } - client.post("/API/V1/start").apply { - TODO("Please write your test here") - } - } - - @Test - fun testPostApiV1Stop() = testApplication { - application { - httpRouting() - } - client.post("/API/V1/stop").apply { - TODO("Please write your test here") - } - } - - @Test - fun testPostApiV1Destroy() = testApplication { - application { - httpRouting() - } - client.post("/API/V1/destroy").apply { - TODO("Please write your test here") - } - } - - @Test - fun testGetApiV1Listinstance() = testApplication { - application { - httpRouting() - } - client.get("/API/V1/listInstance").apply { - TODO("Please write your test here") } } +// +// @Test +// fun testPostApiV1Connect() = testApplication { +// application { +// httpRouting() +// } +// client.post("/API/V1/connect").apply { +// +// } +// } +// +// @Test +// fun testPostApiV1Appendtask() = testApplication { +// application { +// httpRouting() +// } +// client.post("/API/V1/appendTask").apply { +// TODO("Please write your test here") +// } +// } +// +// @Test +// fun testPostApiV1Settaskparams() = testApplication { +// application { +// httpRouting() +// } +// client.post("/API/V1/setTaskParams").apply { +// TODO("Please write your test here") +// } +// } +// +// @Test +// fun testPostApiV1Start() = testApplication { +// application { +// httpRouting() +// } +// client.post("/API/V1/start").apply { +// TODO("Please write your test here") +// } +// } +// +// @Test +// fun testPostApiV1Stop() = testApplication { +// application { +// httpRouting() +// } +// client.post("/API/V1/stop").apply { +// TODO("Please write your test here") +// } +// } +// +// @Test +// fun testPostApiV1Destroy() = testApplication { +// application { +// httpRouting() +// } +// client.post("/API/V1/destroy").apply { +// TODO("Please write your test here") +// } +// } +// +// @Test +// fun testGetApiV1Listinstance() = testApplication { +// application { +// httpRouting() +// } +// client.get("/API/V1/listInstance").apply { +// TODO("Please write your test here") +// } +// } }