gitee.com/liuxuezhan/go-micro-v1.18.0@v1.0.0/api/handler/unix/unix.go (about)

     1  // Package unix reads from a unix socket expecting it to be in /tmp/path
     2  package unix
     3  
     4  import (
     5  	"fmt"
     6  	"io"
     7  	"net"
     8  	"net/http"
     9  	"path/filepath"
    10  )
    11  
    12  type Handler struct{}
    13  
    14  func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    15  	sock := fmt.Sprintf("%s.sock", filepath.Clean(r.URL.Path))
    16  	path := filepath.Join("/tmp", sock)
    17  
    18  	c, err := net.Dial("unix", path)
    19  	if err != nil {
    20  		http.Error(w, err.Error(), 500)
    21  		return
    22  	}
    23  	go io.Copy(c, r.Body)
    24  	// write response
    25  	io.Copy(w, c)
    26  }
    27  
    28  func (h *Handler) String() string {
    29  	return "unix"
    30  }