Skip to content

Instantly share code, notes, and snippets.

@seedifferently
Created November 7, 2014 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seedifferently/5fa617bd944c107bc9db to your computer and use it in GitHub Desktop.
Save seedifferently/5fa617bd944c107bc9db to your computer and use it in GitHub Desktop.
Example Go webserver with combined http and https support
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