caddy wip (#330)

This commit is contained in:
Michael Quigley 2023-08-11 14:15:59 -04:00
parent 8a9d91aa2c
commit f3dd6c0602
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
2 changed files with 28 additions and 5 deletions

View File

@ -1,8 +1,11 @@
package main package main
import ( import (
"fmt"
"github.com/caddyserver/caddy/v2" "github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"os"
"time" "time"
) )
@ -16,17 +19,36 @@ type caddyCommand struct {
func newCaddyCommand() *caddyCommand { func newCaddyCommand() *caddyCommand {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "caddy", Use: "caddy <configPath>",
Short: "Run an embedded caddy backend", Short: "Run an embedded caddy backend",
Args: cobra.ExactArgs(0), Args: cobra.ExactArgs(1),
} }
command := &caddyCommand{cmd: cmd} command := &caddyCommand{cmd: cmd}
cmd.Run = command.run cmd.Run = command.run
return command return command
} }
func (cmd *caddyCommand) run(_ *cobra.Command, _ []string) { func (cmd *caddyCommand) run(_ *cobra.Command, args []string) {
caddy.Run(&caddy.Config{}) if err := caddy.Run(&caddy.Config{}); err != nil {
panic(err)
}
data, err := os.ReadFile(args[0])
if err != nil {
panic(err)
}
var adapter caddyfile.Adapter
cfg, warn, err := adapter.Adapt(data, map[string]interface{}{"filename": args[0]})
if err != nil {
panic(err)
}
for _, w := range warn {
fmt.Println(w.Message)
}
fmt.Printf("cfg: %v\n", cfg)
if err := caddy.Load(cfg, true); err != nil {
panic(err)
}
for { for {
time.Sleep(30 * time.Minute) time.Sleep(30 * time.Minute)
} }

View File

@ -1,6 +1,7 @@
:2015 { servers :2015 {
route / { route / {
visitor_ip "stderr" visitor_ip "stderr"
} }
respond "hello, world!" respond "hello, world!"
protocols h1 h2c
} }