aboutsummaryrefslogtreecommitdiff
path: root/backend/internal/api
diff options
context:
space:
mode:
Diffstat (limited to 'backend/internal/api')
-rw-r--r--backend/internal/api/handler.go40
-rw-r--r--backend/internal/api/router.go3
2 files changed, 40 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)
diff --git a/backend/internal/api/router.go b/backend/internal/api/router.go
index 8188b25..d28da71 100644
--- a/backend/internal/api/router.go
+++ b/backend/internal/api/router.go
@@ -19,6 +19,7 @@ func NewRouter(h *Handler, stream *StreamHandler, auth *AuthHandler) http.Handle
mux.HandleFunc("DELETE /api/services/{id}", h.DeleteService)
mux.HandleFunc("GET /api/services/{id}/history", h.GetHistory)
mux.HandleFunc("GET /api/services/{id}/stats", h.GetStats)
+ mux.HandleFunc("GET /api/services/{id}/ssl", h.GetSSLInfo)
mux.HandleFunc("GET /api/services/{id}/badge.svg", h.BadgeSVG)
mux.HandleFunc("GET /api/active-maintenance", h.GetActiveMaintenance)
@@ -28,6 +29,8 @@ func NewRouter(h *Handler, stream *StreamHandler, auth *AuthHandler) http.Handle
mux.HandleFunc("PUT /api/maintenances/{id}", h.UpdateMaintenance)
mux.HandleFunc("DELETE /api/maintenances/{id}", h.DeleteMaintenance)
+ mux.HandleFunc("GET /api/admin/server-stats", h.ServerStats)
+
mux.Handle("GET /api/stream", stream)
return corsMiddleware(auth.Middleware(mux))