(w http.ResponseWriter, r *http.Request)
| 71 | } |
| 72 | |
| 73 | func (h *Index) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 74 | ip := getIP(r, 0) |
| 75 | |
| 76 | ctx := r.Context() |
| 77 | |
| 78 | if err := h.repo.InsertVisit(ctx, ip); err != nil { |
| 79 | h.logger.Error("failed to insert visit", slog.Any("error", err)) |
| 80 | w.WriteHeader(http.StatusInternalServerError) |
| 81 | return |
| 82 | } |
| 83 | |
| 84 | total, err := h.repo.CountAllVisits(ctx) |
| 85 | if err != nil { |
| 86 | h.logger.Error("failed to get total visits", slog.Any("error", err)) |
| 87 | w.WriteHeader(http.StatusInternalServerError) |
| 88 | return |
| 89 | } |
| 90 | |
| 91 | hour, err := h.repo.CountVisitors(ctx, time.Now().Add(-time.Hour)) |
| 92 | if err != nil { |
| 93 | h.logger.Error("failed to get hour visits", slog.Any("error", err)) |
| 94 | w.WriteHeader(http.StatusInternalServerError) |
| 95 | return |
| 96 | } |
| 97 | |
| 98 | day, err := h.repo.CountVisitors(ctx, time.Now().Add(-time.Hour*24)) |
| 99 | if err != nil { |
| 100 | h.logger.Error("failed to get day visits", slog.Any("error", err)) |
| 101 | w.WriteHeader(http.StatusInternalServerError) |
| 102 | return |
| 103 | } |
| 104 | |
| 105 | week, err := h.repo.CountVisitors(ctx, time.Now().Add(-time.Hour*24*7)) |
| 106 | if err != nil { |
| 107 | h.logger.Error("failed to get week visits", slog.Any("error", err)) |
| 108 | w.WriteHeader(http.StatusInternalServerError) |
| 109 | return |
| 110 | } |
| 111 | |
| 112 | quote := h.quotes.GetQuote() |
| 113 | h.tmpl.ExecuteTemplate(w, "index.html", pageData{ |
| 114 | Quote: quote, |
| 115 | TotalHits: total, |
| 116 | Visitors: Visitors{ |
| 117 | LastHour: hour, |
| 118 | LastDay: day, |
| 119 | LastWeek: week, |
| 120 | }, |
| 121 | }) |
| 122 | } |
no test coverage detected