Reputation: 71
I'm trying to create a REST Server with TLS in Golang, but the program is not working:
http.Handle("/", gorest.Handle())
err = http.ListenAndServeTLS(":443", "cert.pem", "key.pem", nil)
If I run it with a non-root user I get listen tcp 127.0.0.1:443: permission denied
. Running it with root user does not show previous message but execution blocks when invoking
ListenAndServeTLS. OS used is Linux x86_64. Any ideas?
Upvotes: 2
Views: 4043
Reputation: 57599
Ports <= 1024 are privileged ports. You can't use them unless you're root or have the explicit permission to use them. See this answer for an explanation or wikipedia or something you trust more.
See this answer for a solution to allow your application to open these ports without giving them superuser permissions (which is a bad idea). Money Quote:
sudo setcap 'cap_net_bind_service=+ep' /opt/yourGoBinary
ListenAndServe
and its TLS counterpart ListeAndServeTLS
are blocking.
The http handler returned by gorest.Handle()
is executed concurrently when /
is accessed.
If you want to have other code running concurrently, start a goroutine before running ListeAndServe
or implement different http handlers.
Example using goroutines:
func printStuff() {
time.Sleep(1 * time.Second)
fmt.Println("foo")
}
func main() {
go printSTuff()
log.Fatal(http.ListeAndServe(":80"))
}
Upvotes: 10