chore: import upstream snapshot with attribution
govulncheck / govulncheck (push) Has been cancelled
Lint / golangci-lint (push) Has been cancelled
Run Tests / Unit Tests (push) Has been cancelled
Run Tests / Etcd Integration Tests (push) Has been cancelled
Harness (E2E) / Harnesses (mock LLM) (push) Has been cancelled
Harness (E2E) / Provider harnesses (live LLM conformance) (push) Has been cancelled
govulncheck / govulncheck (push) Has been cancelled
Lint / golangci-lint (push) Has been cancelled
Run Tests / Unit Tests (push) Has been cancelled
Run Tests / Etcd Integration Tests (push) Has been cancelled
Harness (E2E) / Harnesses (mock LLM) (push) Has been cancelled
Harness (E2E) / Provider harnesses (live LLM conformance) (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"go-micro.dev/v6/logger"
|
||||
"go-micro.dev/v6/metadata"
|
||||
"go-micro.dev/v6/registry"
|
||||
"go-micro.dev/v6/selector"
|
||||
)
|
||||
|
||||
// Write sets the status and body on a http ResponseWriter.
|
||||
func Write(w http.ResponseWriter, contentType string, status int, body string) {
|
||||
w.Header().Set("Content-Length", fmt.Sprintf("%v", len(body)))
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
w.WriteHeader(status)
|
||||
fmt.Fprintf(w, `%v`, body)
|
||||
}
|
||||
|
||||
// WriteBadRequestError sets a 400 status code.
|
||||
func WriteBadRequestError(w http.ResponseWriter, err error) {
|
||||
rawBody, err := json.Marshal(map[string]string{
|
||||
"error": err.Error(),
|
||||
})
|
||||
if err != nil {
|
||||
WriteInternalServerError(w, err)
|
||||
return
|
||||
}
|
||||
Write(w, "application/json", 400, string(rawBody))
|
||||
}
|
||||
|
||||
// WriteInternalServerError sets a 500 status code.
|
||||
func WriteInternalServerError(w http.ResponseWriter, err error) {
|
||||
rawBody, err := json.Marshal(map[string]string{
|
||||
"error": err.Error(),
|
||||
})
|
||||
if err != nil {
|
||||
logger.Log(logger.ErrorLevel, err)
|
||||
return
|
||||
}
|
||||
Write(w, "application/json", 500, string(rawBody))
|
||||
}
|
||||
|
||||
func NewRoundTripper(opts ...Option) http.RoundTripper {
|
||||
options := Options{
|
||||
Registry: registry.DefaultRegistry,
|
||||
}
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
return &roundTripper{
|
||||
rt: http.DefaultTransport,
|
||||
st: selector.Random,
|
||||
opts: options,
|
||||
}
|
||||
}
|
||||
|
||||
// RequestToContext puts the `Authorization` header bearer token into context
|
||||
// so calls to services will be authorized.
|
||||
func RequestToContext(r *http.Request) context.Context {
|
||||
ctx := context.Background()
|
||||
md := make(metadata.Metadata)
|
||||
for k, v := range r.Header {
|
||||
md[k] = strings.Join(v, ",")
|
||||
}
|
||||
return metadata.NewContext(ctx, md)
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"go-micro.dev/v6/registry"
|
||||
)
|
||||
|
||||
func TestRoundTripper(t *testing.T) {
|
||||
m := registry.NewMemoryRegistry()
|
||||
|
||||
rt := NewRoundTripper(
|
||||
WithRegistry(m),
|
||||
)
|
||||
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte(`hello world`))
|
||||
})
|
||||
|
||||
l, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer l.Close()
|
||||
|
||||
go http.Serve(l, nil)
|
||||
|
||||
m.Register(®istry.Service{
|
||||
Name: "example.com",
|
||||
Nodes: []*registry.Node{
|
||||
{
|
||||
Id: "1",
|
||||
Address: l.Addr().String(),
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, "http://example.com", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
w, err := rt.RoundTrip(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
b, err := io.ReadAll(w.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
w.Body.Close()
|
||||
|
||||
if string(b) != "hello world" {
|
||||
t.Fatal("response is", string(b))
|
||||
}
|
||||
|
||||
// test http request
|
||||
c := &http.Client{
|
||||
Transport: rt,
|
||||
}
|
||||
|
||||
rsp, err := c.Get("http://example.com")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
b, err = io.ReadAll(rsp.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rsp.Body.Close()
|
||||
|
||||
if string(b) != "hello world" {
|
||||
t.Fatal("response is", string(b))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"go-micro.dev/v6/registry"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
Registry registry.Registry
|
||||
}
|
||||
|
||||
type Option func(*Options)
|
||||
|
||||
func WithRegistry(r registry.Registry) Option {
|
||||
return func(o *Options) {
|
||||
o.Registry = r
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"go-micro.dev/v6/selector"
|
||||
)
|
||||
|
||||
type roundTripper struct {
|
||||
rt http.RoundTripper
|
||||
st selector.Strategy
|
||||
opts Options
|
||||
}
|
||||
|
||||
func (r *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
s, err := r.opts.Registry.GetService(req.URL.Host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
next := r.st(s)
|
||||
|
||||
// rudimentary retry 3 times
|
||||
for i := 0; i < 3; i++ {
|
||||
n, err := next()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
req.URL.Host = n.Address
|
||||
w, err := r.rt.RoundTrip(req)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
return w, nil
|
||||
}
|
||||
|
||||
return nil, errors.New("failed request")
|
||||
}
|
||||
Reference in New Issue
Block a user