mvc... minimum viable curl

This commit is contained in:
Michael Quigley 2022-07-20 14:16:01 -04:00
parent e3bb8d73a6
commit 38f0076b61
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
3 changed files with 77 additions and 5 deletions

52
cmd/zrok/curl.go Normal file
View File

@ -0,0 +1,52 @@
package main
import (
"context"
"github.com/openziti/sdk-golang/ziti"
"github.com/openziti/sdk-golang/ziti/config"
"github.com/spf13/cobra"
"io"
"net"
"net/http"
"os"
"strings"
)
func init() {
rootCmd.AddCommand(curlCmd)
}
var curlCmd = &cobra.Command{
Use: "curl <identity>",
Short: "curl a zrok service",
Run: curl,
}
func curl(_ *cobra.Command, args []string) {
zCfg, err := config.NewFromFile(args[0])
if err != nil {
panic(err)
}
zCtx := ziti.NewContextWithConfig(zCfg)
zDialContext := zitiDialContext{context: zCtx}
zTransport := http.DefaultTransport.(*http.Transport).Clone()
zTransport.DialContext = zDialContext.Dial
client := &http.Client{Transport: zTransport}
resp, err := client.Get("http://zrok/")
if err != nil {
panic(err)
}
_, err = io.Copy(os.Stdout, resp.Body)
if err != nil {
panic(err)
}
}
type zitiDialContext struct {
context ziti.Context
}
func (dc *zitiDialContext) Dial(_ context.Context, _ string, addr string) (net.Conn, error) {
service := strings.Split(addr, ":")[0] // will always get passed host:port
return dc.context.Dial(service)
}

View File

@ -1,5 +1,6 @@
package proxy
type Config struct {
Address string
IdentityPath string
Address string
}

View File

@ -1,16 +1,35 @@
package proxy
import (
"fmt"
"github.com/openziti/sdk-golang/ziti"
"github.com/openziti/sdk-golang/ziti/config"
"github.com/pkg/errors"
"net/http"
"time"
)
func Run(cfg *Config) error {
return http.ListenAndServe(cfg.Address, &handler{})
zCfg, err := config.NewFromFile(cfg.IdentityPath)
if err != nil {
return errors.Wrap(err, "error loading config")
}
zCtx := ziti.NewContextWithConfig(zCfg)
handler := &handler{
zCfg: zCfg,
zCtx: zCtx,
}
return http.ListenAndServe(cfg.Address, handler)
}
type handler struct{}
type handler struct {
zCfg *config.Config
zCtx ziti.Context
}
func (self *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "zrok")
_, err := w.Write([]byte(time.Now().String()))
if err != nil {
panic(err)
}
}