Switching from HTTP to HTTPS in Beego

I am trying to switch from HTTP to HTTPS:

func handler(w http.ResponseWriter, req *http.Request) {
    w.Header().Set("Content-Type", "text/plain")
    w.Write([]byte("This is an example server.\n"))
}

func main() {
    http.HandleFunc("/", handler)
    log.Printf("About to listen on 8080. Go to https://127.0.0.1:8080/")
    err := http.ListenAndServeTLS(":8080", "cert.pem", "key.pem", nil)
    if err != nil {
        log.Fatal(err)
    }
}

      

And I am getting the following error:

crypto/tls: failed to parse key PEM data

      

Now my application is running in HTTP mode and I want it to be running in HTTPS mode.

Can anyone suggest how to get it to work in HTTPS?

+3


source to share


1 answer


The error indicates that the file key.pem

cannot be parsed (may be invalid or lack permission to read its contents). Make sure the file is valid and has sufficient permissions.

For testing purposes, use generate_cert.go

the package crypto/tls

for the generation of reliable files cert.pem

and key.pem

.

To generate, run the following command (windows):



go run %GOROOT%/src/crypto/tls/generate_cert.go -host="127.0.0.1"

      

Linux:

go run $GOROOT/src/crypto/tls/generate_cert.go -host="127.0.0.1"

      

+3


source







All Articles