fix: improve GetVisitorCounter()

This commit is contained in:
axolotlmaid 2024-09-25 18:00:32 +01:00
parent bd31a363df
commit afcd4e9a8a

View file

@ -11,19 +11,26 @@ import (
) )
func GetVisitorCounter() model.VisitorCounter { func GetVisitorCounter() model.VisitorCounter {
var data model.VisitorCounter
path := filepath.Join(".", "data", "visitor.json") path := filepath.Join(".", "data", "visitor.json")
jsonFile, err := os.Open(path) jsonFile, err := os.Open(path)
if err != nil { if err != nil {
slog.Warn("File not found!", slog.Any("file", path)) slog.Warn("File not found or unable to open", slog.Any("error", err), slog.Any("file", path))
return model.VisitorCounter{} return data
}
defer jsonFile.Close()
bytes, err := io.ReadAll(jsonFile)
if err != nil {
slog.Error("Error reading file", slog.Any("error", err), slog.Any("file", path))
return data
} }
bytes, _ := io.ReadAll(jsonFile)
var data model.VisitorCounter
err = json.Unmarshal(bytes, &data) err = json.Unmarshal(bytes, &data)
if err != nil { if err != nil {
slog.Error("Error unmarshalling JSON", slog.Any("error", err), slog.Any("file", path)) slog.Error("Error unmarshalling JSON", slog.Any("error", err), slog.Any("file", path))
return data
} }
return data return data