speed-tester/routes/data.go

48 lines
1.3 KiB
Go

package routes
import (
"encoding/json"
"net/http"
"gitea.zokki.net/zokki/speed-tester/database"
"gitea.zokki.net/zokki/speed-tester/models"
)
func allRoute(writer http.ResponseWriter, req *http.Request) {
query := req.URL.Query()
data, err := database.GetAllData(req.Context(), query.Get("from"), query.Get("to"))
handleData(writer, data, err)
}
func speedRoute(writer http.ResponseWriter, req *http.Request) {
query := req.URL.Query()
data, err := database.GetSpeedData(req.Context(), query.Get("from"), query.Get("to"))
handleData(writer, data, err)
}
func latencyRoute(writer http.ResponseWriter, req *http.Request) {
query := req.URL.Query()
data, err := database.GetLatencyData(req.Context(), query.Get("from"), query.Get("to"))
handleData(writer, data, err)
}
func durationRoute(writer http.ResponseWriter, req *http.Request) {
query := req.URL.Query()
data, err := database.GetDurationData(req.Context(), query.Get("from"), query.Get("to"))
handleData(writer, data, err)
}
func handleData(writer http.ResponseWriter, data []models.InternetData, err error) {
if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
json, err := json.Marshal(data)
if err != nil {
http.Error(writer, err.Error(), http.StatusInternalServerError)
return
}
writer.Write(json)
}