api/internal/service/visitor.go
2024-09-25 17:55:40 +01:00

30 lines
581 B
Go

package service
import (
"encoding/json"
"io"
"log/slog"
"os"
"path/filepath"
"backend/internal/model"
)
func GetVisitorCounter() model.VisitorCounter {
path := filepath.Join(".", "data", "visitor.json")
jsonFile, err := os.Open(path)
if err != nil {
slog.Warn("File not found!", slog.Any("file", path))
return model.VisitorCounter{}
}
bytes, _ := io.ReadAll(jsonFile)
var data model.VisitorCounter
err = json.Unmarshal(bytes, &data)
if err != nil {
slog.Error("Error unmarshalling JSON", slog.Any("error", err), slog.Any("file", path))
}
return data
}