I am new to Golang and I did set up a "hello world!" test message for a Golang API on our VPS. It works just fine at http://www.example.com:8080/hello. I would like however to move to HTTPS.
Could someone tell me step by step the right procedure to go from HTTP to HTTPS for a golang API? Thank you!
In case there is an issue with the golang code:
package main
import (
"fmt"
"log"
"net/http"
)
func main() {
http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, World")
})
fmt.Println("Server Started On Port 8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
3
Answers
Thanks to John Hanley for support that lead to this answer. First of all I did set the port 8443 for https by editing /etc/apache2/ports.conf:
Then I added a VirtualHost in the config of the example.com domain so that port 8443 acts as a proxy:
and you need to load the modules proxy and proxy_http using
e2enmod proxy
ande2enmod proxy_http
. After reloading apache, the API can be called at https://www.example.com:8443/hello.Use http.ListenAndServeTLS Instead
https://pkg.go.dev/net/http#ListenAndServeTLS
Adding on to @Dan ‘s answer. A little more complex implementation but enables you to configure it more.
If you want to use more than 1 set of certs
Create tls config
Add handler and start server