2017-04-26 42 views
-1

我有用golang編寫的restful接口。我需要在每個端點上進行身份驗證。一旦完成身份驗證,我必須將其轉發回tcp服務器。 我創建了一個tcp客戶端,任何來自該通道的值都將被髮送到tcp服務器。該通道從http請求主體填充。 問題是,一旦我發出curl命令,客戶端就無法響應;所以顯然我做錯了什麼事不知道錯在哪裏。有沒有人對我的問題可能會有什麼見解?在c <- bodyStringTCP客戶端無法正常工作golang

package main 

      import (
       "bufio" 
       "encoding/json" 
       "flag" 
       "fmt" 
       "io/ioutil" 
       "log" 
       "net" 
       "net/http" 
       "net/http/httputil" 
       "net/url" 
       "os" 
       "strconv" 

       auth "github.com/abbot/go-http-auth" 
      ) 

      type Configuration struct { 
       Server  string 
       Port   int64 
       UserName  string 
       Pwd   string 
       Realm   string 
       ProxyPort  int64 
       Edeserver  string 

      } 

      var (
       Config *Configuration 
       logp = flag.Bool("log", false, "enable logging") 
      ) 

      func ReadConfiguration() { 
       file, _ := os.Open("Config.json") 
       decoder := json.NewDecoder(file) 
       Config = &Configuration{} 
       err := decoder.Decode(&Config) 
       if err != nil { 
        fmt.Println("error:", err) 
       } 

      } 

      func Secret(user, realm string) string { 
       if user == Config.UserName { 
        // password is "hello" 
        return Config.Pwd 
       } 
       return "" 
      } 

      func reverseProxyTows(w http.ResponseWriter, authenticatedRequest *auth.AuthenticatedRequest) { 
       req := &authenticatedRequest.Request 

       if *logp { 
        log.Println(" Authenticated Username ", authenticatedRequest.Username) 
        log.Println(" Authenticated URL ", req.URL.RequestURI()) 
       } 

       destinationURL := fmt.Sprintf("http://%s:%d", Config.Server, Config.Port) 
       u, err := url.Parse(destinationURL) 
       if err != nil { 
        log.Fatal(err) 
       } 

       if *logp { 
        log.Println("reverse_proxy", u) 
       } 

       reverseProxy := httputil.NewSingleHostReverseProxy(u) 

       reverseProxy.ServeHTTP(w, req) 
      } 

      func openConnectionTotcp(edechannel chan string) { 
       conn, _ := net.Dial("tcp", Config.Edeserver) 
       text := <-edechannel 
       fmt.Fprintf(conn, text+"\n") 
       message, _ := bufio.NewReader(conn).ReadString('\n') 
       fmt.Print("Message from server: " + message) 
      } 


      func main() { 
       ReadConfiguration() 
       flag.Parse() 
       c := make(chan string) 
       go openConnectionTotcp(c) 

       fmt.Printf("Started proxy to destination server %v:%d and is listening on %d ", Config.Server, Config.Port, Config.ProxyPort) 

       authenticator := auth.NewBasicAuthenticator(Config.Realm, Secret) 
       http.HandleFunc("/", authenticator.Wrap(reverseProxyTows)) 
       http.HandleFunc("/tyrion/1", authenticator.Wrap(func(w http.ResponseWriter, authenticatedRequest *auth.AuthenticatedRequest) { 
        req := &authenticatedRequest.Request 
        bodyBytes, err2 := ioutil.ReadAll(req.Body) 
        if err2 != nil { 
         log.Fatal(err2) 
        } 
        bodyString := string(bodyBytes) 
        c <- bodyString 
        fmt.Fprintf(w, "success") 
       })) 
       http.ListenAndServe(":"+strconv.FormatInt(Config.ProxyPort, 10), nil) 
      } 
+1

看起來好像你執行流被阻塞在「c < - bodyString」上。這可能發生在沒有任何渠道消費者的情況下。請確定你在這裏沒有任何錯誤「conn,_:= net.Dial(」tcp「,Config.Edeserver)」 –

+0

開始時,你不檢查'net.Dial'中的錯誤或'ReadString',你沒有關閉tcp連接,如果你想嘗試讀取多個消息,那麼你就拋棄了'bufio.Reader',它可能有緩衝數據。你也只需要調用'tcp.Dial',然後退出goroutine,所以即使它工作,它也只能用於第一個http請求。 – JimB

回答

0

你執行代碼塊,因爲沒有什麼似乎從無緩衝通道讀取。該行將暫停執行,直到另一個例程從該通道讀取。

+0

nope不是真正的openConnectionTotcp有一個chan參數,並且去例行程序讀取它'''func openConnectionTotcp(edechannel chan string)conn,_:= net.Dial(「tcp」,Config.Edeserver) text: = <-edechannel fmt.Fprintf(conn,text +「\ n」) message,_:= bufio.NewReader(conn).ReadString('\ n') fmt.Print(「Message from server:」+ message ) } ''' – harry

+0

該例程將消耗一條消息,然後返回,之後對該通道的任何寫入都將永遠阻塞。你也拋棄了tcp.Dial的錯誤,所以誰知道那個例程中發生了什麼。 – Adrian

+0

由於我運行的是本地tcp服務器,因此tcp dial工作正常,我確實在tcp服務器端看到了消息。做捲曲請求的客戶端掛起。那麼這個例程會消耗一條消息,然後返回,'它會返回,因爲我並不是首先關閉通道。「 – harry

相關問題