From 3aef75a73e0ddc8ff30724e3779efcbd33bba290 Mon Sep 17 00:00:00 2001 From: 1e99 Date: Sat, 12 Apr 2025 11:33:40 +0200 Subject: [PATCH] add option in UI to delete past sessions --- index.tmpl | 7 +++++++ main.go | 1 + routes/index.go | 24 ++++++++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/index.tmpl b/index.tmpl index be3cfad..05566db 100644 --- a/index.tmpl +++ b/index.tmpl @@ -88,6 +88,7 @@ Description Created at Ended at + Actions @@ -98,6 +99,12 @@ {{ $session.Description }} {{ $session.CreatedAt.Format "02.01.2006 15:04:05" }} {{ $session.EndedAt.Format "02.01.2006 15:04:05" }} + +
+ + +
+ {{ end }} diff --git a/main.go b/main.go index 92b874f..fd82b7b 100644 --- a/main.go +++ b/main.go @@ -48,6 +48,7 @@ func Run() error { mux.Handle("GET /", routes.GetIndex(device, storage, tmpl)) mux.Handle("POST /start_session", routes.StartSession(logger, storage)) mux.Handle("POST /end_session", routes.EndSession(logger, storage)) + mux.Handle("POST /delete_session", routes.DeleteSession(logger, storage)) mux.Handle("POST /api/session", routes.APIStartSession(logger, storage)) mux.Handle("DELETE /api/session/{session_id}", routes.APIEndSession(logger, storage)) diff --git a/routes/index.go b/routes/index.go index 0dd2c8e..608af97 100644 --- a/routes/index.go +++ b/routes/index.go @@ -83,3 +83,27 @@ func EndSession(logger *log.Logger, storage sessions.Storage) http.HandlerFunc { logger.Printf("Ended session for %s via webpanel (id = %s)", req.RemoteAddr, sessionId) } } + +func DeleteSession(logger *log.Logger, storage sessions.Storage) http.HandlerFunc { + return func(res http.ResponseWriter, req *http.Request) { + err := req.ParseMultipartForm(10 * 1024) + if err != nil { + http.Error(res, "", http.StatusBadRequest) + return + } + + sessionId := req.FormValue("session_id") + err = storage.DeleteSession(sessionId) + switch { + case err == sessions.ErrUnknownSession: + http.Error(res, "", http.StatusNotFound) + return + case err != nil: + http.Error(res, "", http.StatusInternalServerError) + return + } + + http.Redirect(res, req, "/", http.StatusFound) + logger.Printf("Deleted session for %s via webpanel (id = %s)", req.RemoteAddr, sessionId) + } +}