diff --git a/kiszolgalo/src/main/kotlin/Logger.kt b/kiszolgalo/src/main/kotlin/Logger.kt new file mode 100644 index 0000000..16d94dc --- /dev/null +++ b/kiszolgalo/src/main/kotlin/Logger.kt @@ -0,0 +1,8 @@ +import java.time.LocalDateTime +import java.time.format.DateTimeFormatter + +object Logger { + fun log(message: Any) { + println("[${LocalDateTime.now().format(DateTimeFormatter.ofPattern("YYYY-MM-DD-HH:mm:ss"))}]: $message") + } +} \ No newline at end of file diff --git a/kiszolgalo/src/main/kotlin/Main.kt b/kiszolgalo/src/main/kotlin/Main.kt index d0c739b..1ecb6fe 100644 --- a/kiszolgalo/src/main/kotlin/Main.kt +++ b/kiszolgalo/src/main/kotlin/Main.kt @@ -63,11 +63,11 @@ val bullshit = listOf( "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: ", + "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 { @@ -165,7 +165,7 @@ class Message(val user: Boolean, messagetext: String) val text: String init { - text = if (!user) bullshit[Random.nextInt(0..bullshit.size)] + messagetext + text = if (!user) bullshit[Random.nextInt(0..bullshit.size)].trim(' ') + ' ' + messagetext else messagetext } @@ -181,7 +181,7 @@ class Message(val user: Boolean, messagetext: String) } fun main(args: Array) { - println("HTTP kiszolgáló a(z) 127.0.0.1:$httpPort címen") + Logger.log("HTTP server started on 127.0.0.1:$httpPort") runEmbeddedServer() } @@ -207,13 +207,16 @@ fun runEmbeddedServer() route("/admin") { get("/api/all_messages") { + Logger.log("Admin login") call.respondText(ConversationHandler.listAllConversations()) } get("/api/all_messages/{id}") { ConversationHandler.conversations.find { it.ID == call.parameters["id"]?.toInt() - }.also{ call.respondText(it?.sendAll(true) ?: "") } + }.also{ + call.respondText(it?.sendAll(true) ?: "") + } } put("/api/write/{id}") @@ -225,15 +228,18 @@ fun runEmbeddedServer() call.respond(HttpStatusCode.OK) - } catch (e: Exception) { call.respond(HttpStatusCode.NotFound) } + } catch (e: Exception) { + Logger.log("Error: ${e.message}") + call.respond(HttpStatusCode.NotFound) + } } } } + staticFiles("/admin", File(resourcesPath+"/admin.html")) staticFiles("/resources", File(resourcesPath)) staticFiles("/", File(resourcesPath+"/index.html")) - staticFiles("/admin", File(resourcesPath+"/admin.html")) staticFiles("/about", File(resourcesPath+"/about.html")) put("/api/write/{id}") @@ -245,7 +251,10 @@ fun runEmbeddedServer() call.respond(HttpStatusCode.OK) - } catch (e: Exception) { call.respond(HttpStatusCode.NotFound) } + } catch (e: Exception) { + call.respond(HttpStatusCode.NotFound) + Logger.log("Error: ${e.message}") + } } @@ -257,7 +266,7 @@ fun runEmbeddedServer() get("/api/init") { ConversationHandler.newConv() - println(ConversationHandler.conversations.last().ID) + Logger.log("Conversation ${ConversationHandler.conversations.last().ID} initialized") call.respondText(ConversationHandler.conversations.last().ID.toString()) } @@ -265,7 +274,7 @@ fun runEmbeddedServer() if (ConversationHandler.conversations.filter { it.ID == call.parameters["id"]?.toInt()}.size != 1) { ConversationHandler.newConv(call.parameters["id"]?.toInt() ?: -1) - println(ConversationHandler.conversations.last().ID) + Logger.log("Conversation #${ConversationHandler.conversations.last().ID} reinitialised") } call.respond(HttpStatusCode.OK)