Skip to content

Commit

Permalink
teach http server to listen on port 80 when launched as "insecure htt…
Browse files Browse the repository at this point in the history
…p server" without tls
  • Loading branch information
HouzuoGuo committed Nov 26, 2017
1 parent 74dbc13 commit eb32ef2
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
12 changes: 6 additions & 6 deletions daemon/httpd/httpd.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,20 +185,20 @@ func (daemon *Daemon) Initialise() error {
StartAndBlockNoTLS starts HTTP daemon and serve unencrypted connections. Blocks caller until StopNoTLS function is called.
You may call this function only after having called Initialise()!
*/
func (daemon *Daemon) StartAndBlockNoTLS() error {
func (daemon *Daemon) StartAndBlockNoTLS(fallbackPort int) error {
/*
In order to determine the listener's port:
- On ElasticBeanstalk (and several other scenarios), listen on port number specified in environment PORT value.
- If TLS listener has not yet started, listen on the port specified by user configuration (prone to race condition).
In this way, caller of the function can determine whether to listen for TLS or ordinary traffic, one or the other.
- If TLS configuration does not exist, listen on the port specified by user configuration.
- If TLS configuration exists, then listen on the fallback port.
Not very elegant, but it should help to launch HTTP daemon in TLS only, TLS + HTTP, and HTTP only scenarios.
*/
var port int
if envPort := strings.TrimSpace(os.Getenv("PORT")); envPort == "" {
if daemon.serverWithTLS == nil {
if daemon.TLSCertPath == "" {
port = daemon.Port
} else {
return fmt.Errorf("httpd.StartAndBlockNoTLS: was about to listen on port %d but TLS listener is already using it", daemon.Port)
port = fallbackPort
}
} else {
iPort, err := strconv.Atoi(envPort)
Expand Down
2 changes: 1 addition & 1 deletion daemon/httpd/httpd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestHTTPD_StartAndBlock(t *testing.T) {
// HTTP daemon is expected to start in two seconds
var stoppedNormally bool
go func() {
if err := daemon.StartAndBlockNoTLS(); err != nil {
if err := daemon.StartAndBlockNoTLS(0); err != nil {
t.Fatal(err)
}
stoppedNormally = true
Expand Down
2 changes: 1 addition & 1 deletion launcher/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func TestConfig(t *testing.T) {
httpDaemon := config.GetHTTPD()
// HTTP daemon is expected to start in two seconds
go func() {
if err := httpDaemon.StartAndBlockNoTLS(); err != nil {
if err := httpDaemon.StartAndBlockNoTLS(0); err != nil {
t.Fatal(err)
}
}()
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func main() {
}()
case launcher.InsecureHTTPDName:
go func() {
daemonErrs <- config.GetHTTPD().StartAndBlockNoTLS()
daemonErrs <- config.GetHTTPD().StartAndBlockNoTLS(80)
}()
case launcher.MaintenanceName:
go func() {
Expand Down

0 comments on commit eb32ef2

Please sign in to comment.