Skip to content

Commit

Permalink
Merge pull request #39 from aaccioly-open-source/feature/configurable…
Browse files Browse the repository at this point in the history
…-binding-address

Add support for configurable binding address
  • Loading branch information
barrydeen authored Oct 19, 2024
2 parents f56b389 + e179ded commit 7ba4dc1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
OWNER_NPUB="npub1utx00neqgqln72j22kej3ux7803c2k986henvvha4thuwfkper4s7r50e8"
RELAY_URL="relay.utxo.one"
RELAY_PORT=3355
RELAY_BIND_ADDRESS="0.0.0.0" # Can be set to a specific IP4 or IP6 address ("" for all interfaces)
DB_ENGINE="badger" # badger, lmdb (lmdb works best with an nvme, otherwise you might have stability issues)

## Private Relay Settings
Expand Down
18 changes: 13 additions & 5 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type Config struct {
OwnerNpub string `json:"owner_npub"`
DBEngine string `json:"db_engine"`
RelayURL string `json:"relay_url"`
RelayPort int `json:"relay_port"`
RelayBindAddress string `json:"relay_bind_address"`
RelaySoftware string `json:"relay_software"`
RelayVersion string `json:"relay_version"`
PrivateRelayName string `json:"private_relay_name"`
Expand Down Expand Up @@ -52,15 +54,14 @@ type AwsConfig struct {
}

func loadConfig() Config {
godotenv.Load(".env")
if os.Getenv("DB_ENGINE") == "" {
os.Setenv("DB_ENGINE", "lmdb")
}
_ = godotenv.Load(".env")

return Config{
OwnerNpub: getEnv("OWNER_NPUB"),
DBEngine: getEnv("DB_ENGINE"),
DBEngine: getEnvString("DB_ENGINE", "lmdb"),
RelayURL: getEnv("RELAY_URL"),
RelayPort: getEnvInt("RELAY_PORT", 3355),
RelayBindAddress: getEnvString("RELAY_BIND_ADDRESS", "0.0.0.0"),
RelaySoftware: "https://github.com/bitvora/haven",
RelayVersion: "v0.4.4",
PrivateRelayName: getEnv("PRIVATE_RELAY_NAME"),
Expand Down Expand Up @@ -121,6 +122,13 @@ func getEnv(key string) string {
return value
}

func getEnvString(key string, defaultValue string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return defaultValue
}

func getEnvInt(key string, defaultValue int) int {
if value, ok := os.LookupEnv(key); ok {
intValue, err := strconv.Atoi(value)
Expand Down
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ func main() {

http.HandleFunc("/", dynamicRelayHandler)

log.Printf("🔗 listening at http://localhost:3355")
http.ListenAndServe("0.0.0.0:3355", nil)
addr := fmt.Sprintf("%s:%d", config.RelayBindAddress, config.RelayPort)

log.Printf("🔗 listening at %s", addr)
http.ListenAndServe(addr, nil)
}

func dynamicRelayHandler(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit 7ba4dc1

Please sign in to comment.