2019-07-28 20:00:09 +01:00
|
|
|
// Package http resolves names to network addresses using a http request
|
2023-04-11 22:20:37 +03:00
|
|
|
package http // import "go.unistack.org/micro/v4/resolver/http"
|
2019-07-28 12:14:40 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2019-09-25 16:53:06 +01:00
|
|
|
"errors"
|
2021-02-17 23:38:32 +03:00
|
|
|
"io"
|
2019-07-28 12:14:40 +01:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
2023-04-11 22:20:37 +03:00
|
|
|
"go.unistack.org/micro/v4/resolver"
|
2019-07-28 12:14:40 +01:00
|
|
|
)
|
|
|
|
|
2021-09-30 21:13:13 +03:00
|
|
|
// nolint: golint,revive
|
2021-02-14 16:16:01 +03:00
|
|
|
// HTTPResolver is a HTTP network resolver
|
|
|
|
type HTTPResolver struct {
|
|
|
|
// Proto if not set, defaults to http
|
2019-07-28 12:14:40 +01:00
|
|
|
Proto string
|
|
|
|
// Path sets the path to lookup. Defaults to /network
|
|
|
|
Path string
|
2019-09-23 17:50:53 +01:00
|
|
|
// Host url to use for the query
|
|
|
|
Host string
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// Response contains resolver.Record
|
2019-09-23 17:50:53 +01:00
|
|
|
type Response struct {
|
|
|
|
Nodes []*resolver.Record `json:"nodes,omitempty"`
|
2019-07-28 12:14:40 +01:00
|
|
|
}
|
|
|
|
|
2019-07-28 20:00:09 +01:00
|
|
|
// Resolve assumes ID is a domain which can be converted to a http://name/network request
|
2021-02-14 16:16:01 +03:00
|
|
|
func (r *HTTPResolver) Resolve(name string) ([]*resolver.Record, error) {
|
2020-07-26 11:21:57 +01:00
|
|
|
proto := "http"
|
|
|
|
host := "localhost:8080"
|
2020-04-18 21:00:00 +01:00
|
|
|
path := "/network/nodes"
|
2019-07-28 12:14:40 +01:00
|
|
|
|
|
|
|
if len(r.Proto) > 0 {
|
|
|
|
proto = r.Proto
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(r.Path) > 0 {
|
|
|
|
path = r.Path
|
|
|
|
}
|
|
|
|
|
2019-09-23 17:50:53 +01:00
|
|
|
if len(r.Host) > 0 {
|
|
|
|
host = r.Host
|
|
|
|
}
|
|
|
|
|
2019-07-28 12:14:40 +01:00
|
|
|
uri := &url.URL{
|
|
|
|
Scheme: proto,
|
|
|
|
Path: path,
|
2019-09-23 17:50:53 +01:00
|
|
|
Host: host,
|
2019-07-28 12:14:40 +01:00
|
|
|
}
|
2019-09-23 17:50:53 +01:00
|
|
|
q := uri.Query()
|
|
|
|
q.Set("name", name)
|
|
|
|
uri.RawQuery = q.Encode()
|
2019-07-28 12:14:40 +01:00
|
|
|
|
2021-09-30 21:13:13 +03:00
|
|
|
// nolint: noctx
|
2019-07-28 12:14:40 +01:00
|
|
|
rsp, err := http.Get(uri.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rsp.Body.Close()
|
2019-09-25 16:53:06 +01:00
|
|
|
if rsp.StatusCode != 200 {
|
|
|
|
return nil, errors.New("non 200 response")
|
|
|
|
}
|
2021-02-17 23:38:32 +03:00
|
|
|
b, err := io.ReadAll(rsp.Body)
|
2019-07-28 12:14:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// encoding format is assumed to be json
|
2019-09-23 17:50:53 +01:00
|
|
|
var response *Response
|
2019-07-28 12:14:40 +01:00
|
|
|
|
2019-09-23 17:50:53 +01:00
|
|
|
if err := json.Unmarshal(b, &response); err != nil {
|
2019-07-28 12:14:40 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-09-23 17:50:53 +01:00
|
|
|
return response.Nodes, nil
|
2019-07-28 12:14:40 +01:00
|
|
|
}
|