SSE in working state

This commit is contained in:
Maximilian Wagner
2025-12-26 01:37:23 +01:00
parent aa76d9c721
commit a6214e022f
11 changed files with 116 additions and 71 deletions

39
main.go
View File

@@ -16,52 +16,59 @@ import (
// Exit Codes:
// 0 - ok
// 1 - config file could not be read
// 2 - log file could not be accessed
// 1 - log file could not be accessed
// 2 - config file could not be read
func main() {
// application init
err := backend.ConfigReader("config.json", &globals.AppConfig)
// temporary logfile
var err error
globals.LogFile, err = os.OpenFile("./servtex.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
defer globals.LogFile.Close()
// parse configuration
err = backend.ConfigReader("config.json", &globals.AppConfig)
if err != nil { os.Exit(1) }
// user-chosen logfile
globals.LogFile, err = os.OpenFile(globals.AppConfig.LogFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil { os.Exit(2) }
globals.LatexExec.Timestamp = "mytimestamp"
// webserver init
server := &http.Server{Addr: globals.AppConfig.WebserverDomain + ":" + globals.AppConfig.WebserverPort}
serverSecure := &http.Server{Addr: globals.AppConfig.WebserverDomain + ":" + globals.AppConfig.WebserverPortSecure}
// website url paths
http.HandleFunc("/", frontend.MainHandler)
http.HandleFunc("/sse", frontend.SSEventHandler)
http.HandleFunc("/pdf", frontend.PDFHandler)
http.HandleFunc("/compile", frontend.PDFCompile)
jscss, _ := fs.Sub(frontend.WebFiles, "jscss")
http.Handle("/jscss/", http.StripPrefix("/jscss/", http.FileServer(http.FS(jscss))))
// rocket
go server.ListenAndServe()
if globals.AppConfig.WebserverSecure {
go serverSecure.ListenAndServeTLS(globals.AppConfig.CertificatePath, globals.AppConfig.CertificateKeyPath)
}
backend.LogLine("Started")
// shutdown logic
backend.LogLine("Started", 2)
fmt.Println("Press CTRL-C to Exit ServTeX")
// wait for signal to quit
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
<-stop
context, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// remove ^C from stdout
fmt.Print("\r")
// shutdown
context, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
if err = server.Shutdown(context); err != nil {
backend.LogLine("Graceful Shutdown failed")
backend.LogLine("Graceful Shutdown failed", 4)
}
if err = serverSecure.Shutdown(context); err != nil {
backend.LogLine("Graceful Shutdown failed")
backend.LogLine("Graceful Shutdown failed", 4)
}
backend.LogLine("Stopped")
backend.LogLine("Stopped", 2)
}