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
spaceapid
# Saved state
spaceapid-state.json

View file

@ -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))

View file

@ -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{