Make Port and http/https configurable for the test container

It introduces env vars:

* HTTP_PORT: choose a port to serve. If not set, uses 8080
* HTTPS_PORT: choose to serve HTTPS and on which port
* HTTPS_CERT_PATH: Path to the cert to use when serving HTTPS
* HTTPS_KEY_PATH: Path to the ssl key to use when serving HTTPS

Change-Id: I5078ba0345999dadd7d3850f07c538a617c54e46
Signed-off-by: Antoni Segura Puimedon <celebdor@gmail.com>
This commit is contained in:
Antoni Segura Puimedon 2018-08-01 11:58:28 +02:00
parent f02385f453
commit a8ff6c4cad
2 changed files with 36 additions and 11 deletions

Binary file not shown.

View File

@ -1,21 +1,46 @@
package main
import (
"fmt"
"log"
"net/http"
"os"
"fmt"
"log"
"net/http"
"os"
"strings"
)
func handler(w http.ResponseWriter, r *http.Request) {
hostname, err := os.Hostname()
log.Println("Received request")
if err == nil {
fmt.Fprintf(w, "%s: HELLO! I AM ALIVE!!!\n", hostname)
}
hostname, err := os.Hostname()
log.Println("Received request")
if err == nil {
fmt.Fprintf(w, "%s: HELLO! I AM ALIVE!!!\n", hostname)
}
}
func main() {
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(":8080", nil))
http.HandleFunc("/", handler)
httpsPort, httpsPortPresent := os.LookupEnv("HTTPS_PORT")
var port string
if httpsPortPresent {
port = ":" + strings.TrimSpace(httpsPort)
cert, certPresent := os.LookupEnv("HTTPS_CERT_PATH")
key, keyPresent := os.LookupEnv("HTTPS_KEY_PATH")
if !certPresent || !keyPresent {
log.Fatal("HTTPS_PORT configured but missing HTTPS_CERT_PATH and/or HTTPS_KEY_PATH")
}
log.Fatal(http.ListenAndServeTLS(port, cert, key, nil))
} else {
httpPort, confPresent := os.LookupEnv("HTTP_PORT")
if confPresent {
port = ":" + strings.TrimSpace(httpPort)
} else {
port = ":8080"
}
log.Fatal(http.ListenAndServe(port, nil))
}
}