mirror of
https://github.com/openziti/zrok.git
synced 2025-01-31 10:31:48 +01:00
mvc... minimum viable curl
This commit is contained in:
parent
e3bb8d73a6
commit
38f0076b61
52
cmd/zrok/curl.go
Normal file
52
cmd/zrok/curl.go
Normal 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)
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package proxy
|
||||
|
||||
type Config struct {
|
||||
Address string
|
||||
IdentityPath string
|
||||
Address string
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user