1
0
mirror of https://github.com/ManyakRus/telegram_loki.git synced 2025-03-05 03:07:12 +02:00

95 lines
2.2 KiB
Go
Raw Normal View History

2023-10-11 18:04:49 +03:00
package loki
import (
"encoding/json"
"fmt"
"github.com/ManyakRus/starter/log"
"github.com/ManyakRus/telegram_loki/internal/config"
"github.com/ManyakRus/telegram_loki/internal/types"
"io"
"net/http"
"strconv"
"time"
)
2023-10-12 14:34:34 +03:00
var Client = &http.Client{}
func DownloadJSON(ServiceName string, DateFrom, DateTo time.Time) (types.Message, error) {
Otvet := types.Message{}
2023-10-11 18:04:49 +03:00
var err error
2023-10-12 14:34:34 +03:00
query := "%7Bapp%3D%22" + ServiceName + "%22%7D%7C~%22(error%7Cpanic)%22"
sTime1 := strconv.FormatInt(DateFrom.UnixNano(), 10)
sTime2 := strconv.FormatInt(DateTo.UnixNano(), 10)
2023-10-11 18:04:49 +03:00
URL := config.Settings.LOKI_URL + "/api/datasources/proxy/1/loki/api/v1/query_range?direction=BACKWARD&limit=10&query=" + query
URL += "&start=" + sTime1 + "&end=" + sTime2
2023-10-12 14:34:34 +03:00
//URL = "http://logmon.dev.atomsbt.ru/api/datasources" //удалить
2023-10-11 18:04:49 +03:00
//запрос http
2023-10-12 14:34:34 +03:00
req, err := http.NewRequest(http.MethodGet, URL, http.NoBody)
2023-10-11 18:04:49 +03:00
if err != nil {
return Otvet, fmt.Errorf("request(), ошибка создания GET запроса, err=%w", err)
}
2023-10-12 14:34:34 +03:00
req.SetBasicAuth(config.Settings.LOKI_LOGIN, config.Settings.LOKI_PASSWORD)
2023-10-11 18:04:49 +03:00
//req.Header.Add("Content-Type:", "application/json")
2023-10-12 14:34:34 +03:00
//client := &http.Client{}
response, err := Client.Do(req)
2023-10-11 18:04:49 +03:00
if err != nil {
return Otvet, fmt.Errorf("request(), ошибка выполнения GET запроса, err=%w", err)
}
switch response.StatusCode {
case 200:
default:
{
log.Error("http request error: ", response.Status)
return Otvet, err
}
}
//
TextJson, err := io.ReadAll(response.Body)
err = json.Unmarshal(TextJson, &Otvet)
if err != nil {
return Otvet, err
}
return Otvet, err
}
2023-10-12 14:34:34 +03:00
// Authentication - ненужная
// функция для аутентификации
2023-10-11 18:04:49 +03:00
func Authentication() error {
var err error
2023-10-12 14:34:34 +03:00
URL := config.Settings.LOKI_URL //+ "/login"
2023-10-11 18:04:49 +03:00
2023-10-12 14:34:34 +03:00
//client := http.Client{Timeout: 60 * time.Second}
2023-10-11 18:04:49 +03:00
req, err := http.NewRequest(http.MethodGet, URL, http.NoBody)
if err != nil {
log.Fatal(err)
}
req.SetBasicAuth(config.Settings.LOKI_LOGIN, config.Settings.LOKI_PASSWORD)
2023-10-12 14:34:34 +03:00
res, err := Client.Do(req)
2023-10-11 18:04:49 +03:00
if err != nil {
2023-10-12 14:34:34 +03:00
log.Panic(err)
2023-10-11 18:04:49 +03:00
}
defer res.Body.Close()
2023-10-12 14:34:34 +03:00
resBody, err := io.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
print(resBody)
2023-10-11 18:04:49 +03:00
//
return err
}