diff --git a/README.md b/README.md index 40ef0d0..5ee4da9 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ services: image: ghcr.io/daltonbr/wol-server:latest network_mode: "host" # Required for WOL broadcast on Linux environment: + PORT: "5000" # Optional, defaults to 5000 WOL_CONFIG: | devices: desktop: @@ -76,6 +77,13 @@ curl http://localhost:5000/status ## Configuration +### Environment Variables + +| Variable | Description | Default | +| -------- | ----------- | ------- | +| PORT | HTTP server port | 5000 | +| WOL_CONFIG | YAML configuration (inline) | - | + ### Option 1: Environment Variable (Docker) Set `WOL_CONFIG` with YAML content (see docker-compose example above). diff --git a/docs/DEVELOPMENT.md b/docs/DEVELOPMENT.md index f493ae8..73462c7 100644 --- a/docs/DEVELOPMENT.md +++ b/docs/DEVELOPMENT.md @@ -23,7 +23,13 @@ cp config.example.yaml ~/.config/wol-server/config.yaml go run . ``` -**Server runs on:** `http://localhost:5000` +**Server runs on:** `http://localhost:5000` (or set `PORT` environment variable) + +**Change port:** + +```bash +PORT=8080 go run . +``` ## Testing @@ -89,9 +95,16 @@ docker build -t wol-server:local . **Run locally built image:** ```bash +# Default port (5000) docker run --rm -p 5000:5000 \ -e WOL_CONFIG="$(cat ~/.config/wol-server/config.yaml)" \ wol-server:local + +# Custom port +docker run --rm -p 8080:8080 \ + -e PORT=8080 \ + -e WOL_CONFIG="$(cat ~/.config/wol-server/config.yaml)" \ + wol-server:local ``` ## Publishing to GHCR diff --git a/main.go b/main.go index 97d8a09..cebb187 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "log" "net" "net/http" + "os" "strconv" ) @@ -15,10 +16,25 @@ func main() { log.Println("Server will only accept direct parameters (mac/ip/port)") } + // Get port from environment or use default + portStr := os.Getenv("PORT") + if portStr == "" { + portStr = "5000" + } + + // Validate port + port, err := strconv.Atoi(portStr) + if err != nil { + log.Fatalf("Invalid PORT environment variable '%s': must be a number", portStr) + } + if port < 1 || port > 65535 { + log.Fatalf("Invalid PORT value %d: must be between 1 and 65535", port) + } + http.HandleFunc("/wake", handler) http.HandleFunc("/status", statusHandler) - fmt.Println("Server is listening on port :5000") - log.Fatal(http.ListenAndServe(":5000", nil)) + fmt.Printf("Server is listening on port :%d\n", port) + log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil)) } func handler(w http.ResponseWriter, r *http.Request) {