Created
November 7, 2014 02:33
-
-
Save seedifferently/5fa617bd944c107bc9db to your computer and use it in GitHub Desktop.
Example Go webserver with combined http and https support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"net/http" | |
"io" | |
"strconv" | |
) | |
func HelloServer(c http.ResponseWriter, req *http.Request) { | |
body := "Hello World\n" | |
c.Header().Set("Content-Type", "text/plain") | |
c.Header().Set("Content-Length", strconv.Itoa(len(body))) | |
io.WriteString(c, body); | |
} | |
func main() { | |
done := make(chan bool) | |
go func() { | |
http.ListenAndServeTLS(":443", "cert.pem", "key.pem", http.HandlerFunc(HelloServer)) | |
done <- true | |
}() | |
go func() { | |
http.ListenAndServe(":80", http.HandlerFunc(HelloServer)) | |
done <- true | |
}() | |
<- done | |
<- done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment