zrok/proxy/proxy.go
2022-07-21 16:01:39 -04:00

28 lines
719 B
Go

package proxy
import (
"github.com/openziti-test-kitchen/zrok/util"
"github.com/openziti/sdk-golang/ziti"
"github.com/openziti/sdk-golang/ziti/config"
"github.com/pkg/errors"
"net/http"
)
func Run(cfg *Config) error {
zCfg, err := config.NewFromFile(cfg.IdentityPath)
if err != nil {
return errors.Wrap(err, "error loading config")
}
zCtx := ziti.NewContextWithConfig(zCfg)
zDialCtx := util.ZitiDialContext{Context: zCtx}
zTransport := http.DefaultTransport.(*http.Transport).Clone()
zTransport.DialContext = zDialCtx.Dial
proxy, err := util.NewProxy("http://zrok")
if err != nil {
return err
}
proxy.Transport = zTransport
return http.ListenAndServe(cfg.Address, util.NewProxyHandler(proxy))
}