mirror of
https://github.com/trafficlunar/api.git
synced 2026-06-27 22:24:08 +00:00
feat: add getting visitor counter
This commit is contained in:
parent
0f2d805df2
commit
bd31a363df
4 changed files with 58 additions and 0 deletions
30
internal/service/visitor.go
Normal file
30
internal/service/visitor.go
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue