Skip to content

Commit

Permalink
Add support for static sites handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Artyom Pervukhin committed May 16, 2016
1 parent 32027ca commit 63f28f4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ Run:

leproxy -addr :https -map /path/to/mapping.yml -cache /path/to/letsencrypt.cache

`mapping.yml` contains host-to-backend mapping, where backend can be specified
either as host:port for TCP or an absolute path for unix socket connections.
`mapping.yml` contains host-to-backend mapping, where backend can be specified as:

* host:port for TCP connections to backend;
* absolute path for unix socket connections;
* 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
static.example.com: /var/www/
11 changes: 9 additions & 2 deletions leproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"net/http"
"net/http/httputil"
"os"
"path/filepath"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -75,12 +76,18 @@ func setProxy(mapping map[string]string) (http.Handler, error) {
}
mux := http.NewServeMux()
for hostname, backendAddr := range mapping {
if strings.Contains(hostname, "/") {
if strings.ContainsRune(hostname, os.PathSeparator) {
return nil, fmt.Errorf("invalid hostname: %q", hostname)
}
network := "tcp"
if strings.HasPrefix(backendAddr, "/") {
if filepath.IsAbs(backendAddr) {
network = "unix"
if strings.HasSuffix(backendAddr, string(os.PathSeparator)) {
// path specified as directory with explicit trailing
// slash; add this path as static site
mux.Handle(hostname+"/", http.FileServer(http.Dir(backendAddr)))
continue
}
}
rp := &httputil.ReverseProxy{
Director: func(req *http.Request) {
Expand Down

0 comments on commit 63f28f4

Please sign in to comment.