Skip to content

Instantly share code, notes, and snippets.

@jamesrr39
Created November 17, 2022 21:49
Show Gist options
  • Save jamesrr39/4b773d46cd4e6910c81c48c0fc70943e to your computer and use it in GitHub Desktop.
Save jamesrr39/4b773d46cd4e6910c81c48c0fc70943e to your computer and use it in GitHub Desktop.
Go HTTPS server example
package main
import (
"net/http"
"fmt"
)
/*
To generate a key/cert pair:
openssl genrsa 2048 > host.key
chmod 400 host.key
openssl req -new -x509 -nodes -sha256 -days 365 -key host.key -out host.cert
*/
func main() {
s := &http.Server{
Addr: ":443",
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "hello world")
})
err := s.ListenAndServeTLS("host.cert", "host.key")
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment