Skip to content

Instantly share code, notes, and snippets.

@xphyr
Created May 1, 2020 13:36
Show Gist options
  • Save xphyr/85a1bb7707949ff6dfbf729e4ffc892b to your computer and use it in GitHub Desktop.
Save xphyr/85a1bb7707949ff6dfbf729e4ffc892b to your computer and use it in GitHub Desktop.
Go route test app
package main
import (
"fmt"
"log"
"net/http"
"os"
)
var (
myHostname = os.Getenv("HOSTNAME")
)
// Run takes a http port, an https port and a ssl cert path and starts a http and https server on the given ports
func Run(addr string, sslAddr string, ssl map[string]string) chan error {
errs := make(chan error)
// Starting HTTP server
go func() {
log.Printf("Staring HTTP service on %s ...", addr)
if err := http.ListenAndServe(addr, nil); err != nil {
errs <- err
}
}()
// Starting HTTPS server
go func() {
log.Printf("Staring HTTPS service on %s ...", addr)
if err := http.ListenAndServeTLS(sslAddr, ssl["cert"], ssl["key"], nil); err != nil {
errs <- err
}
}()
return errs
}
func sampleHandler(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte(fmt.Sprintf("I am running on pod %s\n", myHostname)))
}
func main() {
http.HandleFunc("/", sampleHandler)
errs := Run(":8080", ":8443", map[string]string{
"cert": "/var/run/secrets/openshift.io/services_serving_certs/tls.crt",
"key": "/var/run/secrets/openshift.io/services_serving_certs/tls.key",
})
// This will run forever until channel receives error
select {
case err := <-errs:
log.Printf("Could not start serving service due to (error: %s)", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment