-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bdf1fcc
commit 2de1952
Showing
4 changed files
with
107 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"net" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
func main() { | ||
port := flag.Int("p", 3000, "port to serve on") | ||
flag.Parse() | ||
|
||
path := flag.Arg(0) | ||
if path == "" { | ||
fmt.Fprintf(os.Stderr, "missing required path") | ||
os.Exit(1) | ||
} | ||
|
||
client := &http.Client{ | ||
Transport: &http.Transport{ | ||
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) { | ||
return (&net.Dialer{}).DialContext(ctx, "unix", path) | ||
}, | ||
}, | ||
} | ||
|
||
err := http.ListenAndServe(fmt.Sprintf("localhost:%d", *port), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
req, err := http.NewRequest(r.Method, fmt.Sprintf("http://_%s?%s", r.URL.Path, r.URL.RawQuery), r.Body) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "failed to proxy request: %v", err) | ||
os.Exit(1) | ||
} | ||
|
||
req.Header = r.Header | ||
|
||
res, err := client.Do(req) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "failed to proxy request: %v", err) | ||
os.Exit(1) | ||
} | ||
defer res.Body.Close() | ||
|
||
header := w.Header() | ||
for k, v := range res.Header { | ||
header[k] = v | ||
} | ||
|
||
w.WriteHeader(res.StatusCode) | ||
if _, err := io.Copy(w, res.Body); err != nil { | ||
fmt.Fprintf(os.Stderr, "failed to proxy request: %v", err) | ||
os.Exit(1) | ||
} | ||
})) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "failed to serve: %v", err) | ||
os.Exit(1) | ||
} | ||
} |