36 lines
875 B
Go
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)
|
|
} |