diff --git a/kiszolgalo/build.gradle.kts b/kiszolgalo/build.gradle.kts index 491cf4c..26e9de0 100644 --- a/kiszolgalo/build.gradle.kts +++ b/kiszolgalo/build.gradle.kts @@ -25,6 +25,7 @@ dependencies { implementation("io.ktor:ktor-server-core:$ktor_version") implementation("io.ktor:ktor-server-netty:$ktor_version") implementation("io.ktor:ktor-server-sessions:${ktor_version}") + implementation("io.ktor:ktor-server-auth:${ktor_version}") } tasks.test { diff --git a/kiszolgalo/src/main/kotlin/Main.kt b/kiszolgalo/src/main/kotlin/Main.kt index be2332a..d0c739b 100644 --- a/kiszolgalo/src/main/kotlin/Main.kt +++ b/kiszolgalo/src/main/kotlin/Main.kt @@ -11,11 +11,17 @@ import java.io.File import kotlin.random.Random import kotlin.random.nextInt +import io.ktor.server.auth.* +import io.ktor.server.application.* + //átlagos JAVA alapú fejlesztő val httpPort: Int = System.getenv("HTTP_PORT")?.toInt() ?: 8080 val resourcesPath: String = System.getenv("RESOURCES") ?: "src/main/resources/" +val adminUser: String = System.getenv("ADMIN_USER") ?: "foo" +val adminPass: String = System.getenv("ADMIN_PASSWORD") ?: "bar" + val bullshit = listOf( "That's a great question/opinion/response! ", "I'm so proud of you for coming up with that. ", @@ -49,11 +55,19 @@ val bullshit = listOf( "I eat cement. ", "Answering these kinds of questions wasn't in my job description btw. ", "Let's see... ", - "Let's see - hold on someone is calling - holy shit. A Second one? Oh my. Oh god. Anyways where were we? Right. ", - "Let's see - hold on someone is calling - it's for you, some 'Feri' I think. ", + "Let's see - hold on someone is calling - holy shit. A second one? Oh my. Oh god. That's horrible. Anyways where were we? Right. ", + "Let's see - hold on someone is calling - it's for you, some 'Feri' they say. ", "Let's see what we have here. ", "Really? You have to ask the computer to figure that one out? Alright man I can look into it... ", "Let's take a look!", + "You should talk about this to a medical professional, but for now here is what I think: ", + "You kick Sc0tt? You kick his body like the Football? Jail for User! Jail for User for one thousand years! ", + "You're on the right track! ", + "You've almost got it! ", + "That is almost correct! ", + "Here are my 2 cents on this topic: ", + "I will piss on the moon. ", + "Here is what I think: ", ) object ConversationHandler { @@ -75,8 +89,6 @@ object ConversationHandler { { val stringBuilder = StringBuilder() - - for (conversation in conversations) { stringBuilder.append(conversation.list()) @@ -105,7 +117,8 @@ class Conversation(val ID: Int) fun list(): String { - return """ + return if(messages.isEmpty()) " " + else """

$ID

${messages.last().text}

@@ -160,6 +173,7 @@ class Message(val user: Boolean, messagetext: String) { return """
+

${if (user) "You asked:" else "Sc0tt answered:"}

$text

""".trimIndent() @@ -175,38 +189,53 @@ fun main(args: Array) { fun runEmbeddedServer() { embeddedServer(Netty, port = httpPort) { + install(Authentication) { + basic("auth-basic") { + realm = "Access to the '/admin' path" + validate { credentials -> + if (credentials.name == adminUser && credentials.password == adminPass) { + UserIdPrincipal(credentials.name) + } else { + null + } + } + } + } + routing { + authenticate("auth-basic") { + route("/admin") + { + get("/api/all_messages") { + call.respondText(ConversationHandler.listAllConversations()) + } + + get("/api/all_messages/{id}") { + ConversationHandler.conversations.find { + it.ID == call.parameters["id"]?.toInt() + }.also{ call.respondText(it?.sendAll(true) ?: "") } + } + + put("/api/write/{id}") + { + try { + ConversationHandler.conversations.find { + it.ID == call.parameters["id"]?.toInt() + }?.write(Message(false, call.receiveText())) + + call.respond(HttpStatusCode.OK) + + } catch (e: Exception) { call.respond(HttpStatusCode.NotFound) } + + } + } + } + staticFiles("/resources", File(resourcesPath)) staticFiles("/", File(resourcesPath+"/index.html")) staticFiles("/admin", File(resourcesPath+"/admin.html")) staticFiles("/about", File(resourcesPath+"/about.html")) - route("/admin") - { - get("/api/all_messages") { - call.respondText(ConversationHandler.listAllConversations()) - } - - get("/api/all_messages/{id}") { - ConversationHandler.conversations.find { - it.ID == call.parameters["id"]?.toInt() - }.also{ call.respondText(it?.sendAll(true) ?: "") } - } - - put("/api/write/{id}") - { - try { - ConversationHandler.conversations.find { - it.ID == call.parameters["id"]?.toInt() - }?.write(Message(false, call.receiveText())) - - call.respond(HttpStatusCode.OK) - - } catch (e: Exception) { call.respond(HttpStatusCode.NotFound) } - - } - } - put("/api/write/{id}") { try { diff --git a/kiszolgalo/src/main/resources/about.html b/kiszolgalo/src/main/resources/about.html index 8cd8a3d..8c6f9bd 100644 --- a/kiszolgalo/src/main/resources/about.html +++ b/kiszolgalo/src/main/resources/about.html @@ -7,7 +7,7 @@ - +