diff --git a/db/config.go b/db/config.go index d40e650..ce15f06 100644 --- a/db/config.go +++ b/db/config.go @@ -19,6 +19,10 @@ type Config struct { // MaxMemory is the maximum memory limit in bytes. 0 means unlimited. MaxMemory int64 + + // Port is the TCP port the server is listening on. Set once at startup from + // the --port flag; read-only at runtime (changing it would require a rebind). + Port int } // ServerConfig is the package-level config instance used across the server. @@ -26,6 +30,7 @@ var ServerConfig = &Config{ Hz: 1, ActiveExpireEnabled: true, MaxMemory: 0, + Port: 6378, } // CleanupInterval returns the active expiry ticker interval derived from Hz. @@ -50,6 +55,8 @@ func (c *Config) Get(param string) (string, bool) { return boolToYesNo(c.ActiveExpireEnabled), true case "maxmemory": return itoa64(c.MaxMemory), true + case "port": + return itoa(c.Port), true } return "", false } diff --git a/docs/config.md b/docs/config.md index 65b6c8d..305a348 100644 --- a/docs/config.md +++ b/docs/config.md @@ -29,11 +29,12 @@ CONFIG SET parameter value ## Supported parameters -| Parameter | Type | Default | Description | -|-------------------------|---------|---------|----------------------------------------------------------| -| `hz` | integer | `1` | Number of active expiry cycles per second. Must be > 0. | -| `active-expire-enabled` | yes/no | `yes` | Enable or disable the active expiry background job. | -| `maxmemory` | integer | `0` | Max memory in bytes. `0` means unlimited. | +| Parameter | Type | Default | Writable | Description | +|-------------------------|---------|---------|----------|----------------------------------------------------------| +| `hz` | integer | `1` | yes | Number of active expiry cycles per second. Must be > 0. | +| `active-expire-enabled` | yes/no | `yes` | yes | Enable or disable the active expiry background job. | +| `maxmemory` | integer | `0` | yes | Max memory in bytes. `0` means unlimited. | +| `port` | integer | `6378` | no | TCP port the server is listening on. Set via `--port` flag at startup. | ## Return value @@ -74,4 +75,8 @@ maxmemory > CONFIG SET maxmemory 1073741824 OK + +> CONFIG GET port +port +6378 ``` diff --git a/main.go b/main.go index 6c38bc3..0241517 100644 --- a/main.go +++ b/main.go @@ -3,15 +3,23 @@ package main import ( "HelixDB/db" "HelixDB/resp" + "flag" "fmt" "net" "os" ) func main() { - l, err := net.Listen("tcp", "0.0.0.0:6378") + port := flag.Int("port", 6378, "Port to listen on") + flag.IntVar(port, "p", *port, "Port to listen on (shorthand)") + flag.Parse() + + db.ServerConfig.Port = *port + + addr := fmt.Sprintf("0.0.0.0:%d", *port) + l, err := net.Listen("tcp", addr) if err != nil { - fmt.Println("Failed to bind to port 6378") + fmt.Printf("Failed to bind to port %d\n", *port) os.Exit(1) } defer l.Close()