aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md4
-rw-r--r--cmd/gcp/main.go28
2 files changed, 27 insertions, 5 deletions
diff --git a/README.md b/README.md
index ca9464e..6c97d53 100644
--- a/README.md
+++ b/README.md
@@ -17,3 +17,7 @@ Additionally, the version under `gcp/` will use:
* `PORT` for the port to listen on, defaulting to 8080.
See https://golang.org/pkg/net/#Dial for details of the address format.
+
+If `ADDR` is set and starts with `/`, go-import-redirect will treat it
+as the path of a UNIX socket to create and listen on. `PORT` is also
+ignored.
diff --git a/cmd/gcp/main.go b/cmd/gcp/main.go
index 80f3114..c284bbc 100644
--- a/cmd/gcp/main.go
+++ b/cmd/gcp/main.go
@@ -8,6 +8,7 @@ package main
import (
"context"
"log"
+ "net"
"net/http"
"os"
"os/signal"
@@ -45,12 +46,29 @@ func main() {
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
go func() {
- port := os.Getenv("PORT")
- if port == "" {
- port = "8080"
+ var (
+ addr = os.Getenv("ADDR")
+ l net.Listener
+ err error
+ )
+ if addr != "" && addr[0] == '/' {
+ if l, err = net.Listen("unix", addr); err != nil {
+ log.Fatalln("net.Listen:", err)
+ }
+ // We do not do any authorization anyway, so 0666 makes sense here.
+ if err = os.Chmod(addr, 0666); err != nil {
+ log.Fatalln("os.Chmod:", err)
+ }
+ } else {
+ port := os.Getenv("PORT")
+ if port == "" {
+ port = "8080"
+ }
+ if l, err = net.Listen("tcp", os.Getenv("ADDR")+":"+port); err != nil {
+ log.Fatalln("net.Listen:", err)
+ }
}
- srv.Addr = os.Getenv("ADDR") + ":" + port
- if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
+ if err = srv.Serve(l); err != nil && err != http.ErrServerClosed {
log.Fatalln("server.ListenAndServe:", err)
}
}()