Skip to content

Commit dcba985

Browse files
committed
Refactor port logic to seperate function
1 parent 61dae8f commit dcba985

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

webserver/webserver.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,7 @@ func handleMessages() {
8080

8181
func Start() {
8282
r := mux.NewRouter()
83-
port := os.Getenv("PORT")
84-
if port == "" {
85-
log.Fatal("$PORT must be set")
86-
}
83+
8784
r.HandleFunc("/ws", handleConnections)
8885
go handleMessages()
8986

@@ -92,10 +89,19 @@ func Start() {
9289
r.HandleFunc("/interfaces", InterfaceMethod)
9390
r.HandleFunc("/startmining", StartMining)
9491
fmt.Printf("Server started on port :%v \n", port)
95-
portString := fmt.Sprintf(":%v", port)
96-
err := http.ListenAndServe(portString, r)
92+
err := http.ListenAndServe(GetPort(), r)
9793
if err != nil {
9894
fmt.Printf("Could not start the server: %v", err)
9995
}
10096

10197
}
98+
99+
func GetPort() string {
100+
var port = os.Getenv("PORT")
101+
// Set a default port if there is nothing in the environment
102+
if port == "" {
103+
port = "4747"
104+
fmt.Println("INFO: No PORT environment variable detected, defaulting to " + port)
105+
}
106+
return ":" + port
107+
}

0 commit comments

Comments
 (0)