init commit
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
//package com.example
|
||||
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
import io.ktor.server.engine.*
|
||||
import io.ktor.server.http.content.staticFiles
|
||||
import io.ktor.server.netty.*
|
||||
import io.ktor.server.request.receiveText
|
||||
import java.io.File
|
||||
import kotlin.random.Random
|
||||
|
||||
//átlagos JAVA alapú fejlesztő
|
||||
|
||||
val httpPort: Int = System.getenv("HTTP_PORT")?.toInt() ?: 8080
|
||||
val resourcesPath: String = System.getenv("RESOURCES") ?: "src/main/resources/"
|
||||
|
||||
|
||||
object ConversationHandler {
|
||||
val ids = mutableListOf<Int>()
|
||||
|
||||
fun nextID(): Int
|
||||
{
|
||||
val num = Random.nextInt(0, 2000000000)
|
||||
|
||||
if (num in ids) return nextID()
|
||||
else {
|
||||
ids.add(num)
|
||||
return num
|
||||
}
|
||||
}
|
||||
val conversations = mutableListOf<Conversation>()
|
||||
|
||||
fun newConv(id: Int = nextID())
|
||||
{
|
||||
conversations.add(Conversation(id))
|
||||
}
|
||||
}
|
||||
|
||||
class Conversation(val ID: Int)
|
||||
{
|
||||
val messages = mutableListOf<Message>()
|
||||
var newMsg: Boolean = true
|
||||
|
||||
fun write(message: Message)
|
||||
{
|
||||
messages.add(message)
|
||||
newMsg = true
|
||||
}
|
||||
|
||||
override fun toString(): String
|
||||
{
|
||||
newMsg = false
|
||||
|
||||
return messages.last().toString()
|
||||
}
|
||||
|
||||
fun sendAll(): String
|
||||
{
|
||||
val returnString = StringBuilder()
|
||||
|
||||
for (message in messages)
|
||||
{
|
||||
returnString.append(message.toString())
|
||||
returnString.append("\n")
|
||||
}
|
||||
|
||||
return returnString.toString()
|
||||
}
|
||||
}
|
||||
|
||||
data class Message(val user: Boolean, val text: String)
|
||||
{
|
||||
|
||||
|
||||
override fun toString() = """
|
||||
<div class="message-${if (user) "user" else "scott"}">
|
||||
<p>$text</p>
|
||||
</div>
|
||||
""".trimIndent()
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
println("HTTP kiszolgáló a(z) 127.0.0.1:$httpPort címen")
|
||||
|
||||
runEmbeddedServer()
|
||||
}
|
||||
|
||||
fun runEmbeddedServer()
|
||||
{
|
||||
embeddedServer(Netty, port = httpPort) {
|
||||
routing {
|
||||
staticFiles("/resources", File(resourcesPath))
|
||||
staticFiles("/", File(resourcesPath+"/index.html"))
|
||||
|
||||
put("/api/write/{id}")
|
||||
{
|
||||
try {
|
||||
ConversationHandler.conversations.find {
|
||||
it.ID == call.parameters["id"]?.toInt()
|
||||
}?.write(Message(true, call.receiveText()))
|
||||
|
||||
call.respond(HttpStatusCode.OK)
|
||||
|
||||
} catch (e: Exception) { call.respond(HttpStatusCode.NotFound) }
|
||||
|
||||
}
|
||||
|
||||
get("/api/init") {
|
||||
ConversationHandler.newConv()
|
||||
println(ConversationHandler.conversations.last().ID)
|
||||
call.respondText(ConversationHandler.conversations.last().ID.toString())
|
||||
}
|
||||
|
||||
get("/api/all_messages/{id}") {
|
||||
ConversationHandler.conversations.find {
|
||||
it.ID == call.parameters["id"]?.toInt()
|
||||
}.also{ call.respondText(it?.sendAll() ?: "") }
|
||||
}
|
||||
|
||||
get("/api/reinit/{id}") {
|
||||
ConversationHandler.newConv(call.parameters["id"]?.toInt() ?: -1)
|
||||
println(ConversationHandler.conversations.last().ID)
|
||||
call.respond(HttpStatusCode.OK)
|
||||
}
|
||||
|
||||
get("/api/messages/{id}")
|
||||
{
|
||||
ConversationHandler.conversations.find {
|
||||
it.ID == call.parameters["id"]?.toInt()
|
||||
}.also { if (it?.newMsg?:false) call.respondText(it.toString()) else call.respondText("") }
|
||||
}
|
||||
}
|
||||
}.start(wait = true)
|
||||
}
|
||||
Reference in New Issue
Block a user