diff options
| author | Xe Iaso <me@xeiaso.net> | 2024-08-21 12:42:32 -0400 |
|---|---|---|
| committer | Xe Iaso <me@xeiaso.net> | 2024-08-21 12:42:40 -0400 |
| commit | 83902a1af00e76e9aaa40651cf33e4c399ff7380 (patch) | |
| tree | 5e1e1f3d00975fd8d9191c4a40312a1cd17cdb96 /cmd/hdrwtch/probes.go | |
| parent | 7a4ad5e24d0c3c3e702544315062485281a868a7 (diff) | |
| download | x-83902a1af00e76e9aaa40651cf33e4c399ff7380.tar.xz x-83902a1af00e76e9aaa40651cf33e4c399ff7380.zip | |
get started on htmx fun
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'cmd/hdrwtch/probes.go')
| -rw-r--r-- | cmd/hdrwtch/probes.go | 184 |
1 files changed, 184 insertions, 0 deletions
diff --git a/cmd/hdrwtch/probes.go b/cmd/hdrwtch/probes.go new file mode 100644 index 0000000..b9a8c3a --- /dev/null +++ b/cmd/hdrwtch/probes.go @@ -0,0 +1,184 @@ +package main + +import ( + "log/slog" + "net/http" + + "github.com/a-h/templ" + "gorm.io/gorm" + "within.website/x/htmx" +) + +type Probe struct { + gorm.Model + UserID string + Name string + URL string + LastResultID int + LastResult ProbeResult +} + +type ProbeResult struct { + gorm.Model + ProbeID int + LastModified string + StatusCode int + Region string +} + +func (s *Server) probeList(w http.ResponseWriter, r *http.Request) { + tu, ok := s.getTelegramUserData(r) + if !ok { + http.Error(w, "unauthorized", http.StatusUnauthorized) + return + } + + var probes []Probe + + if err := s.dao.db.Find(&probes).Joins("LastResult").Where("id = ?", tu.ID).Error; err != nil { + slog.Error("failed to get probes", "err", err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + templ.Handler( + base( + "Probes", + nil, + authedNavBar(tu), + probeListPage(probes), + ), + ).ServeHTTP(w, r) +} + +func (s *Server) probeCreate(w http.ResponseWriter, r *http.Request) { + tu, ok := s.getTelegramUserData(r) + if !ok { + http.Error(w, "unauthorized", http.StatusUnauthorized) + return + } + + newProbe := &Probe{ + UserID: tu.ID, + Name: r.FormValue("name"), + URL: r.FormValue("url"), + } + + if err := s.dao.db.Create(newProbe).Error; err != nil { + slog.Error("failed to create probe", "err", err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + var probes []Probe + + if err := s.dao.db.Find(&probes).Joins("LastResult").Where("id = ?", tu.ID).Error; err != nil { + slog.Error("failed to get probes", "err", err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + templ.Handler( + probeListPage(probes), + ).ServeHTTP(w, r) +} + +func (s *Server) probeEdit(w http.ResponseWriter, r *http.Request) { + tu, ok := getTelegramUser(r.Context()) + if !ok { + http.Error(w, "unauthorized", http.StatusUnauthorized) + return + } + + probe, ok := getProbe(r.Context()) + if !ok { + http.Error(w, "no probe data", http.StatusUnauthorized) + return + } + + if probe.UserID != tu.ID { + http.Error(w, "unauthorized", http.StatusUnauthorized) + return + } + + templ.Handler( + probeEdit(*probe), + ).ServeHTTP(w, r) +} + +func (s *Server) probeGet(w http.ResponseWriter, r *http.Request) { + tu, ok := getTelegramUser(r.Context()) + if !ok { + http.Error(w, "unauthorized", http.StatusUnauthorized) + return + } + + probe, err := s.dao.GetProbe(r.Context(), r.PathValue("id"), tu.ID) + slog.Info("probe", "probe", probe) + if err != nil { + slog.Error("failed to get probe", "path", r.URL.Path, "err", err) + http.Error(w, "no probe data", http.StatusUnauthorized) + return + } + + if probe.UserID != tu.ID { + http.Error(w, "unauthorized", http.StatusUnauthorized) + return + } + + if htmx.Is(r) { + templ.Handler( + probeRow(*probe), + ).ServeHTTP(w, r) + } else { + var results []ProbeResult + + if err := s.dao.db.Find(&results).Where("probe_id = ?", probe.ID).Order("created_at DESC").Limit(15).Error; err != nil { + slog.Error("failed to get probe results", "err", err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + templ.Handler( + base("Probe "+probe.Name, nil, authedNavBar(tu), probePage(*probe, results)), + ).ServeHTTP(w, r) + } +} + +func (s *Server) probeUpdate(w http.ResponseWriter, r *http.Request) { + tu, ok := getTelegramUser(r.Context()) + if !ok { + http.Error(w, "unauthorized", http.StatusUnauthorized) + return + } + + probe, ok := getProbe(r.Context()) + if !ok { + http.Error(w, "no probe data", http.StatusUnauthorized) + return + } + + if probe.UserID != tu.ID { + http.Error(w, "unauthorized", http.StatusUnauthorized) + return + } +} + +func (s *Server) probeDelete(w http.ResponseWriter, r *http.Request) { + tu, ok := getTelegramUser(r.Context()) + if !ok { + http.Error(w, "unauthorized", http.StatusUnauthorized) + return + } + + probe, ok := getProbe(r.Context()) + if !ok { + http.Error(w, "no probe data", http.StatusUnauthorized) + return + } + + if probe.UserID != tu.ID { + http.Error(w, "unauthorized", http.StatusUnauthorized) + return + } +} |
