add logging
This commit is contained in:
8
kiszolgalo/src/main/kotlin/Logger.kt
Normal file
8
kiszolgalo/src/main/kotlin/Logger.kt
Normal file
@@ -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")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -63,11 +63,11 @@ val bullshit = listOf<String>(
|
|||||||
"You should talk about this to a medical professional, but for now here is what I think: ",
|
"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 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're on the right track! ",
|
||||||
"You've almost got it! ",
|
"You've almost got it!",
|
||||||
"That is almost correct! ",
|
"That is almost correct!",
|
||||||
"Here are my 2 cents on this topic: ",
|
"Here are my 2 cents on this topic:",
|
||||||
"I will piss on the moon. ",
|
"I will piss on the moon.",
|
||||||
"Here is what I think: ",
|
"Here is what I think:",
|
||||||
)
|
)
|
||||||
|
|
||||||
object ConversationHandler {
|
object ConversationHandler {
|
||||||
@@ -165,7 +165,7 @@ class Message(val user: Boolean, messagetext: String)
|
|||||||
val text: String
|
val text: String
|
||||||
|
|
||||||
init {
|
init {
|
||||||
text = if (!user) bullshit[Random.nextInt(0..bullshit.size)] + messagetext
|
text = if (!user) bullshit[Random.nextInt(0..bullshit.size)].trim(' ') + ' ' + messagetext
|
||||||
else messagetext
|
else messagetext
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -181,7 +181,7 @@ class Message(val user: Boolean, messagetext: String)
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun main(args: Array<String>) {
|
fun main(args: Array<String>) {
|
||||||
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()
|
runEmbeddedServer()
|
||||||
}
|
}
|
||||||
@@ -207,13 +207,16 @@ fun runEmbeddedServer()
|
|||||||
route("/admin")
|
route("/admin")
|
||||||
{
|
{
|
||||||
get("/api/all_messages") {
|
get("/api/all_messages") {
|
||||||
|
Logger.log("Admin login")
|
||||||
call.respondText(ConversationHandler.listAllConversations())
|
call.respondText(ConversationHandler.listAllConversations())
|
||||||
}
|
}
|
||||||
|
|
||||||
get("/api/all_messages/{id}") {
|
get("/api/all_messages/{id}") {
|
||||||
ConversationHandler.conversations.find {
|
ConversationHandler.conversations.find {
|
||||||
it.ID == call.parameters["id"]?.toInt()
|
it.ID == call.parameters["id"]?.toInt()
|
||||||
}.also{ call.respondText(it?.sendAll(true) ?: "") }
|
}.also{
|
||||||
|
call.respondText(it?.sendAll(true) ?: "")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
put("/api/write/{id}")
|
put("/api/write/{id}")
|
||||||
@@ -225,15 +228,18 @@ fun runEmbeddedServer()
|
|||||||
|
|
||||||
call.respond(HttpStatusCode.OK)
|
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("/resources", File(resourcesPath))
|
||||||
staticFiles("/", File(resourcesPath+"/index.html"))
|
staticFiles("/", File(resourcesPath+"/index.html"))
|
||||||
staticFiles("/admin", File(resourcesPath+"/admin.html"))
|
|
||||||
staticFiles("/about", File(resourcesPath+"/about.html"))
|
staticFiles("/about", File(resourcesPath+"/about.html"))
|
||||||
|
|
||||||
put("/api/write/{id}")
|
put("/api/write/{id}")
|
||||||
@@ -245,7 +251,10 @@ fun runEmbeddedServer()
|
|||||||
|
|
||||||
call.respond(HttpStatusCode.OK)
|
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") {
|
get("/api/init") {
|
||||||
ConversationHandler.newConv()
|
ConversationHandler.newConv()
|
||||||
println(ConversationHandler.conversations.last().ID)
|
Logger.log("Conversation ${ConversationHandler.conversations.last().ID} initialized")
|
||||||
call.respondText(ConversationHandler.conversations.last().ID.toString())
|
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)
|
if (ConversationHandler.conversations.filter { it.ID == call.parameters["id"]?.toInt()}.size != 1)
|
||||||
{
|
{
|
||||||
ConversationHandler.newConv(call.parameters["id"]?.toInt() ?: -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)
|
call.respond(HttpStatusCode.OK)
|
||||||
|
|||||||
Reference in New Issue
Block a user