Skip to content

Commit 1b7353f

Browse files
committed
optimization
1 parent bb84a80 commit 1b7353f

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

main.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"log"
66
"net/http"
77
"net/url"
8+
"os"
89
"sync"
910
"time"
1011

@@ -45,14 +46,20 @@ var (
4546
func main() {
4647
log.Println("Starting Concurrent Web Crawler with Frontend")
4748

49+
// Get the port from the environment (for Railway) or default to 8080
50+
port := os.Getenv("PORT")
51+
if port == "" {
52+
port = "8080"
53+
}
54+
4855
// Start HTTP server
4956
go func() {
5057
http.HandleFunc("/", serveFrontend)
5158
http.HandleFunc("/ws", handleWebSocket)
5259
http.HandleFunc("/stop", handleStop)
5360
http.HandleFunc("/start", handleStart)
54-
log.Println("Starting HTTP server on :8080")
55-
log.Fatal(http.ListenAndServe(":8080", nil))
61+
log.Println("Starting HTTP server on :" + port)
62+
log.Fatal(http.ListenAndServe(":"+port, nil))
5663
}()
5764

5865
// Keep main thread alive
@@ -199,7 +206,11 @@ func serveFrontend(w http.ResponseWriter, r *http.Request) {
199206
</div>
200207
</div>
201208
<script>
202-
const socket = new WebSocket("ws://localhost:8080/ws");
209+
// Dynamically determine the WebSocket URL based on the current host
210+
const wsProtocol = window.location.protocol === "https:" ? "wss:" : "ws:";
211+
const wsHost = window.location.host;
212+
const socket = new WebSocket(wsProtocol + "//" + wsHost + "/ws");
213+
203214
const tbody = document.querySelector("#results");
204215
const progress = document.getElementById("progress");
205216
const startForm = document.getElementById("startForm");

0 commit comments

Comments
 (0)