2016-06-30 46 views
0

這真的讓我難住......我只是試圖設置一個基本的服務器使用眼鏡蛇和標準的http包。我是按照如何創建證書和密鑰的golang例如here,但無論怎樣我扔它,我沒有得到它要經過,並保持收到此錯誤:Golang ListenAndServeTLS不會拿我的證書

Cannot serve on http port: crypto/tls: failed to find certificate PEM data in certificate input, but did find a private key; PEM inputs may have been switched

所以我我認爲它一定是錯誤的,我的程序不會讓我處理證書。

這是我用它來分析我的標誌來啓動服務器:

var serverCmd = &cobra.Command{ 
    Use: "server", 
    Short: "start a compiler server", 
    Run: func(cmd *cobra.Command, args []string) { 
     addrUnsecure := "" 
     addrSecure := "" 

     addrUnsecure += ":" + strconv.FormatUint(serverPort, 10) 
     addrSecure += ":" + strconv.FormatUint(securePort, 10) 

     if noSSL { 
      addrSecure = "" 
     } else { 
      if secureOnly { 
       addrUnsecure = "" 
      } 
      if _, err := os.Stat(serverKey); os.IsNotExist(err) { 
       log.Error("Can't find ssl key %s. Use --no-ssl flag to disable", serverKey) 
       os.Exit(1) 
      } 
      if _, err := os.Stat(serverCert); os.IsNotExist(err) { 
       log.Error("Can't find ssl cert %s. Use --no-ssl flag to disable", serverCert) 
       os.Exit(1) 
      } 
     } 

     server.StartServer(addrUnsecure, addrSecure, serverCert, serverKey) 
    }, 
} 

func addServerFlags() { 
    serverCmd.Flags().Uint64VarP(&serverPort, "port", "p", setServerPort(), "set the listening port for http") 
    serverCmd.Flags().Uint64VarP(&securePort, "secure-port", "s", setSecurePort(), "set the listening port for https") 
    serverCmd.Flags().BoolVarP(&noSSL, "no-ssl", "n", setSSL(), "use only http") 
    serverCmd.Flags().BoolVarP(&secureOnly, "secure-only", "o", setSecureOnly(), "use only https") 
    serverCmd.Flags().StringVarP(&serverCert, "cert", "c", setDefaultServerCert(), "set the https certificate") 
    serverCmd.Flags().StringVarP(&serverKey, "key", "k", setDefaultServerKey(), "set the key to interact with the https certificate") 
} 

func setServerPort() uint64 { 
    return 9099 
} 

func setSecurePort() uint64 { 
    return 9098 
} 

func setSSL() bool { 
    return false 
} 

func setSecureOnly() bool { 
    return false 
} 

func setDefaultServerCert() string { 
    return "" 
} 

func setDefaultServerKey() string { 
    return "" 
} 

,這是實際服務器啓動功能:

func StartServer(addrUnsecure, addrSecure, key, cert string) { 
    log.Warn("Hello I'm the marmots' compilers server") 
    common.InitErisDir() 
    // Routes 

    http.HandleFunc("/", CompileHandler) 
    // Use SSL ? 
    log.Debug(cert) 
    if addrSecure != "" { 
     log.Debug("Using HTTPS") 
     log.WithField("=>", addrSecure).Debug("Listening on...") 
     if err := http.ListenAndServeTLS(addrUnsecure, cert, key, nil); err != nil { 
      log.Error("Cannot serve on http port: ", err) 
      os.Exit(1) 
     } 
    } 
    if addrUnsecure != "" { 
     log.Debug("Using HTTP") 
     log.WithField("=>", addrUnsecure).Debug("Listening on...") 
     if err := http.ListenAndServe(addrUnsecure, nil); err != nil { 
      log.Error("Cannot serve on http port: ", err) 
      os.Exit(1) 
     } 
    } 
} 

我在獲得這樣的兩臺Docker都在我的機器上本地運行Ubuntu 14.04和OS X.預先感謝誰能幫助我。這有點令人尷尬,因爲這看起來應該是一件非常簡單的事情,我敢打賭,這會讓我感到愚蠢。

回答

2

func StartServer(addrUnsecure, addrSecure, key, cert string) {

所以key是在第3位,在cert 4.在參數列表的地方。

server.StartServer(addrUnsecure, addrSecure, serverCert, serverKey)

現在,你撥打的FUNC與cert處於在4處的3處和key

也許交換它們會有幫助。

+0

哇...我現在覺得非常愚蠢。就像我說的...可能有些愚蠢。萬分感謝。 –

+0

用於閱讀所有代碼的榮譽! – eduncan911