caddyfile/proxy backend cleanup (#391)

This commit is contained in:
Michael Quigley 2023-08-28 14:56:54 -04:00
parent a96f3e9f96
commit d1f88e4b6c
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
5 changed files with 75 additions and 79 deletions

View File

@ -4,7 +4,6 @@ import (
"fmt"
tea "github.com/charmbracelet/bubbletea"
"github.com/openziti/zrok/endpoints"
"github.com/openziti/zrok/endpoints/caddyf"
"github.com/openziti/zrok/endpoints/proxy"
"github.com/openziti/zrok/environment"
"github.com/openziti/zrok/environment/env_core"
@ -169,13 +168,13 @@ func (cmd *sharePublicCommand) run(_ *cobra.Command, args []string) {
}()
case "caddy":
cfg := &caddyf.BackendConfig{
cfg := &proxy.CaddyfileBackendConfig{
CaddyfilePath: target,
Shr: shr,
Requests: requests,
}
be, err := caddyf.NewBackend(cfg)
be, err := proxy.NewCaddyfileBackend(cfg)
if err != nil {
if !panicInstead {
tui.Error("unable to create caddy backend", err)

View File

@ -1,51 +0,0 @@
package caddyf
import (
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/openziti/zrok/endpoints"
"github.com/openziti/zrok/sdk"
"github.com/sirupsen/logrus"
)
type BackendConfig struct {
CaddyfilePath string
Shr *sdk.Share
Requests chan *endpoints.Request
}
type Backend struct {
cdycfg []byte
}
func NewBackend(cfg *BackendConfig) (*Backend, error) {
cdyf, err := preprocessCaddyfile(cfg.CaddyfilePath, cfg.Shr)
if err != nil {
return nil, err
}
var adapter caddyfile.Adapter
adapter.ServerType = httpcaddyfile.ServerType{}
cdycfg, warnings, err := adapter.Adapt([]byte(cdyf), map[string]interface{}{"filename": cfg.CaddyfilePath})
if err != nil {
return nil, err
}
for _, warning := range warnings {
logrus.Warnf("%v [%d] (%v): %v", cfg.CaddyfilePath, warning.Line, warning.Directive, warning.Message)
}
return &Backend{cdycfg: cdycfg}, nil
}
func (b *Backend) Run() error {
if err := caddy.Run(&caddy.Config{}); err != nil {
return err
}
if err := caddy.Load(b.cdycfg, true); err != nil {
return err
}
return nil
}
func (b *Backend) Requests() func() int32 {
return nil
}

View File

@ -1,24 +0,0 @@
package caddyf
import (
"github.com/openziti/zrok/sdk"
"os"
"strings"
"text/template"
)
func preprocessCaddyfile(inF string, shr *sdk.Share) (string, error) {
input, err := os.ReadFile(inF)
if err != nil {
return "", err
}
tmpl, err := template.New(inF).Parse(string(input))
if err != nil {
return "", err
}
output := new(strings.Builder)
if err := tmpl.Execute(output, shr); err != nil {
return "", err
}
return output.String(), nil
}

View File

@ -0,0 +1,72 @@
package proxy
import (
"fmt"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/openziti/zrok/endpoints"
"github.com/openziti/zrok/sdk"
"github.com/sirupsen/logrus"
"os"
"strings"
"text/template"
)
type CaddyfileBackendConfig struct {
CaddyfilePath string
Shr *sdk.Share
Requests chan *endpoints.Request
}
type CaddyfileBackend struct {
cfg []byte
}
func NewCaddyfileBackend(cfg *CaddyfileBackendConfig) (*CaddyfileBackend, error) {
cdyf, err := preprocessCaddyfile(cfg.CaddyfilePath, cfg.Shr)
if err != nil {
return nil, err
}
var adapter caddyfile.Adapter
adapter.ServerType = httpcaddyfile.ServerType{}
caddyCfg, warnings, err := adapter.Adapt([]byte(cdyf), map[string]interface{}{"filename": cfg.CaddyfilePath})
if err != nil {
return nil, err
}
for _, warning := range warnings {
logrus.Warnf("%v [%d] (%v): %v", cfg.CaddyfilePath, warning.Line, warning.Directive, warning.Message)
}
return &CaddyfileBackend{cfg: caddyCfg}, nil
}
func (b *CaddyfileBackend) Run() error {
if err := caddy.Load(b.cfg, true); err != nil {
return err
}
return nil
}
func (b *CaddyfileBackend) Requests() func() int32 {
return nil
}
func preprocessCaddyfile(inF string, shr *sdk.Share) (string, error) {
input, err := os.ReadFile(inF)
if err != nil {
return "", err
}
tmpl, err := template.New(inF).Parse(string(input))
if err != nil {
return "", err
}
output := new(strings.Builder)
if err := tmpl.Execute(output, &caddyfileData{ZrokBindAddress: fmt.Sprintf("zrok/%s", shr.Token)}); err != nil {
return "", err
}
return output.String(), nil
}
type caddyfileData struct {
ZrokBindAddress string
}

View File

@ -1,4 +1,4 @@
http:// {
bind zrok/{{ .Token }}
bind {{ .ZrokBindAddress }}
reverse_proxy 127.0.0.1:3000
}