|
5 | 5 | "log"
|
6 | 6 | "net/http"
|
7 | 7 | "net/url"
|
| 8 | + "os" |
8 | 9 | "sync"
|
9 | 10 | "time"
|
10 | 11 |
|
@@ -45,14 +46,20 @@ var (
|
45 | 46 | func main() {
|
46 | 47 | log.Println("Starting Concurrent Web Crawler with Frontend")
|
47 | 48 |
|
| 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 | + |
48 | 55 | // Start HTTP server
|
49 | 56 | go func() {
|
50 | 57 | http.HandleFunc("/", serveFrontend)
|
51 | 58 | http.HandleFunc("/ws", handleWebSocket)
|
52 | 59 | http.HandleFunc("/stop", handleStop)
|
53 | 60 | 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)) |
56 | 63 | }()
|
57 | 64 |
|
58 | 65 | // Keep main thread alive
|
@@ -199,7 +206,11 @@ func serveFrontend(w http.ResponseWriter, r *http.Request) {
|
199 | 206 | </div>
|
200 | 207 | </div>
|
201 | 208 | <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 | +
|
203 | 214 | const tbody = document.querySelector("#results");
|
204 | 215 | const progress = document.getElementById("progress");
|
205 | 216 | const startForm = document.getElementById("startForm");
|
|
0 commit comments