sdk listener (#40)

This commit is contained in:
Michael Quigley 2022-10-03 16:15:57 -04:00
parent 99bf504301
commit 12b2a7466e
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62

View File

@ -7,9 +7,13 @@ import (
"github.com/openziti-test-kitchen/zrok/rest_client_zrok/tunnel"
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
"github.com/openziti-test-kitchen/zrok/zrokdir"
"github.com/openziti/sdk-golang/ziti"
"github.com/openziti/sdk-golang/ziti/config"
"github.com/openziti/sdk-golang/ziti/edge"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"math/rand"
"net/http"
"time"
)
@ -47,8 +51,9 @@ func (r *run) run(_ *cobra.Command, _ []string) {
}
type looper struct {
id int
done chan struct{}
id int
done chan struct{}
listener edge.Listener
}
func newLooper(id int) *looper {
@ -67,6 +72,10 @@ func (l *looper) run() {
if err != nil {
panic(err)
}
zif, err := zrokdir.ZitiIdentityFile("environment")
if err != nil {
panic(err)
}
zrok, err := zrokdir.ZrokClient(env.ApiEndpoint)
if err != nil {
panic(err)
@ -84,7 +93,13 @@ func (l *looper) run() {
}
logrus.Infof("service: %v, frontend: %v", tunnelResp.Payload.Service, tunnelResp.Payload.ProxyEndpoint)
time.Sleep(time.Duration(rand.Intn(5000)) * time.Millisecond)
go l.serviceListener(zif, tunnelResp.Payload.Service)
time.Sleep(time.Duration(rand.Intn(10000)) * time.Millisecond)
if l.listener != nil {
if err := l.listener.Close(); err != nil {
logrus.Errorf("error closing listener: %v", err)
}
}
untunnelReq := tunnel.NewUntunnelParams()
untunnelReq.Body = &rest_model_zrok.UntunnelRequest{
@ -95,3 +110,26 @@ func (l *looper) run() {
logrus.Errorf("error shutting down looper #%d: %v", l.id, err)
}
}
func (l *looper) serviceListener(zitiIdPath string, svcId string) {
zcfg, err := config.NewFromFile(zitiIdPath)
if err != nil {
logrus.Errorf("error opening ziti config '%v': %v", zitiIdPath, err)
return
}
opts := ziti.ListenOptions{
ConnectTimeout: 5 * time.Minute,
MaxConnections: 10,
}
if l.listener, err = ziti.NewContextWithConfig(zcfg).ListenWithOptions(svcId, &opts); err == nil {
if err := http.Serve(l.listener, l); err != nil {
logrus.Errorf("error serving: %v", err)
}
} else {
logrus.Errorf("error listening: %v", err)
}
}
func (l *looper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
}