From b600ec5267688641bd8acf65c696a7c63f135554 Mon Sep 17 00:00:00 2001 From: Maximilian Wagner Date: Sat, 27 Dec 2025 19:22:51 +0100 Subject: [PATCH] tbh i forgot --- backend/backend.go | 73 ++++++++++++++++++------------------- backend/default_config.json | 2 +- config.json | 2 +- frontend/webserver.go | 1 - globals/memory.go | 1 - main.go | 12 +++--- 6 files changed, 43 insertions(+), 48 deletions(-) diff --git a/backend/backend.go b/backend/backend.go index bcff64b..1d0dd5a 100644 --- a/backend/backend.go +++ b/backend/backend.go @@ -81,6 +81,14 @@ func VerifyLogRequest(request *http.Request) (err error) { return nil } + +// Writes a configuration file populated with defaults +func createConfigWithDefaults(path string) error { + config, _ := defaultConfig.ReadFile("default_config.json") + err := os.WriteFile(path, config, 0644) + return err +} + // Helper for configReader() that does the actual reading // // Also validates the populated fields @@ -88,7 +96,7 @@ func configReaderParse(filePath string, configOptionStorage *globals.Config) err jsonData, err := os.ReadFile(filePath) if err == nil { if err = json.Unmarshal(jsonData, &configOptionStorage); err != nil { - LogLine("Configuration file is invalid JSON", 5) + LogLine("Configuration file is invalid JSON", 4) return errors.New("Config file could not be read") } } else { @@ -112,17 +120,10 @@ func configReaderParse(filePath string, configOptionStorage *globals.Config) err return nil } -// tbd what exactly happens here -func configValidator(config globals.Config) error { - return nil -} - -func configPopulator(config globals.Config) error { - return nil -} - -// Creates the configuration file populated with defaults -func createConfigWithDefaults() { +// populates the appconfig with default values +func configPopulator(configOptionStorage *globals.Config) { + jsonData, _ := defaultConfig.ReadFile("default_config.json") + json.Unmarshal(jsonData, &configOptionStorage) } // Reads config file and stores the options in configOptionStorage @@ -132,29 +133,39 @@ func createConfigWithDefaults() { // %LOCALAPPDATA%\servtex\configFileName // ~/.config/servtex/configFileName func ConfigReader(configFileName string, configOptionStorage *globals.Config) error { + var defaultPath string + + // populate default config + configPopulator(configOptionStorage) + configRead := false + + // read config file from disk exePath, err := os.Executable() if err == nil { - path := filepath.Join(filepath.Dir(exePath), configFileName) - err = configReaderParse(path, configOptionStorage) - if err == nil { return nil } + defaultPath = filepath.Join(filepath.Dir(exePath), configFileName) + err = configReaderParse(defaultPath, configOptionStorage) + if err == nil { configRead = true } } localappdata := os.Getenv("LOCALAPPDATA") - if localappdata != "" { + if !configRead && localappdata != "" { path := filepath.Join(localappdata, "servtex", configFileName) err = configReaderParse(path, configOptionStorage) - if err == nil { return nil } } - path := filepath.Join("~", ".config", "servtex", configFileName) - err = configReaderParse(path, configOptionStorage) - if err != nil { return err } + if !configRead { + path := filepath.Join("~", ".config", "servtex", configFileName) + err = configReaderParse(path, configOptionStorage) - err = configPopulator(*configOptionStorage) - if err != nil { return err } - - err = configValidator(*configOptionStorage) - if err != nil { return err } + // create default config file for user to edit + if err != nil && defaultPath != "" { + LogLine(fmt.Sprintf("Configuration file does not exist. Creating with defaults at %s", defaultPath), 4) + err = createConfigWithDefaults(defaultPath) + if err != nil { + LogLine("Configuration file could not be created", 4) + } + } + } return nil } @@ -286,18 +297,6 @@ func LatexCompile() error { return nil } -// Returns intersection of two slices -func sliceIntersection[T comparable](left []T, right []T) (intersection []T) { - for _, leftItem := range left { - for _, rightItem := range right { - if leftItem == rightItem { - intersection = append(intersection, leftItem) - } - } - } - return intersection -} - // Checks whether the proxy is trusted. Returns trusted status and the proxy. // // If X-Forwarded-For chain is passed, only the last in chain will be considered diff --git a/backend/default_config.json b/backend/default_config.json index 63dbc2c..3a62b66 100644 --- a/backend/default_config.json +++ b/backend/default_config.json @@ -1,5 +1,5 @@ { - "timezone": "Europe/Berlin" + "timezone": "Europe/Berlin", "logFilePath": "./servtex.log", "logLevel": "warning", diff --git a/config.json b/config.json index edb0e71..34751dd 100644 --- a/config.json +++ b/config.json @@ -1,5 +1,5 @@ { - "timezone": "Europe/Berlin" + "timezone": "Europe/Berlin", "logFilePath": "./servtex.log", "logLevel": "debug", diff --git a/frontend/webserver.go b/frontend/webserver.go index 23109b8..5eca85f 100644 --- a/frontend/webserver.go +++ b/frontend/webserver.go @@ -88,7 +88,6 @@ func SSEventHandler(writer http.ResponseWriter, request *http.Request) { for range time.Tick(time.Second) { select { case <-request.Context().Done(): - backend.LogLine("SSE Context Done", 1) return default: if lastExecution == globals.LatexExec.TimestampRFC { diff --git a/globals/memory.go b/globals/memory.go index 8d623b5..0036fd2 100644 --- a/globals/memory.go +++ b/globals/memory.go @@ -30,7 +30,6 @@ type Config struct { } var AppConfig Config - type LatexExecution struct { ExecutionLock sync.Mutex ExecutionState string diff --git a/main.go b/main.go index 490ccbe..83149d7 100644 --- a/main.go +++ b/main.go @@ -72,14 +72,12 @@ func main() { fmt.Print("\r") // shutdown - context, cancel := context.WithTimeout(context.Background(), 2*time.Second) + // known issue: sse blocks shutdown if a client is still connected + context, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() - if err = server.Shutdown(context); err != nil { - backend.LogLine("Graceful Shutdown failed", 4) - } - if err = serverSecure.Shutdown(context); err != nil { - backend.LogLine("Graceful Shutdown failed", 4) - } + server.Shutdown(context) + serverSecure.Shutdown(context) + backend.LogLine("Stopped", 2) }