Small refactoring

This commit is contained in:
Bendodroid 2024-01-06 19:43:51 +01:00
parent baf73f33e7
commit 883d25898e
Signed by: bendodroid
GPG key ID: 3EEE19A0F73D5FFC
3 changed files with 8 additions and 8 deletions

3
.gitignore vendored
View file

@ -6,3 +6,6 @@
# go build output # go build output
spaceapid spaceapid
# Saved state
spaceapid-state.json

View file

@ -25,12 +25,12 @@ func main() {
// Register signal handler // Register signal handler
sc := make(chan os.Signal, 1) sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM)
go func(ch chan os.Signal, resp *types.SpaceAPIResponseV14) { go func(resp *types.SpaceAPIResponseV14) {
<-ch <-sc
log.Println("Saving state and shutting down...") log.Println("Saving state and shutting down...")
util.SaveCurrentState(*resp) util.SaveCurrentState(*resp)
os.Exit(0) os.Exit(0)
}(sc, &spaceApiResponse) }(&spaceApiResponse)
// Register HTTP handlers // Register HTTP handlers
http.HandleFunc("/", handlers.Root(&spaceApiResponse)) http.HandleFunc("/", handlers.Root(&spaceApiResponse))

View file

@ -16,7 +16,6 @@ const savedStateJSONPath = "/var/lib/spaceapid/spaceapid-state.json"
// ParseTemplate parses the given file and // ParseTemplate parses the given file and
func ParseTemplate(file string) (resp types.SpaceAPIResponseV14) { func ParseTemplate(file string) (resp types.SpaceAPIResponseV14) {
// Read template file // Read template file
template, err := os.ReadFile(file) template, err := os.ReadFile(file)
if err != nil { if err != nil {
@ -84,14 +83,12 @@ func SaveCurrentState(response types.SpaceAPIResponseV14) {
log.Fatalln("Failed creating", savedStateJSONPath, ", aborting... error:", err) 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) file, err := os.OpenFile(savedStateJSONPath, os.O_RDWR|os.O_CREATE, 0644)
if err != nil { if err != nil {
log.Fatalln("Failed opening", savedStateJSONPath, "while trying to save current state... error:", err) log.Fatalln("Failed opening", savedStateJSONPath, "while trying to save current state... error:", err)
} }
defer func(file *os.File) { defer file.Close()
_ = file.Close()
}(file)
// Create persistent state // Create persistent state
persistentStateV14 := types.PersistentStateV14{ persistentStateV14 := types.PersistentStateV14{