Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for configurable binding address #39

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this changed? It should default to badger

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was already defaulting to LMDB when no env parameter was set. Look at line 57 above. I assumed you did this intentionally - wanting new installs to default to Badger, but not to touch old installs already using LMDB without specifying the env variable. If you'd prefer to switch even old installs to use Bager by default, feel free to change the string, or let me know and I can make the change myself.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes you're right lol, thanks for knowing my relay better than me. Too many relays! Thanks for the pr

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good change


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

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