In this case, the users will be mostly internal - so supporting lots of configs would not make sense in this case as it will be shipped in a Docker image for internal use.
Here is the example ServeHTTP
with the Proxy
:
type Proxy struct {
API string `json:"api,omitempty"`
Token string `json:"token,omitempty"`
log *zap.Logger
}
func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
client, err := consulapi.NewDefaultClient()
if err != nil {
p.log.Error("unable to create a new client", zap.Error(err))
return err
}
services, err := client.FindService(r.Host)
if err != nil {
p.log.Error("unable to find the service from consul", zap.Error(err), zap.String("service", r.Host))
return err
}
var placeholder string
for i, s := range services {
// only working with the first service for now
if i == 0 {
a := fmt.Sprintf("http://%s:%d", s.TaggedAddresses.LanIpv4.Address, s.TaggedAddresses.LanIpv4.Port)
placeholder = a
}
}
caddyhttp.SetVar(r.Context(), "http.consul_proxy.actual_upstream", placeholder)
if err := next.ServeHTTP(w, r); err != nil {
p.log.Error("error passing to next handler", zap.Error(err))
return fmt.Errorf("error passing to next handler. %w", err)
}
return nil
}
If I switch the var to http.vars.proxy_upstream
I should just be able to return ServeHTTP
correct?