package main import ( "os" "os/signal" "syscall" "io/fs" "context" "fmt" "time" "net/http" "git.noctra.dev/noctra/servtex/backend" "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.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") }