package main

import (
	"encoding/json"
	"log"
	endpoint "monitoring/app/routers/metrics"
	task "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() {
	err := client.Connect()
	if err != nil {
		panic(err)
	}
	defer client.Close()

	err = client.Migrate()
	if err != nil {
		panic(err)
	}

	http.HandleFunc("/api/v1/metrics/endpoint", handlerWrapper(endpoint.AddEndpointMetric))
	http.HandleFunc("/api/v1/metrics/task", handlerWrapper(task.AddTaskMetric))
	log.Printf("Server started")
	http.ListenAndServe(":1237", nil)
}
