Skip to content

Read bind address from env #681

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
39 changes: 33 additions & 6 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ package main
import (
"encoding/json"
"flag"
"io/ioutil"
"net/http"
"os"
"plugin"
"strconv"
"strings"

"github.com/bbernhard/signal-cli-rest-api/api"
"github.com/bbernhard/signal-cli-rest-api/client"
docs "github.com/bbernhard/signal-cli-rest-api/docs"
Expand All @@ -12,11 +19,6 @@ import (
log "github.com/sirupsen/logrus"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"io/ioutil"
"net/http"
"os"
"strconv"
"plugin"
)

// @title Signal Cli REST API
Expand Down Expand Up @@ -81,6 +83,18 @@ func main() {
}))

router.Use(gin.Recovery())
trustedProxies := utils.GetEnv("TRUSTED_PROXIES", "")
log.Info("Debug: TRUSTED_PROXIES env variable = ", trustedProxies)
if trustedProxies != "" {
proxiesArr := strings.Split(trustedProxies, ",")
for i, p := range proxiesArr {
proxiesArr[i] = strings.TrimSpace(p)
}
err := router.SetTrustedProxies(proxiesArr)
if err != nil {
log.Fatal("Couldn't set trusted proxies: ", err.Error())
}
}

port := utils.GetEnv("PORT", "8080")
if _, err := strconv.Atoi(port); err != nil {
Expand Down Expand Up @@ -422,5 +436,18 @@ func main() {
c.Start()
}

router.Run()
router.Run(getBindAddress(port))
}

func getBindAddress(port string) string {
bindIP := utils.GetEnv("BIND_IP", "")
log.Info("Debug: BIND_IP env variable = ", bindIP)
var addr string
if bindIP == "" {
addr = ":" + port // Listen to all incoming traffic
} else {
addr = bindIP + ":" + port // Bind to the specified IP
}
log.Info("Final bind address: ", addr)
return addr
}
Loading