From 883d25898e36e46ebcebe252d73993b54c211093 Mon Sep 17 00:00:00 2001 From: Bennett Wetters Date: Sat, 6 Jan 2024 19:43:51 +0100 Subject: [PATCH] Small refactoring --- .gitignore | 3 +++ main.go | 6 +++--- util/util.go | 7 ++----- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 2e1f8da..8a7f400 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,6 @@ # go build output spaceapid + +# Saved state +spaceapid-state.json diff --git a/main.go b/main.go index 7678238..56ac017 100644 --- a/main.go +++ b/main.go @@ -25,12 +25,12 @@ func main() { // Register signal handler sc := make(chan os.Signal, 1) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM) - go func(ch chan os.Signal, resp *types.SpaceAPIResponseV14) { - <-ch + go func(resp *types.SpaceAPIResponseV14) { + <-sc log.Println("Saving state and shutting down...") util.SaveCurrentState(*resp) os.Exit(0) - }(sc, &spaceApiResponse) + }(&spaceApiResponse) // Register HTTP handlers http.HandleFunc("/", handlers.Root(&spaceApiResponse)) diff --git a/util/util.go b/util/util.go index e43123d..23e1773 100644 --- a/util/util.go +++ b/util/util.go @@ -16,7 +16,6 @@ const savedStateJSONPath = "/var/lib/spaceapid/spaceapid-state.json" // ParseTemplate parses the given file and func ParseTemplate(file string) (resp types.SpaceAPIResponseV14) { - // Read template file template, err := os.ReadFile(file) if err != nil { @@ -84,14 +83,12 @@ func SaveCurrentState(response types.SpaceAPIResponseV14) { log.Fatalln("Failed creating", savedStateJSONPath, ", aborting... error:", err) } - // Open persistent state file for reading + // Open persistent state file file, err := os.OpenFile(savedStateJSONPath, os.O_RDWR|os.O_CREATE, 0644) if err != nil { log.Fatalln("Failed opening", savedStateJSONPath, "while trying to save current state... error:", err) } - defer func(file *os.File) { - _ = file.Close() - }(file) + defer file.Close() // Create persistent state persistentStateV14 := types.PersistentStateV14{