duanmiexi2275
2016-11-22 16:38
浏览 54
已采纳

在Golang中将管道读取写入HTTP响应

Here is the schema :

Client sends a POST request to server A

server A process this and sends a GET to server B

server B sends a response through A to the client


I though the best idea was to make a pipe which would read the response of the GET, and write into the response of the POST, but I got many types problems.

func main() {
    r := mux.NewRouter()
    r.HandleFunc("/test/{hash}", testHandler)

    log.Fatal(http.ListenAndServe(":9095", r))
}

func handleErr(err error) {
    if err != nil {
        log.Fatalf("%s
", err)
    }
}


func testHandler(w http.ResponseWriter, r *http.Request){

    fmt.Println("FIRST REQUEST RECEIVED")
    vars := mux.Vars(r)
    hash := vars["hash"]
    read, write := io.Pipe()

    // writing without a reader will deadlock so write in a goroutine
    go func() {
        write, _ = http.Get("http://localhost:9090/test/" + hash)
        defer write.Close()
    }()

    w.Write(read)
}

When I run this I get the following error:

./ReverseProxy.go:61: cannot use read (type *io.PipeReader) as type []byte in argument to w.Write

Is there a way, to properly insert a io.PipeReader format into an http response? Or am I doing this in a totally wrong way?

图片转代码服务由CSDN问答提供 功能建议

以下是架构:

客户端向服务器A发送POST请求

服务器A对此进行处理并将GET发送到服务器B

服务器B通过A向客户端发送响应 \ n


尽管我最好的主意是制作一个可以读取GET响应并写入POST响应的管道,但是我遇到了很多类型问题。

  func main(){
r:= mux.NewRouter()
 r.HandleFunc(“ / test / {hash}”,testHandler)
 
 log.Fatal(http  .ListenAndServe(“:9095”,r))
} 
 
func handleErr(err error){
 if err!= nil {
 log.Fatalf(“%s 
”,err)
}  
} 
 
 
func testHandler(w http.ResponseWriter,r * http.Request){
 
 fmt.Println(“ FIRST REQUEST RECEIVED”)
 vars:= mux.Vars(r)
  hash:= vars [“ hash”] 
读取,写入:= io.Pipe()
 
 //没有读取器的写入将死锁,因此在goroutine中写入
 go func(){
 write,_  = http.Get(“ http:// localhost:9090 / t  est /“ +哈希)
延迟write.Close()
}()
 
 w.Write(read)
} 
   
 
 

何时 我运行此错误消息:

./ ReverseProxy.go:61:无法将read(类型* io.PipeReader)用作w.Write的参数中的[] byte类型 / p>

是否可以将io.PipeReader格式正确插入到http响应中? 还是我用完全错误的方式这样做?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duandie0921 2016-11-22 16:51
    已采纳

    You are not actually writing to it, you're replacing the pipe's write.

    Something along the lines of:

    func testHandler(w http.ResponseWriter, r *http.Request) {
    
        fmt.Println("FIRST REQUEST RECEIVED")
    
        vars := mux.Vars(r)
        hash := vars["hash"]
    
        read, write := io.Pipe()
    
        // writing without a reader will deadlock so write in a goroutine
        go func() {
            defer write.Close()
            resp, err := http.Get("http://localhost:9090/test/" + hash)
            if err != nil {
                return
            }
            defer resp.Body.Close()
            io.Copy(write, resp.Body)
    
        }()
    
        io.Copy(w, read)
    
    }
    

    Although, I agree with @JimB, for this instance, the pipe isn't even needed, something like this should be more efficient:

    func testHandler(w http.ResponseWriter, r *http.Request) {
        vars := mux.Vars(r)
        hash := vars["hash"]
    
        resp, err := http.Get("http://localhost:9090/test/" + hash)
        if err != nil {
            // handle error
            return
        }
        defer resp.Body.Close()
    
        io.Copy(w, resp.Body)
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题