feat: add incrementing visitor counter
This commit is contained in:
parent
afcd4e9a8a
commit
a92db04713
3 changed files with 41 additions and 5 deletions
|
|
@ -14,5 +14,8 @@ func HandleGetVisitorCounter(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandlePatchVisitorCounter(w http.ResponseWriter, r *http.Request) {
|
func HandlePatchVisitorCounter(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte("hello world"))
|
data := service.IncrementVisitorCounter()
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(data)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
5
internal/model/model.go
Normal file
5
internal/model/model.go
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
package model
|
||||||
|
|
||||||
|
type Success struct {
|
||||||
|
Success bool `json:"success"`
|
||||||
|
}
|
||||||
|
|
@ -10,28 +10,56 @@ import (
|
||||||
"backend/internal/model"
|
"backend/internal/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const path = "./data/visitor.json"
|
||||||
|
|
||||||
func GetVisitorCounter() model.VisitorCounter {
|
func GetVisitorCounter() model.VisitorCounter {
|
||||||
var data model.VisitorCounter
|
var data model.VisitorCounter
|
||||||
|
|
||||||
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 or unable to open", slog.Any("error", err), slog.Any("file", path))
|
slog.Warn("File not found or unable to open", slog.Any("error", err), slog.Any("path", path))
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
defer jsonFile.Close()
|
defer jsonFile.Close()
|
||||||
|
|
||||||
bytes, err := io.ReadAll(jsonFile)
|
bytes, err := io.ReadAll(jsonFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Error reading file", slog.Any("error", err), slog.Any("file", path))
|
slog.Error("Error reading file", slog.Any("error", err), slog.Any("path", path))
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
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("path", path))
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func IncrementVisitorCounter() model.Success {
|
||||||
|
data := GetVisitorCounter()
|
||||||
|
data.Counter++
|
||||||
|
|
||||||
|
err := os.MkdirAll(filepath.Dir(path), 0755)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("Unable to create directory", slog.Any("error", err), slog.Any("path", filepath.Dir(path)))
|
||||||
|
return model.Success{}
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonString, err := json.Marshal(data)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("Error marshalling JSON", slog.Any("error", err), slog.Any("path", path))
|
||||||
|
return model.Success{}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.WriteFile(path, jsonString, 0644)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("Error writing to file", slog.Any("error", err), slog.Any("path", path))
|
||||||
|
return model.Success{}
|
||||||
|
}
|
||||||
|
|
||||||
|
return model.Success{
|
||||||
|
Success: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue