diff options
Diffstat (limited to 'backend/internal/api/handler.go')
| -rw-r--r-- | backend/internal/api/handler.go | 40 |
1 files changed, 37 insertions, 3 deletions
diff --git a/backend/internal/api/handler.go b/backend/internal/api/handler.go index 5f934da..8da4290 100644 --- a/backend/internal/api/handler.go +++ b/backend/internal/api/handler.go @@ -10,6 +10,7 @@ import ( "yaum/internal/monitor" "yaum/internal/model" + "yaum/internal/servermetrics" "yaum/internal/store" ) @@ -21,10 +22,11 @@ type ServiceResponse struct { type Handler struct { store store.Store timeout int + metrics *servermetrics.Collector } -func NewHandler(s store.Store, timeoutSec int) *Handler { - return &Handler{store: s, timeout: timeoutSec} +func NewHandler(s store.Store, timeoutSec int, metrics *servermetrics.Collector) *Handler { + return &Handler{store: s, timeout: timeoutSec, metrics: metrics} } func (h *Handler) ListServices(w http.ResponseWriter, r *http.Request) { @@ -91,9 +93,14 @@ func (h *Handler) TestService(w http.ResponseWriter, r *http.Request) { ctx, cancel := context.WithTimeout(r.Context(), time.Duration(h.timeout)*time.Second) defer cancel() - hb := monitor.CheckHTTP(ctx, svc.URL, svc.KeywordToFind) + hb, ssl := monitor.CheckHTTP(ctx, svc.URL, svc.KeywordToFind) hb.ServiceID = svc.ID + if ssl != nil { + ssl.ServiceID = svc.ID + _ = h.store.SaveSSLInfo(*ssl) + } + if saverr := h.store.AddHeartbeat(hb); saverr != nil { writeJSON(w, http.StatusInternalServerError, map[string]string{"error": saverr.Error()}) return @@ -323,6 +330,33 @@ func (h *Handler) GetStats(w http.ResponseWriter, r *http.Request) { writeJSON(w, http.StatusOK, stats) } +func (h *Handler) ServerStats(w http.ResponseWriter, r *http.Request) { + if h.metrics == nil { + writeJSON(w, http.StatusNotFound, map[string]string{"error": "server metrics nao disponivel"}) + return + } + stats := h.metrics.Get() + writeJSON(w, http.StatusOK, stats) +} + +func (h *Handler) GetSSLInfo(w http.ResponseWriter, r *http.Request) { + id, err := strconv.Atoi(r.PathValue("id")) + if err != nil { + writeJSON(w, http.StatusBadRequest, map[string]string{"error": "id invalido"}) + return + } + info, err := h.store.GetSSLInfo(id) + if err != nil { + writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()}) + return + } + if info == nil { + writeJSON(w, http.StatusNotFound, map[string]string{"error": "ssl info nao disponivel"}) + return + } + writeJSON(w, http.StatusOK, info) +} + func writeJSON(w http.ResponseWriter, status int, data any) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(status) |