chore: clean the start of the server

This commit is contained in:
Fernandez Ludovic
2021-12-03 18:55:03 +01:00
parent a067f0ee4c
commit c36625cb56

25
app.go
View File

@ -55,27 +55,32 @@ var upgrader = websocket.Upgrader{
func main() {
flag.Parse()
http.HandleFunc("/data", dataHandler)
http.HandleFunc("/echo", echoHandler)
http.HandleFunc("/bench", benchHandler)
http.HandleFunc("/", whoamiHandler)
http.HandleFunc("/api", apiHandler)
http.HandleFunc("/health", healthHandler)
mux := http.NewServeMux()
mux.HandleFunc("/data", dataHandler)
mux.HandleFunc("/echo", echoHandler)
mux.HandleFunc("/bench", benchHandler)
mux.HandleFunc("/", whoamiHandler)
mux.HandleFunc("/api", apiHandler)
mux.HandleFunc("/health", healthHandler)
fmt.Println("Starting up on port " + port)
if cert == "" || key == "" {
log.Printf("Starting up on port %s", port)
log.Fatal(http.ListenAndServe(":"+port, mux))
}
if len(cert) > 0 && len(key) > 0 {
server := &http.Server{
Addr: ":" + port,
Handler: mux,
}
if len(ca) > 0 {
server.TLSConfig = setupMutualTLS(ca)
}
log.Printf("Starting up with TLS on port %s", port)
log.Fatal(server.ListenAndServeTLS(cert, key))
}
log.Fatal(http.ListenAndServe(":"+port, nil))
}
func setupMutualTLS(ca string) *tls.Config {