Skip to content

Instantly share code, notes, and snippets.

@jordanorelli
Created March 25, 2017 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jordanorelli/5ef1833d6f2d16d34c403b4b88f42139 to your computer and use it in GitHub Desktop.
Save jordanorelli/5ef1833d6f2d16d34c403b4b88f42139 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"net"
"net/http"
"os"
)
type rootHandler struct {
socket string
}
func (h rootHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("[%s] %s", h.socket, r.URL.Path)
fmt.Fprintf(w, "hi from %s", h.socket)
}
func listenOnSocket(name string) {
os.Remove(name)
lis, err := net.Listen("unix", name)
if err != nil {
log.Fatalf("couldn't listen on unix socket: %v", err)
}
// lol
os.Chmod(name, 0777)
log.Printf("unix socket listener: %v", lis)
http.Serve(lis, rootHandler{name})
}
func main() {
go listenOnSocket("/tmp/socket-one")
listenOnSocket("/tmp/socket-two")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment