monitoring/main.go
Egor Matveev 6e257edcc0
All checks were successful
Deploy Dev / Push (pull_request) Successful in 37s
Deploy Dev / Build (pull_request) Successful in 1m13s
Deploy Dev / Deploy dev (pull_request) Successful in 13s
intiail
2025-06-13 00:25:14 +03:00

36 lines
875 B
Go

package main
import (
"encoding/json"
"log"
endpoint "monitoring/app/routers/metrics"
client "monitoring/app/storage/clickhouse"
"net/http"
"time"
)
func handlerWrapper(f func(*http.Request) (interface{}, int)) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
response, status := f(r)
if response != nil {
data, err := json.Marshal(response)
if err != nil {
log.Printf("Error marshalling response")
status = http.StatusInternalServerError
}
w.WriteHeader(status)
w.Write(data)
} else {
w.WriteHeader(status)
}
log.Printf("%s %d %s", r.URL, status, time.Since(start))
}
}
func main() {
client.Connect()
http.HandleFunc("/api/v1/metrics/endpoint", handlerWrapper(endpoint.AddEndpointMetric))
log.Printf("Server started")
http.ListenAndServe(":1237", nil)
}