diff --git a/README.md b/README.md index 70a82c4..1a94ae3 100644 --- a/README.md +++ b/README.md @@ -19,12 +19,13 @@ Run: * absolute path with trailing slash to serve files from given directory. Example: - - subdomain1.example.com: 127.0.0.1:8080 - subdomain2.example.com: /var/run/http.socket - subdomain3.example.com: @abstractUnixSocket - uploads.example.com: https://uploads-bucket.s3.amazonaws.com - static.example.com: /var/www/ +```yaml +subdomain1.example.com: 127.0.0.1:8080 +subdomain2.example.com: /var/run/http.socket +subdomain3.example.com: "@abstractUnixSocket" # double quote needed! see: https://yaml.org/spec/1.2/spec.html#id2774058 +uploads.example.com: https://uploads-bucket.s3.amazonaws.com +static.example.com: /var/www/ +``` Note that when `@name` backend is specified, connection to abstract unix socket is made in a manner compatible with some other implementations like uWSGI, that diff --git a/go.mod b/go.mod index 4c70810..a30b175 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/artyom/leproxy require ( github.com/artyom/autoflags v1.1.0 golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 + gopkg.in/yaml.v2 v2.4.0 ) go 1.13 diff --git a/go.sum b/go.sum index 27b419a..5361dd0 100644 --- a/go.sum +++ b/go.sum @@ -9,3 +9,7 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= diff --git a/main.go b/main.go index 548aac9..005e6bc 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,6 @@ package main import ( - "bufio" "fmt" "io/ioutil" "log" @@ -20,6 +19,7 @@ import ( "github.com/artyom/autoflags" "golang.org/x/crypto/acme/autocert" + "gopkg.in/yaml.v2" ) func main() { @@ -133,7 +133,7 @@ func setProxy(mapping map[string]string) (http.Handler, error) { // append \0 to address so addrlen for connect(2) is // calculated in a way compatible with some other // implementations (i.e. uwsgi) - network, backendAddr = "unix", backendAddr+string(0) + network, backendAddr = "unix", backendAddr+"0" } else if filepath.IsAbs(backendAddr) { network = "unix" if strings.HasSuffix(backendAddr, string(os.PathSeparator)) { @@ -178,18 +178,15 @@ func readMapping(file string) (map[string]string, error) { } defer f.Close() m := make(map[string]string) - sc := bufio.NewScanner(f) - for sc.Scan() { - if b := sc.Bytes(); len(b) == 0 || b[0] == '#' { - continue - } - s := strings.SplitN(sc.Text(), ":", 2) - if len(s) != 2 { - return nil, fmt.Errorf("invalid line: %q", sc.Text()) - } - m[strings.TrimSpace(s[0])] = strings.TrimSpace(s[1]) + raw, err := ioutil.ReadAll(f) + if err != nil { + return nil, err + } + err = yaml.Unmarshal(raw, &m) + if err != nil { + return nil, err } - return m, sc.Err() + return m, nil } func keys(m map[string]string) []string {