second commit 20/01/2026
This commit is contained in:
@@ -2,20 +2,110 @@ package Web
|
||||
|
||||
import config
|
||||
import io.javalin.Javalin
|
||||
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 org.tinylog.Logger
|
||||
|
||||
@Suppress("unused")
|
||||
/**
|
||||
* Start WebUI Server
|
||||
*/
|
||||
class WebUI{
|
||||
private var app : Javalin = Javalin.create { config ->
|
||||
config.staticFiles.add("/html")
|
||||
}.start(config.WebPort)
|
||||
private var app : Javalin = Javalin.create { cfg ->
|
||||
cfg.staticFiles.add("/html")
|
||||
cfg.router.apiBuilder {
|
||||
path("/"){
|
||||
get {
|
||||
if (config.WebUsername==it.cookie("username")){
|
||||
it.redirect("home.html")
|
||||
} else{
|
||||
it.redirect("login.html")
|
||||
}
|
||||
}
|
||||
}
|
||||
path("login"){
|
||||
post{
|
||||
val username = it.formParam("username")
|
||||
val password = it.formParam("password")
|
||||
if (config.WebUsername==username && config.WebPassword==password) {
|
||||
it.cookie("username", username)
|
||||
it.redirect("home.html")
|
||||
} else {
|
||||
it.redirect("/login.html?error=1")
|
||||
}
|
||||
}
|
||||
}
|
||||
path("logout"){
|
||||
get {
|
||||
it.removeCookie("username")
|
||||
it.redirect("login.html")
|
||||
}
|
||||
}
|
||||
path("login.html"){
|
||||
get {
|
||||
it.removeCookie("username")
|
||||
}
|
||||
}
|
||||
path("home.html"){
|
||||
|
||||
get {
|
||||
if (config.WebUsername!=it.cookie("username")){
|
||||
it.redirect("login.html")
|
||||
return@get
|
||||
}
|
||||
|
||||
}
|
||||
ws("/ws"){ ws ->
|
||||
ws.onConnect { wsconnectcontext -> Logger.info { "WebSocket connected: ${wsconnectcontext.sessionId()}"; wsconnectcontext.enableAutomaticPings() } }
|
||||
ws.onClose { wsclosecontext -> Logger.info { "WebSocket closed: ${wsclosecontext.sessionId()}" } }
|
||||
ws.onError { wserrorcontext -> Logger.error { "WebSocket error in session ${wserrorcontext.sessionId()}: ${wserrorcontext.error()?.message}" } }
|
||||
ws.onMessage { wsMessageContext ->
|
||||
// TODO handle incoming messages
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
path("log.html"){
|
||||
get {
|
||||
if (config.WebUsername!=it.cookie("username")){
|
||||
it.redirect("login.html")
|
||||
return@get
|
||||
}
|
||||
val logdate = it.queryParam("logdate")
|
||||
if (logdate.isNullOrEmpty()) return@get
|
||||
|
||||
}
|
||||
}
|
||||
path("setting.html"){
|
||||
get {
|
||||
if (config.WebUsername!=it.cookie("username")){
|
||||
it.redirect("login.html")
|
||||
return@get
|
||||
}
|
||||
// TODO send config values in JSON format
|
||||
}
|
||||
post {
|
||||
// TODO save config values from form parameters
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Start WebUI Server
|
||||
*/
|
||||
fun Start(){
|
||||
app.start(config.WebPort)
|
||||
}
|
||||
/**
|
||||
* Stop WebUI Server
|
||||
*/
|
||||
fun Stop(){
|
||||
app.stop()
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user