cf-based config (#23)

This commit is contained in:
Michael Quigley 2022-08-09 11:34:00 -04:00
parent 8606072b80
commit 36b08f1e00
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
5 changed files with 33 additions and 23 deletions

View File

@ -2,11 +2,7 @@ package main
import (
"github.com/openziti-test-kitchen/zrok/controller"
"github.com/openziti-test-kitchen/zrok/controller/store"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"strconv"
"strings"
)
func init() {
@ -20,35 +16,24 @@ type controllerCommand struct {
func newControllerCommand() *controllerCommand {
cmd := &cobra.Command{
Use: "controller",
Use: "controller <configPath>",
Short: "Start a zrok controller",
Aliases: []string{"ctrl"},
Args: cobra.ExactArgs(1),
}
ccmd := &controllerCommand{
cmd: cmd,
}
cmd.Run = ccmd.run
cmd.Flags().StringVarP(&ccmd.dbPath, "database", "d", "zrok.db", "Path to zrok controller database")
return ccmd
}
func (cmd *controllerCommand) run(_ *cobra.Command, _ []string) {
tokens := strings.Split(endpoint, ":")
if len(tokens) != 2 {
panic(errors.Errorf("malformed endpoint '%v'", endpoint))
}
host := tokens[0]
port, err := strconv.Atoi(tokens[1])
func (cmd *controllerCommand) run(_ *cobra.Command, args []string) {
cfg, err := controller.LoadConfig(args[0])
if err != nil {
panic(err)
}
if err := controller.Run(&controller.Config{
Endpoint: controller.EndpointConfig{
Host: host,
Port: port,
},
Store: &store.Config{Path: cmd.dbPath},
}); err != nil {
if err := controller.Run(cfg); err != nil {
panic(err)
}
}

View File

@ -1,10 +1,14 @@
package controller
import "github.com/openziti-test-kitchen/zrok/controller/store"
import (
"github.com/michaelquigley/cf"
"github.com/openziti-test-kitchen/zrok/controller/store"
"github.com/pkg/errors"
)
type Config struct {
Endpoint EndpointConfig
Proxy ProxyConfig
Endpoint *EndpointConfig
Proxy *ProxyConfig
Store *store.Config
}
@ -16,3 +20,11 @@ type EndpointConfig struct {
type ProxyConfig struct {
UrlTemplate string
}
func LoadConfig(path string) (*Config, error) {
cfg := &Config{}
if err := cf.BindYaml(cfg, path, cf.DefaultOptions()); err != nil {
return nil, errors.Wrapf(err, "error loading controller config '%v'", path)
}
return cfg, nil
}

9
etc/ctrl.yml Normal file
View File

@ -0,0 +1,9 @@
endpoint:
host: 0.0.0.0
port: 10888
proxy:
url_template: "http://{id}.zrok.quigley.com:10111/"
store:
path: zrok.db

2
go.mod
View File

@ -46,6 +46,7 @@ require (
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/michaelquigley/cf v0.0.12 // indirect
github.com/miekg/pkcs11 v1.1.1 // indirect
github.com/mitchellh/go-ps v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
@ -73,4 +74,5 @@ require (
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

2
go.sum
View File

@ -337,6 +337,8 @@ github.com/mattn/go-sqlite3 v1.14.14/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d h1:5PJl274Y63IEHC+7izoQE9x6ikvDFZS2mDVS3drnohI=
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
github.com/michaelquigley/cf v0.0.12 h1:DEGncGj4gikWMSkpnAD5h7l472hjGgnaGYnT2zAS1V8=
github.com/michaelquigley/cf v0.0.12/go.mod h1:yzQJzoNRfeyB4pr5lxia1dtoS/7X96IoM5uZEIW6nzo=
github.com/michaelquigley/pfxlog v0.6.9 h1:K/weH6ARu58aEDQi0ccinItvV958CeO6Ri4jqeFwd/w=
github.com/michaelquigley/pfxlog v0.6.9/go.mod h1:D2vg1tPyPdSXWWkSnGk6Fomwh5b3clwVJDUh71tq8Sk=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=