wolbodge/routes/api.go
2025-03-22 13:19:55 +01:00

45 lines
971 B
Go

package routes
import (
"fmt"
"net/http"
"strings"
"time"
"git.1e99.eu/1e99/wolbodge/sessions"
)
func APIStartSession(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
}
description := req.FormValue("description")
if strings.TrimSpace(description) == "" {
description = "description"
}
session, err := storage.StartSession(description, time.Now())
if err != nil {
http.Error(res, "", http.StatusInternalServerError)
return
}
fmt.Fprintf(res, "%s", session.Id)
}
}
func APIEndSession(storage sessions.Storage) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
sessionId := req.PathValue("session_id")
err := storage.EndSession(sessionId)
if err != nil {
http.Error(res, "", http.StatusInternalServerError)
return
}
}
}