Torma Kristóf
f4ce568aa8
All checks were successful
continuous-integration/drone/push Build is passing
118 lines
3.4 KiB
Kotlin
118 lines
3.4 KiB
Kotlin
package com.kmalbz
|
|
|
|
import com.google.gson.Gson
|
|
import com.kmalbz.api.model.ApiObject
|
|
import com.kmalbz.api.route.OutputServiceRDBServer
|
|
import io.ktor.application.*
|
|
import io.ktor.response.*
|
|
import io.ktor.routing.*
|
|
import io.ktor.http.*
|
|
import io.ktor.gson.*
|
|
import io.ktor.features.*
|
|
import io.ktor.client.*
|
|
import io.ktor.client.engine.apache.*
|
|
import io.ktor.auth.*
|
|
import org.apache.http.HttpException
|
|
import com.kmalbz.database.DatabaseFactory
|
|
import com.kmalbz.database.dao.ResultObjects
|
|
import com.kmalbz.database.service.ResultObjectService
|
|
import com.kmalbz.mq.RecieveOutput
|
|
import io.ktor.util.KtorExperimentalAPI
|
|
import com.rabbitmq.client.*
|
|
import com.typesafe.config.ConfigFactory
|
|
import io.ktor.config.HoconApplicationConfig
|
|
import com.viartemev.thewhiterabbit.channel.*
|
|
import kotlinx.coroutines.Dispatchers
|
|
import kotlinx.coroutines.GlobalScope
|
|
import kotlinx.coroutines.launch
|
|
import org.jetbrains.exposed.sql.SchemaUtils
|
|
import org.jetbrains.exposed.sql.transactions.transaction
|
|
|
|
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
|
|
|
|
@KtorExperimentalAPI
|
|
@Suppress("unused") // Referenced in application.conf
|
|
@kotlin.jvm.JvmOverloads
|
|
fun Application.module(testing: Boolean = false) {
|
|
install(ContentNegotiation) {
|
|
gson {
|
|
}
|
|
}
|
|
|
|
val client = HttpClient(Apache) {
|
|
}
|
|
|
|
install(Authentication) {
|
|
}
|
|
|
|
DatabaseFactory.init()
|
|
transaction{
|
|
SchemaUtils.create(ResultObjects)
|
|
}
|
|
|
|
|
|
val appConfig = HoconApplicationConfig(ConfigFactory.load())
|
|
val factory = ConnectionFactory()
|
|
factory.host = appConfig.property("ktor.mq.host").getString()
|
|
factory.username = appConfig.property("ktor.mq.username").getString()
|
|
factory.password = appConfig.property("ktor.mq.password").getString()
|
|
|
|
val connection = factory.newConnection()
|
|
val channel = connection.createChannel()
|
|
|
|
channel.exchangeDeclare(RecieveOutput.EXCHANGE_NAME, BuiltinExchangeType.FANOUT)
|
|
val queueName = channel.queueDeclare().queue
|
|
channel.queueBind(queueName, RecieveOutput.EXCHANGE_NAME, "")
|
|
|
|
GlobalScope.launch(Dispatchers.Default) {
|
|
connection.channel {
|
|
consume(queueName) {
|
|
consumeMessageWithConfirm({
|
|
val resultObjectService = ResultObjectService()
|
|
val rawJson = String(it.body)
|
|
val gson = Gson()
|
|
val apiObject = gson.fromJson(rawJson,ApiObject::class.java)
|
|
|
|
resultObjectService.addOne(apiObject)
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
routing {
|
|
get("/") {
|
|
call.respondText("HELLO WORLD!", contentType = ContentType.Text.Plain)
|
|
}
|
|
|
|
get("/json/gson") {
|
|
call.respond(mapOf("hello" to "world"))
|
|
}
|
|
|
|
install(StatusPages) {
|
|
exception<AuthenticationException> { _ ->
|
|
call.respond(HttpStatusCode.Unauthorized)
|
|
}
|
|
|
|
exception<AuthorizationException> { _ ->
|
|
call.respond(HttpStatusCode.Forbidden)
|
|
}
|
|
|
|
exception<HttpException> { _ ->
|
|
call.respond(HttpStatusCode.BadRequest)
|
|
}
|
|
|
|
exception<IllegalStateException> { _ ->
|
|
call.respond(HttpStatusCode.NotAcceptable)
|
|
}
|
|
}
|
|
|
|
OutputServiceRDBServer().apply {
|
|
registerOutput()
|
|
}
|
|
}
|
|
}
|
|
|
|
class AuthenticationException : RuntimeException()
|
|
class AuthorizationException : RuntimeException()
|
|
|