68 lines
1.3 KiB
Go
68 lines
1.3 KiB
Go
/*
|
|
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
|
*
|
|
* This work is published and licensed under a Creative Commons
|
|
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
|
|
*
|
|
* Distribution of this work is permitted, but commercial use and
|
|
* modifications are strictly prohibited.
|
|
*/
|
|
package client
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
type Client struct {
|
|
username string
|
|
password string
|
|
}
|
|
|
|
func NewClient() *Client {
|
|
return &Client{}
|
|
}
|
|
|
|
func NewClientWithAuth(username, password string) *Client {
|
|
return &Client{
|
|
username: username,
|
|
password: password,
|
|
}
|
|
}
|
|
|
|
func (cli *Client) ServiceHello(ctx context.Context, ref string, timeout time.Duration) (bool, error) {
|
|
var res bool
|
|
var err error
|
|
|
|
ctx, _ = context.WithTimeout(ctx, timeout)
|
|
|
|
ref, err = convertServiceRefer(ref)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ref, nil)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
|
|
transport := &http.Transport{
|
|
TLSClientConfig: &tls.Config{
|
|
InsecureSkipVerify: true,
|
|
},
|
|
}
|
|
client := &http.Client{
|
|
Transport: transport,
|
|
}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode == http.StatusOK {
|
|
res = true
|
|
}
|
|
return res, err
|
|
}
|