logger, compiler logic, sse endpoints
This commit is contained in:
@@ -2,26 +2,45 @@ package backend
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.noctra.dev/noctra/servtex/globals"
|
||||
"time"
|
||||
)
|
||||
|
||||
func getLocalTime() time.Time {
|
||||
timezone, _ := time.LoadLocation(globals.AppConfig.Timezone)
|
||||
return time.Now().In(timezone)
|
||||
}
|
||||
|
||||
func getLocalTimePretty() string {
|
||||
return getLocalTime().Format("02.01.2006 15:04")
|
||||
}
|
||||
|
||||
func LogLine(message string) {
|
||||
logline := "\n" + getLocalTime().Format(time.RFC3339) + " - " + message
|
||||
globals.LogFile.WriteString(logline)
|
||||
}
|
||||
|
||||
// Helper for configReader() that does the actual reading
|
||||
// Also validates the populated fields
|
||||
func configReaderParse(filePath string, configOptionStorage *globals.Config) error {
|
||||
jsonData, err := os.ReadFile(filePath)
|
||||
if err == nil {
|
||||
err = json.Unmarshal(jsonData, &configOptionStorage)
|
||||
if err != nil {
|
||||
// log error unmarshal failure
|
||||
return errors.New("Config file could not be read")
|
||||
}
|
||||
} else {
|
||||
return errors.New("Config file does not exist")
|
||||
}
|
||||
|
||||
return configValidator(*configOptionStorage)
|
||||
}
|
||||
|
||||
func configValidator(config globals.Config) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -48,34 +67,52 @@ func ConfigReader(configFileName string, configOptionStorage *globals.Config) er
|
||||
err = configReaderParse(path, configOptionStorage)
|
||||
if err == nil { return nil }
|
||||
|
||||
// log error no config file found
|
||||
return errors.New("Configuration file not found or json parsing error")
|
||||
return err
|
||||
}
|
||||
|
||||
// Watches a specified directory and calls a function on change
|
||||
// Optionally, a minimum time to wait for consecutive changes can be specified
|
||||
func ChangeWatch(path string, callOnChange func()) {
|
||||
LogLine("File change noticed")
|
||||
}
|
||||
|
||||
// Intended to be run as goroutine
|
||||
func LatexCompile(config *globals.Config) error {
|
||||
if !config.ExecutionLock.TryLock() {
|
||||
func LatexCompile(config globals.Config, execution *globals.LatexExecution) error {
|
||||
if !execution.ExecutionLock.TryLock() {
|
||||
LogLine("LaTeX execution already underway")
|
||||
return errors.New("Execution already in progress")
|
||||
}
|
||||
LogLine("LaTeX execution started")
|
||||
execution.ExecutionState = "Started"
|
||||
|
||||
var latexCommand string
|
||||
var latexCommand *exec.Cmd
|
||||
switch config.LatexEngine {
|
||||
case "lualatex":
|
||||
latexCommand = fmt.Sprintf(
|
||||
"lualatex -output-directory=%s -jobname=servtex %s",
|
||||
config.LatexOutputPath,
|
||||
config.LatexSourceFilePath,
|
||||
)
|
||||
|
||||
latexCommand = exec.Command(
|
||||
"lualatex",
|
||||
"-output-directory=" + config.LatexOutputPath,
|
||||
"-jobname=servtex",
|
||||
config.LatexSourceFilePath,
|
||||
)
|
||||
default:
|
||||
execution.ExecutionState = "Unknown Latex Engine"
|
||||
execution.Output = []byte{}
|
||||
execution.Timestamp = getLocalTimePretty()
|
||||
execution.ExecutionLock.Unlock()
|
||||
return errors.New("Unknown Latex Engine")
|
||||
}
|
||||
|
||||
fmt.Sprintf(latexCommand) // placeholder for compilation
|
||||
config.ExecutionLock.Unlock()
|
||||
|
||||
execution.ExecutionState = "Running"
|
||||
stdout, err := latexCommand.Output()
|
||||
if err != nil {
|
||||
execution.ExecutionState = "Failed"
|
||||
return err
|
||||
}
|
||||
|
||||
execution.Output = stdout
|
||||
execution.Timestamp = getLocalTimePretty()
|
||||
execution.ExecutionState = "Done"
|
||||
execution.ExecutionLock.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user