webserver up and running for http/https, logger timestamp format change

This commit is contained in:
Maximilian Wagner
2025-12-25 23:31:48 +01:00
parent a54c4cf9ff
commit aa76d9c721
12 changed files with 190 additions and 62 deletions

54
main.go
View File

@@ -2,20 +2,66 @@ package main
import (
"os"
"git.noctra.dev/noctra/servtex/globals"
"os/signal"
"syscall"
"io/fs"
"context"
"fmt"
"time"
"net/http"
"git.noctra.dev/noctra/servtex/backend"
//"net/http"
"git.noctra.dev/noctra/servtex/frontend"
"git.noctra.dev/noctra/servtex/globals"
)
// Exit Codes:
// 0 - ok
// 1 - config file could not be read
// 2 - log file could not be accessed
func main() {
// application init
err := backend.ConfigReader("config.json", &globals.AppConfig)
if err != nil { os.Exit(1) }
globals.LogFile, err = os.Open(globals.AppConfig.LogFilePath)
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}
http.HandleFunc("/", frontend.MainHandler)
http.HandleFunc("/sse", frontend.SSEventHandler)
http.HandleFunc("/pdf", frontend.PDFHandler)
jscss, _ := fs.Sub(frontend.WebFiles, "jscss")
http.Handle("/jscss/", http.StripPrefix("/jscss/", http.FileServer(http.FS(jscss))))
go server.ListenAndServe()
if globals.AppConfig.WebserverSecure {
go serverSecure.ListenAndServeTLS(globals.AppConfig.CertificatePath, globals.AppConfig.CertificateKeyPath)
}
backend.LogLine("Started")
// shutdown logic
fmt.Println("Press CTRL-C to Exit ServTeX")
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()
fmt.Print("\r")
if err = server.Shutdown(context); err != nil {
backend.LogLine("Graceful Shutdown failed")
}
if err = serverSecure.Shutdown(context); err != nil {
backend.LogLine("Graceful Shutdown failed")
}
backend.LogLine("Stopped")
}