commit 20/08/2025
This commit is contained in:
145
src/web/WebApp.kt
Normal file
145
src/web/WebApp.kt
Normal file
@@ -0,0 +1,145 @@
|
||||
package web
|
||||
|
||||
import codes.Somecodes
|
||||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||
import io.javalin.Javalin
|
||||
import io.javalin.apibuilder.ApiBuilder.before
|
||||
import io.javalin.apibuilder.ApiBuilder.get
|
||||
import io.javalin.apibuilder.ApiBuilder.path
|
||||
import io.javalin.apibuilder.ApiBuilder.post
|
||||
import io.javalin.apibuilder.ApiBuilder.ws
|
||||
import io.javalin.http.Context
|
||||
|
||||
@Suppress("unused")
|
||||
class WebApp(val listenPort: Int, val userlist: List<Pair<String, String>>) {
|
||||
|
||||
var app : Javalin? = null
|
||||
private val objectmapper = jacksonObjectMapper()
|
||||
|
||||
fun Start() {
|
||||
app = Javalin.create {
|
||||
config ->
|
||||
config.useVirtualThreads = true
|
||||
config.staticFiles.add("/webpage")
|
||||
config.router.apiBuilder {
|
||||
path("/"){
|
||||
get { ctx ->
|
||||
// Serve the main page
|
||||
ctx.sessionAttribute("user", null) // Clear user session
|
||||
ctx.redirect("login.html")
|
||||
}
|
||||
|
||||
}
|
||||
path("login.html"){
|
||||
post{ it ->
|
||||
// get username and password from form
|
||||
val username = it.formParam("username")
|
||||
val password = it.formParam("password")
|
||||
if (username == null || password == null) {
|
||||
it.status(400).result("Username and password are required")
|
||||
return@post
|
||||
}
|
||||
// Check if user exists in userlist
|
||||
val user = userlist.find { it.first == username && it.second == password }
|
||||
if (user == null) {
|
||||
it.status(401).result("Invalid username or password")
|
||||
return@post
|
||||
}
|
||||
// Set user session
|
||||
it.sessionAttribute("user", user.first)
|
||||
println("User ${user.first} logged in")
|
||||
// Redirect to home page
|
||||
it.redirect("home.html")
|
||||
}
|
||||
}
|
||||
|
||||
path("home.html") {
|
||||
before { CheckUsers(it) }
|
||||
ws("/ws") { it ->
|
||||
// WebSocket endpoint for home
|
||||
it.onClose {
|
||||
// TODO Handle WebSocket close event
|
||||
println("WebSocket closed: ${it.session.remoteAddress}")
|
||||
}
|
||||
it.onMessage {
|
||||
try{
|
||||
val cmd = objectmapper.readValue(it.message(), WebsocketCommand::class.java)
|
||||
when (cmd.command) {
|
||||
"getCPUStatus" ->{
|
||||
//TODO Get CPU status
|
||||
|
||||
it.send(objectmapper.writeValueAsString(WebsocketReply(cmd.command,"OK")))
|
||||
}
|
||||
"getMemoryStatus" ->{
|
||||
// TODO Get Memory status
|
||||
it.send(objectmapper.writeValueAsString(WebsocketReply(cmd.command, Somecodes.getMemoryUsage())))
|
||||
}
|
||||
"getDiskStatus" ->{
|
||||
// TODO Get Disk status
|
||||
it.send(objectmapper.writeValueAsString(WebsocketReply(cmd.command,"OK")))
|
||||
}
|
||||
"getNetworkStatus" ->{
|
||||
// TODO Get Network status
|
||||
it.send(objectmapper.writeValueAsString(WebsocketReply(cmd.command,"OK")))
|
||||
}
|
||||
else -> {
|
||||
it.send(objectmapper.writeValueAsString(WebsocketReply("error", "Unknown command: ${cmd.command}")))
|
||||
}
|
||||
}
|
||||
} catch (e: Exception){
|
||||
println("Error processing WebSocket message: ${e.message}")
|
||||
}
|
||||
|
||||
}
|
||||
it.onConnect {
|
||||
// TODO Handle WebSocket connect event
|
||||
println("WebSocket connected: ${it.session.remoteAddress}")
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
path("soundbank.html") {
|
||||
before {CheckUsers(it)}
|
||||
}
|
||||
path("messagebank.html") {
|
||||
before { CheckUsers(it) }
|
||||
}
|
||||
path("language.html") {
|
||||
before { CheckUsers(it) }
|
||||
}
|
||||
path("log.html") {
|
||||
before { CheckUsers(it) }
|
||||
}
|
||||
path("setting.html") {
|
||||
before { CheckUsers(it) }
|
||||
}
|
||||
path("timer.html") {
|
||||
before { CheckUsers(it) }
|
||||
}
|
||||
}
|
||||
}.start(listenPort)
|
||||
|
||||
}
|
||||
|
||||
fun CheckUsers(ctx: Context){
|
||||
println("Checking user session at ${ctx.req().requestURI}")
|
||||
val user = ctx.sessionAttribute<String?>("user")
|
||||
if (user == null) {
|
||||
println("User not logged in, redirecting to login page")
|
||||
ctx.redirect("login.html")
|
||||
}
|
||||
println("User is logged in: $user")
|
||||
val foundUser = userlist.find { it.first == user }
|
||||
if (foundUser==null) {
|
||||
println("User not found in user list, redirecting to login page")
|
||||
ctx.redirect("login.html")
|
||||
} else {
|
||||
println("User found: $user")
|
||||
}
|
||||
}
|
||||
|
||||
fun Stop(){
|
||||
app?.stop()
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user