Skip to content

Commit

Permalink
Add support for running BemiDB with IPv6
Browse files Browse the repository at this point in the history
  • Loading branch information
exAspArk committed Nov 30, 2024
1 parent 3f4eecf commit be711d6
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 3 deletions.
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ BEMIDB_PORT=54321
BEMIDB_DATABASE=bemidb
BEMIDB_USER=
BEMIDB_PASSWORD=
BEMIDB_HOST=127.0.0.1
BEMIDB_INIT_SQL=./init.sql
BEMIDB_LOG_LEVEL=INFO

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ psql postgres://localhost:54321/bemidb -c \

| CLI argument | Environment variable | Default value | Description |
|---------------------------|-------------------------|----------------|---------------------------------------------------------------------------|
| `--host` | `BEMIDB_HOST` | `127.0.0.1` | Host for BemiDB to listen on |
| `--port` | `BEMIDB_PORT` | `54321` | Port for BemiDB to listen on |
| `--database` | `BEMIDB_DATABASE` | `bemidb` | Database name |
| `--storage-type` | `BEMIDB_STORAGE_TYPE` | `LOCAL` | Storage type: `LOCAL` or `S3` |
Expand Down
2 changes: 1 addition & 1 deletion scripts/install.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

VERSION="0.15.1"
VERSION="0.16.0"

# Detect OS and architecture
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
Expand Down
7 changes: 7 additions & 0 deletions src/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
ENV_DATABASE = "BEMIDB_DATABASE"
ENV_USER = "BEMIDB_USER"
ENV_PASSWORD = "BEMIDB_PASSWORD"
ENV_HOST = "BEMIDB_HOST"
ENV_INIT_SQL_FILEPATH = "BEMIDB_INIT_SQL"
ENV_STORAGE_PATH = "BEMIDB_STORAGE_PATH"
ENV_LOG_LEVEL = "BEMIDB_LOG_LEVEL"
Expand All @@ -32,6 +33,7 @@ const (
DEFAULT_DATABASE = "bemidb"
DEFAULT_USER = ""
DEFAULT_PASSWORD = ""
DEFAULT_HOST = "127.0.0.1"
DEFAULT_INIT_SQL_FILEPATH = "./init.sql"
DEFAULT_STORAGE_PATH = "iceberg"
DEFAULT_LOG_LEVEL = "INFO"
Expand All @@ -57,6 +59,7 @@ type PgConfig struct {
}

type Config struct {
Host string
Port string
Database string
User string
Expand All @@ -78,6 +81,7 @@ func init() {
}

func registerFlags() {
flag.StringVar(&_config.Host, "host", os.Getenv(ENV_HOST), "Database host. Default: \""+DEFAULT_HOST+"\"")
flag.StringVar(&_config.Port, "port", os.Getenv(ENV_PORT), "Port for BemiDB to listen on. Default: \""+DEFAULT_PORT+"\"")
flag.StringVar(&_config.Database, "database", os.Getenv(ENV_DATABASE), "Database name. Default: \""+DEFAULT_DATABASE+"\"")
flag.StringVar(&_config.User, "user", os.Getenv(ENV_USER), "Database user. Default: \""+DEFAULT_USER+"\"")
Expand All @@ -100,6 +104,9 @@ func registerFlags() {
func parseFlags() {
flag.Parse()

if _config.Host == "" {
_config.Host = DEFAULT_HOST
}
if _config.Port == "" {
_config.Port = DEFAULT_PORT
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"
)

const VERSION = "0.15.1"
const VERSION = "0.16.0"

func main() {
config := LoadConfig()
Expand Down
16 changes: 15 additions & 1 deletion src/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,21 @@ func NewPostgres(config *Config, conn *net.Conn) *Postgres {
}

func NewTcpListener(config *Config) net.Listener {
tcpListener, err := net.Listen("tcp", "127.0.0.1:"+config.Port)
parsedIp := net.ParseIP(config.Host)
if parsedIp == nil {
panic("Invalid host: " + config.Host)
}

var network, host string
if parsedIp.To4() == nil {
network = "tcp6"
host = "[" + config.Host + "]"
} else {
network = "tcp4"
host = config.Host
}

tcpListener, err := net.Listen(network, host+":"+config.Port)
PanicIfError(err)
return tcpListener
}
Expand Down

0 comments on commit be711d6

Please sign in to comment.