mirror of
https://github.com/foomo/sesamy-go.git
synced 2025-10-16 12:35:43 +00:00
wip: use new request
This commit is contained in:
parent
84f5a2f76b
commit
9af3ced441
@ -1,23 +1,29 @@
|
|||||||
package collect
|
package collect
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
"strings"
|
||||||
"net/url"
|
|
||||||
|
|
||||||
"github.com/foomo/sesamy-go/pkg/encoding/gtag"
|
"github.com/foomo/sesamy-go/pkg/encoding/gtag"
|
||||||
"github.com/foomo/sesamy-go/pkg/encoding/mpv2"
|
"github.com/foomo/sesamy-go/pkg/encoding/mpv2"
|
||||||
gtaghttp "github.com/foomo/sesamy-go/pkg/http/gtag"
|
gtaghttp "github.com/foomo/sesamy-go/pkg/http/gtag"
|
||||||
mpv2http "github.com/foomo/sesamy-go/pkg/http/mpv2"
|
mpv2http "github.com/foomo/sesamy-go/pkg/http/mpv2"
|
||||||
|
"github.com/pkg/errors"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
Collect struct {
|
Collect struct {
|
||||||
l *zap.Logger
|
l *zap.Logger
|
||||||
taggingProxy *httputil.ReverseProxy
|
taggingURL string
|
||||||
gtagHTTPMiddlewares []gtaghttp.Middleware
|
taggingClient *http.Client
|
||||||
mpv2HTTPMiddlewares []mpv2http.Middleware
|
taggingMaxResponseCode int
|
||||||
|
gtagHTTPMiddlewares []gtaghttp.Middleware
|
||||||
|
mpv2HTTPMiddlewares []mpv2http.Middleware
|
||||||
}
|
}
|
||||||
Option func(*Collect) error
|
Option func(*Collect) error
|
||||||
)
|
)
|
||||||
@ -26,16 +32,16 @@ type (
|
|||||||
// ~ Options
|
// ~ Options
|
||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
func WithTagging(endpoint string) Option {
|
func WithTagging(v string) Option {
|
||||||
return func(c *Collect) error {
|
return func(c *Collect) error {
|
||||||
target, err := url.Parse(endpoint)
|
c.taggingURL = v
|
||||||
if err != nil {
|
return nil
|
||||||
return err
|
}
|
||||||
}
|
}
|
||||||
c.l.Info("--->" + endpoint)
|
|
||||||
proxy := httputil.NewSingleHostReverseProxy(target)
|
func WithTaggingClient(v *http.Client) Option {
|
||||||
proxy.ErrorLog = zap.NewStdLog(c.l)
|
return func(c *Collect) error {
|
||||||
c.taggingProxy = proxy
|
c.taggingClient = v
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -60,7 +66,9 @@ func WithMPv2HTTPMiddlewares(v ...mpv2http.Middleware) Option {
|
|||||||
|
|
||||||
func New(l *zap.Logger, opts ...Option) (*Collect, error) {
|
func New(l *zap.Logger, opts ...Option) (*Collect, error) {
|
||||||
inst := &Collect{
|
inst := &Collect{
|
||||||
l: l,
|
l: l,
|
||||||
|
taggingClient: http.DefaultClient,
|
||||||
|
taggingMaxResponseCode: http.StatusBadRequest,
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
@ -117,15 +125,67 @@ func (c *Collect) MPv2HTTPHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
func (c *Collect) gtagHandler(l *zap.Logger, w http.ResponseWriter, r *http.Request, payload *gtag.Payload) error {
|
func (c *Collect) gtagHandler(l *zap.Logger, w http.ResponseWriter, r *http.Request, payload *gtag.Payload) error {
|
||||||
if c.taggingProxy != nil {
|
values, body, err := gtag.Encode(payload)
|
||||||
c.taggingProxy.ServeHTTP(w, r)
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequestWithContext(r.Context(), http.MethodPost, fmt.Sprintf("%s%s?%s", c.taggingURL, "/g/collect", gtag.EncodeValues(values)), body)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "failed to create request")
|
||||||
|
}
|
||||||
|
|
||||||
|
// copy headers
|
||||||
|
req.Header = r.Header.Clone()
|
||||||
|
|
||||||
|
resp, err := c.taggingClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
// copy headers
|
||||||
|
for name, values := range resp.Header {
|
||||||
|
r.Header.Add(name, strings.Join(values, ","))
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := io.Copy(w, resp.Body); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Collect) mpv2Handler(l *zap.Logger, w http.ResponseWriter, r *http.Request, payload *mpv2.Payload[any]) error {
|
func (c *Collect) mpv2Handler(l *zap.Logger, w http.ResponseWriter, r *http.Request, payload *mpv2.Payload[any]) error {
|
||||||
if c.taggingProxy != nil {
|
body, err := json.Marshal(payload)
|
||||||
c.taggingProxy.ServeHTTP(w, r)
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequestWithContext(r.Context(), http.MethodPost, fmt.Sprintf("%s%s", c.taggingURL, "/mp/collect"), bytes.NewReader(body))
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "failed to create request")
|
||||||
|
}
|
||||||
|
|
||||||
|
// copy headers
|
||||||
|
req.Header = r.Header.Clone()
|
||||||
|
// copy raw query
|
||||||
|
req.URL.RawQuery = r.URL.RawQuery
|
||||||
|
|
||||||
|
resp, err := c.taggingClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
// copy headers
|
||||||
|
for name, values := range resp.Header {
|
||||||
|
r.Header.Add(name, strings.Join(values, ","))
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := io.Copy(w, resp.Body); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user