Commit 4aa4a61b authored by ryoii's avatar ryoii

http api server handle ktor exception

parent f8282e7d
...@@ -15,15 +15,18 @@ import io.ktor.server.engine.applicationEngineEnvironment ...@@ -15,15 +15,18 @@ import io.ktor.server.engine.applicationEngineEnvironment
import io.ktor.server.engine.connector import io.ktor.server.engine.connector
import io.ktor.server.engine.embeddedServer import io.ktor.server.engine.embeddedServer
import io.ktor.util.KtorExperimentalAPI import io.ktor.util.KtorExperimentalAPI
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import net.mamoe.mirai.api.http.route.mirai import net.mamoe.mirai.api.http.route.mirai
import net.mamoe.mirai.utils.DefaultLogger import net.mamoe.mirai.utils.DefaultLogger
import org.slf4j.LoggerFactory
import org.slf4j.helpers.NOPLogger
import org.slf4j.helpers.NOPLoggerFactory import org.slf4j.helpers.NOPLoggerFactory
import kotlin.coroutines.CoroutineContext
object MiraiHttpAPIServer { object MiraiHttpAPIServer : CoroutineScope {
private val logger = DefaultLogger("Mirai HTTP API") private val logger = DefaultLogger("Mirai HTTP API")
override val coroutineContext: CoroutineContext = CoroutineExceptionHandler { _, throwable -> logger.error(throwable) }
init { init {
SessionManager.authKey = generateSessionKey()//用于验证的key, 使用和SessionKey相同的方法生成, 但意义不同 SessionManager.authKey = generateSessionKey()//用于验证的key, 使用和SessionKey相同的方法生成, 但意义不同
...@@ -43,20 +46,19 @@ object MiraiHttpAPIServer { ...@@ -43,20 +46,19 @@ object MiraiHttpAPIServer {
SessionManager.authKey = authKey SessionManager.authKey = authKey
// TODO: start是无阻塞的,理应获取启动状态后再执行后续代码 // TODO: start是无阻塞的,理应获取启动状态后再执行后续代码
try { launch {
embeddedServer(CIO, environment = applicationEngineEnvironment { embeddedServer(CIO, environment = applicationEngineEnvironment {
this.parentCoroutineContext = coroutineContext
this.log = NOPLoggerFactory().getLogger("NMYSL") this.log = NOPLoggerFactory().getLogger("NMYSL")
this.module(Application::mirai) this.module(Application::mirai)
connector { connector {
this.port = port this.port = port
} }
}).start() }).start(wait = true)
logger.info("Http api server is running with authKey: ${SessionManager.authKey}")
callback?.invoke()
} catch (e: Exception) {
logger.error("Http api server launch error")
} }
logger.info("Http api server is running with authKey: ${SessionManager.authKey}")
callback?.invoke()
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment