logging and client info parsing changes, added trusted proxies

This commit is contained in:
Maximilian Wagner
2025-12-27 18:02:02 +01:00
parent 8dcf9fba11
commit ad3825c871
7 changed files with 211 additions and 32 deletions

View File

@@ -70,30 +70,46 @@ func sseOutputSend(writer *http.ResponseWriter) {
backend.LogLine("Output Event has been sent", 1)
}
// Server Side Event Handler
//
// Sends a Ping instead of actual data when no new data available to save bandwidth
func SSEventHandler(writer http.ResponseWriter, request *http.Request) {
if err := backend.VerifyLogRequest(request); err != nil { http.Error(writer, "Bad Request", http.StatusBadRequest) }
writer.Header().Set("Content-Type", "text/event-stream")
writer.Header().Set("Cache-Control", "no-cache")
writer.Header().Set("Connection", "keep-alive")
ssePing(&writer)
lastExecution := "startup"
lastExecution := ""
for range time.Tick(time.Second) {
if lastExecution == globals.LatexExec.TimestampRFC {
ssePing(&writer)
} else {
sseStatusSend(&writer)
ssePDFSend(&writer)
sseOutputSend(&writer)
lastExecution = globals.LatexExec.TimestampRFC
}
select {
case <-request.Context().Done():
backend.LogLine("SSE Context Done", 1)
return
default:
if lastExecution == globals.LatexExec.TimestampRFC {
ssePing(&writer)
} else {
sseStatusSend(&writer)
sseOutputSend(&writer)
// let client keep current pdf, if compile failed
if globals.LatexExec.ExecutionState != "Failed" {
ssePDFSend(&writer)
}
lastExecution = globals.LatexExec.TimestampRFC
}
}
}
}
// Serves the compiled PDF file
func PDFHandler(writer http.ResponseWriter, request *http.Request) {
if err := backend.VerifyLogRequest(request); err != nil { http.Error(writer, "Bad Request", http.StatusBadRequest) }
pdfPath := filepath.Join(globals.AppConfig.LatexOutputPath, "servtex.pdf")
pdf, err := os.Open(pdfPath)
if err != nil {
@@ -109,12 +125,16 @@ func PDFHandler(writer http.ResponseWriter, request *http.Request) {
// Serves the main page of ServTeX
func MainHandler(writer http.ResponseWriter, request *http.Request) {
if err := backend.VerifyLogRequest(request); err != nil { http.Error(writer, "Bad Request", http.StatusBadRequest) }
writer.Header().Set("Content-Type", "text/html")
main, _ := WebFiles.ReadFile("templates/main.html")
writer.Write(main)
}
func PDFCompile(writer http.ResponseWriter, request *http.Request) {
if err := backend.VerifyLogRequest(request); err != nil { http.Error(writer, "Bad Request", http.StatusBadRequest) }
backend.LatexCompile()
}