88 lines
2.4 KiB
Go
88 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io/fs"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"path/filepath"
|
|
"syscall"
|
|
"time"
|
|
|
|
"git.noctra.dev/noctra/servtex/backend"
|
|
"git.noctra.dev/noctra/servtex/frontend"
|
|
"git.noctra.dev/noctra/servtex/globals"
|
|
)
|
|
|
|
// Exit Codes:
|
|
// 0 - ok
|
|
// 1 - log file could not be accessed
|
|
// 2 - config file could not be read
|
|
func main() {
|
|
// 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) }
|
|
|
|
// 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))))
|
|
|
|
favicon, _ := fs.Sub(frontend.WebFiles, "media")
|
|
http.Handle("/favicon.ico", http.FileServer(http.FS(favicon)))
|
|
|
|
// rocket
|
|
go server.ListenAndServe()
|
|
if globals.AppConfig.WebserverSecure {
|
|
go serverSecure.ListenAndServeTLS(globals.AppConfig.CertificatePath, globals.AppConfig.CertificateKeyPath)
|
|
}
|
|
|
|
// file system change watch
|
|
absTexPath, err := filepath.Abs(globals.AppConfig.LatexSourceFilePath)
|
|
if err == nil {
|
|
backend.ChangeWatch(filepath.Dir(absTexPath), backend.LatexCompile)
|
|
} else {
|
|
backend.LogLine("Absolute TeX file path could not be gotten", 4)
|
|
}
|
|
|
|
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
|
|
|
|
// remove ^C from stdout
|
|
fmt.Print("\r")
|
|
|
|
// shutdown
|
|
// known issue: sse blocks shutdown if a client is still connected
|
|
context, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
defer cancel()
|
|
server.Shutdown(context)
|
|
serverSecure.Shutdown(context)
|
|
|
|
backend.LogLine("Stopped", 2)
|
|
}
|
|
|