Skip to content

Commit c36625c

Browse files
committed
chore: clean the start of the server
1 parent a067f0e commit c36625c

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

app.go

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,32 @@ var upgrader = websocket.Upgrader{
5555
func main() {
5656
flag.Parse()
5757

58-
http.HandleFunc("/data", dataHandler)
59-
http.HandleFunc("/echo", echoHandler)
60-
http.HandleFunc("/bench", benchHandler)
61-
http.HandleFunc("/", whoamiHandler)
62-
http.HandleFunc("/api", apiHandler)
63-
http.HandleFunc("/health", healthHandler)
64-
65-
fmt.Println("Starting up on port " + port)
66-
67-
if len(cert) > 0 && len(key) > 0 {
68-
server := &http.Server{
69-
Addr: ":" + port,
70-
}
58+
mux := http.NewServeMux()
59+
mux.HandleFunc("/data", dataHandler)
60+
mux.HandleFunc("/echo", echoHandler)
61+
mux.HandleFunc("/bench", benchHandler)
62+
mux.HandleFunc("/", whoamiHandler)
63+
mux.HandleFunc("/api", apiHandler)
64+
mux.HandleFunc("/health", healthHandler)
65+
66+
if cert == "" || key == "" {
67+
log.Printf("Starting up on port %s", port)
68+
69+
log.Fatal(http.ListenAndServe(":"+port, mux))
70+
}
7171

72-
if len(ca) > 0 {
73-
server.TLSConfig = setupMutualTLS(ca)
74-
}
72+
server := &http.Server{
73+
Addr: ":" + port,
74+
Handler: mux,
75+
}
7576

76-
log.Fatal(server.ListenAndServeTLS(cert, key))
77+
if len(ca) > 0 {
78+
server.TLSConfig = setupMutualTLS(ca)
7779
}
78-
log.Fatal(http.ListenAndServe(":"+port, nil))
80+
81+
log.Printf("Starting up with TLS on port %s", port)
82+
83+
log.Fatal(server.ListenAndServeTLS(cert, key))
7984
}
8085

8186
func setupMutualTLS(ca string) *tls.Config {

0 commit comments

Comments
 (0)