wip
This commit is contained in:
51
config.go
Normal file
51
config.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
type (
|
||||
Config struct {
|
||||
Cert Cert
|
||||
Port int
|
||||
Endpoints map[string]Endpoint
|
||||
}
|
||||
|
||||
Cert struct {
|
||||
CRT string
|
||||
Key string
|
||||
}
|
||||
|
||||
Endpoint struct {
|
||||
From string
|
||||
To string
|
||||
BasicAuth string
|
||||
}
|
||||
)
|
||||
|
||||
func NewConfig() (Config, error) {
|
||||
var c Config
|
||||
|
||||
fs := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
|
||||
fs.StringVar(&c.Cert.CRT, "crt", "", "path to .crt")
|
||||
fs.StringVar(&c.Cert.Key, "key", "", "path to .key")
|
||||
fs.IntVar(&c.Port, "p", 56112, "port to listen on")
|
||||
f := fs.String("f", "/dev/null", `file of {endpoints:{"": {from:"", to:"", basicAuth:""}}}`)
|
||||
if err := fs.Parse(os.Args[1:]); err != nil {
|
||||
return c, err
|
||||
}
|
||||
|
||||
if b, err := os.ReadFile(*f); err != nil {
|
||||
return c, err
|
||||
} else if err := yaml.Unmarshal(b, &c); err != nil {
|
||||
return c, err
|
||||
} else if len(c.Endpoints) == 0 {
|
||||
return c, fmt.Errorf("no endpoints configured")
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
Reference in New Issue
Block a user