Repository navigation
Expand file tree
/
Copy pathapplication.kt
More file actions
69 lines (59 loc) · 2 KB
/
Copy pathapplication.kt
File metadata and controls
69 lines (59 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.oshai
import com.github.jasync.sql.db.Connection
import com.github.jasync.sql.db.QueryResult
import com.github.jasync.sql.db.mysql.MySQLConnectionBuilder
import io.ktor.application.ApplicationCall
import io.ktor.application.call
import io.ktor.application.install
import io.ktor.features.ContentNegotiation
import io.ktor.gson.gson
import io.ktor.response.respond
import io.ktor.routing.get
import io.ktor.routing.routing
import io.ktor.server.engine.embeddedServer
import io.ktor.server.netty.Netty
import io.ktor.util.pipeline.PipelineContext
import kotlinx.coroutines.future.await
import mu.KotlinLogging
import java.util.concurrent.TimeUnit
fun main() {
val server = embeddedServer(Netty, port = 8080) {
install(ContentNegotiation) {
gson {
setPrettyPrinting()
}
routing {
get("/") {
logger.info { "handling mysql request" }
handleMysqlRequest("select 0")
}
}
}
}
println("STARTING")
connectionPool.connect().get()
try {
server.start(wait = true)
} finally {
println("DISCO")
connectionPool.disconnect().get()
}
}
private val logger = KotlinLogging.logger {}
val connectionPool = MySQLConnectionBuilder.createConnectionPool {
username = "test"
host = "localhost"
port = 3306
password = "123456"
database = "test"
maxActiveConnections = 100
maxIdleTime = TimeUnit.MINUTES.toMillis(15)
maxPendingQueries = 10_000
connectionValidationInterval = TimeUnit.SECONDS.toMillis(30)
}
private suspend fun PipelineContext<Unit, ApplicationCall>.handleMysqlRequest(query: String) {
val queryResult = connectionPool.sendPreparedStatementAwait(query = query)
call.respond(queryResult.rows[0][0].toString())
}
private suspend fun Connection.sendPreparedStatementAwait(query: String, values: List<Any> = emptyList()): QueryResult =
this.sendPreparedStatement(query, values).await()