init commit
This commit is contained in:
135
kiszolgalo/src/main/kotlin/Main.kt
Normal file
135
kiszolgalo/src/main/kotlin/Main.kt
Normal file
@@ -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)
|
||||
}
|
||||
BIN
kiszolgalo/src/main/resources/favicon.ico
Normal file
BIN
kiszolgalo/src/main/resources/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 18 KiB |
0
kiszolgalo/src/main/resources/header.html
Normal file
0
kiszolgalo/src/main/resources/header.html
Normal file
160
kiszolgalo/src/main/resources/index.html
Normal file
160
kiszolgalo/src/main/resources/index.html
Normal file
@@ -0,0 +1,160 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>ScottGPT</title>
|
||||
<link rel="icon" type="image/x-icon" href="/resources/favicon.ico">
|
||||
<link rel="stylesheet" href="/resources/style.css">
|
||||
|
||||
</head>
|
||||
<body onload="javascript:init()">
|
||||
|
||||
<div class="navbar" id="myNavbar">
|
||||
<a href="#" style="float: left">ScottGPT</a>
|
||||
<a href="https://sc0tt.org/" style="float: left" target="_blank">Sc0tt főoldal</a>
|
||||
<a href="#" style="float: left">About</a>
|
||||
|
||||
<img src="resources/logo.png" alt=logo" style="float: right;">
|
||||
|
||||
<a href="javascript:void(0);"
|
||||
class="icon"
|
||||
onclick="toggleMenu()">
|
||||
☰
|
||||
</a>
|
||||
</div>
|
||||
|
||||
<div class="main-content">
|
||||
<div id="messages" class="messages">
|
||||
|
||||
</div>
|
||||
<div class="textbox-parent">
|
||||
<div class="textbox">
|
||||
<input class="text_input" name="valami" type="text" id="promptText" placeholder="Kérdezz az OSI modellről... vagy idk...">
|
||||
<button class="send_button" onclick="send_data()">send</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<script>
|
||||
let id;
|
||||
|
||||
function sleep(ms) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
async function init()
|
||||
{
|
||||
var oldid = localStorage['ID'] || 'gatya';
|
||||
|
||||
if (oldid != 'gatya')
|
||||
{
|
||||
id = oldid;
|
||||
|
||||
let url = "/api/reinit/" + id
|
||||
|
||||
fetch(url)
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
throw new Error('Network response was not ok');
|
||||
}
|
||||
return response.text();
|
||||
})
|
||||
.then(data => {
|
||||
|
||||
console.log(data.body)
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
});
|
||||
|
||||
await sleep(1000);
|
||||
fetch_data("all_messages")
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
fetch('/api/init')
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
throw new Error('Network response was not ok');
|
||||
}
|
||||
return response.text();
|
||||
})
|
||||
.then(data => {
|
||||
|
||||
id = data.toString();
|
||||
|
||||
localStorage['ID'] = data;
|
||||
|
||||
console.log(data.body)
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
});
|
||||
|
||||
await sleep(1000);
|
||||
|
||||
fetch_data("messages");
|
||||
}
|
||||
}
|
||||
|
||||
async function send_data()
|
||||
{
|
||||
let text = document.getElementById("promptText").value;
|
||||
|
||||
let url = "/api/write/" + id
|
||||
|
||||
fetch(url, {
|
||||
method: "PUT",
|
||||
body: text,
|
||||
headers: {
|
||||
"Content-type": "application/json; charset=UTF-8"
|
||||
}
|
||||
});
|
||||
|
||||
//document.getElementById("promptText").value = "";
|
||||
|
||||
await sleep(10);
|
||||
fetch_data("messages", false);
|
||||
|
||||
}
|
||||
|
||||
async function fetch_data(route, recursive = true)
|
||||
{
|
||||
var url = '/api/' + route + '/' + id
|
||||
|
||||
fetch(url)
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
throw new Error('Network response was not ok');
|
||||
}
|
||||
return response.text();
|
||||
})
|
||||
.then(data => {
|
||||
|
||||
document.getElementById("messages").insertAdjacentHTML("afterbegin", data)
|
||||
|
||||
console.log(data.body)
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
});
|
||||
|
||||
await sleep(6000);
|
||||
|
||||
if (recursive) fetch_data("messages");
|
||||
}
|
||||
|
||||
</script>
|
||||
|
||||
<script>
|
||||
function toggleMenu() {
|
||||
let navbar = document.getElementById("myNavbar");
|
||||
navbar.className = navbar.className === "navbar" ?
|
||||
"navbar responsive" : "navbar";
|
||||
}
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
BIN
kiszolgalo/src/main/resources/logo.png
Normal file
BIN
kiszolgalo/src/main/resources/logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 4.3 KiB |
BIN
kiszolgalo/src/main/resources/mogi.png
Normal file
BIN
kiszolgalo/src/main/resources/mogi.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 48 KiB |
BIN
kiszolgalo/src/main/resources/paper-plane.png
Normal file
BIN
kiszolgalo/src/main/resources/paper-plane.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 13 KiB |
137
kiszolgalo/src/main/resources/style.css
Normal file
137
kiszolgalo/src/main/resources/style.css
Normal file
@@ -0,0 +1,137 @@
|
||||
body {
|
||||
background: #212121;
|
||||
color: white;
|
||||
font-family: Roboto, sans-serif;
|
||||
}
|
||||
|
||||
.main-content {
|
||||
display: flex;
|
||||
text-align: center;
|
||||
box-sizing: border-box;
|
||||
justify-content: center;
|
||||
flex-direction: column;
|
||||
min-height: 90vh;
|
||||
}
|
||||
|
||||
.message-user {
|
||||
margin: auto;
|
||||
margin-top: 10px;
|
||||
padding: 0px 20px;
|
||||
text-align: left;
|
||||
width: 60%;
|
||||
color: white;
|
||||
background: black;
|
||||
border-radius: 25px;
|
||||
}
|
||||
|
||||
.messages {
|
||||
display: flex;
|
||||
|
||||
bottom: 20px;
|
||||
margin: 10px auto 0px auto;
|
||||
padding: 10px 0px;
|
||||
flex-direction: column-reverse;
|
||||
width: 80%;
|
||||
overflow:scroll;
|
||||
}
|
||||
|
||||
.textbox-parent {
|
||||
position: fixed;
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
bottom: 0px;
|
||||
width: 100%;
|
||||
display: flex;
|
||||
}
|
||||
|
||||
|
||||
.text_input {
|
||||
justify-content: flex-start;
|
||||
margin: auto;
|
||||
margin-left: 15px;
|
||||
height: 30px;
|
||||
border-radius: 5px;
|
||||
background-color: transparent;
|
||||
white-space: initial;
|
||||
color: white;
|
||||
outline: none;
|
||||
border: none;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.send_button {
|
||||
justify-content: flex-end;
|
||||
padding: 4px 8px;
|
||||
margin: 10px;
|
||||
height: 30px;
|
||||
border-radius: 50%;
|
||||
background: white;
|
||||
color: #212121;
|
||||
border: none;
|
||||
}
|
||||
|
||||
.navbar {
|
||||
overflow: hidden;
|
||||
height: 10%;
|
||||
width: 100%;
|
||||
position: fixed;
|
||||
padding: 5px;
|
||||
background: #212121;
|
||||
top: 0;
|
||||
}
|
||||
.navbar a {
|
||||
display: block;
|
||||
color: white;
|
||||
text-align: center;
|
||||
padding: 4px 8px;
|
||||
margin: 5px 10px;
|
||||
text-decoration: none;
|
||||
border-radius: 5px;
|
||||
}
|
||||
|
||||
.navbar img {
|
||||
height: 32px;
|
||||
margin: auto 5px;
|
||||
}
|
||||
|
||||
.navbar a:hover {
|
||||
background-color: #545454;
|
||||
}
|
||||
.navbar a.icon {
|
||||
display: none;
|
||||
}
|
||||
@media screen and (max-width: 600px) {
|
||||
.textbox {
|
||||
display: flex;
|
||||
position: relative;
|
||||
justify-content: center;
|
||||
width: 60%;
|
||||
margin: 5px;
|
||||
background: #2f2f2f;
|
||||
border-radius: 25px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
|
||||
.navbar a:not(:first-child) {
|
||||
display: none;
|
||||
}
|
||||
.navbar a.icon {
|
||||
float: right;
|
||||
display: block;
|
||||
}
|
||||
.navbar.responsive {
|
||||
position: relative;
|
||||
}
|
||||
.navbar.responsive a.icon {
|
||||
position: absolute;
|
||||
right: 0;
|
||||
top: 0;
|
||||
}
|
||||
.navbar.responsive a {
|
||||
float: none;
|
||||
display: block;
|
||||
text-align: left;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user