Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use *real* yaml parser to handle mapping.yml #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
23 changes: 10 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package main

import (
"bufio"
"fmt"
"io/ioutil"
"log"
Expand All @@ -20,6 +19,7 @@ import (

"github.com/artyom/autoflags"
"golang.org/x/crypto/acme/autocert"
"gopkg.in/yaml.v2"
)

func main() {
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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 {
Expand Down