Binding interface IP address for REST Server 29/6729/2
authorwahidw <abdulwahid.w@nokia.com>
Wed, 22 Sep 2021 19:26:47 +0000 (19:26 +0000)
committerwahidw <abdulwahid.w@nokia.com>
Thu, 23 Sep 2021 05:11:17 +0000 (05:11 +0000)
Signed-off-by: wahidw <abdulwahid.w@nokia.com>
Change-Id: Ib4fa4aac2030f6b06c25b49292a715ba2f15a50c

pkg/xapp/xapp.go

index ad62e36..0e85845 100755 (executable)
@@ -23,6 +23,7 @@ import (
        "bytes"
        "encoding/json"
        "fmt"
+       "net"
        "net/http"
        "os"
        "os/signal"
@@ -269,12 +270,40 @@ func init() {
        InstallSignalHandler()
 }
 
+func getIpAdress() string {
+       var ip net.IP
+       itf, err := net.InterfaceByName(os.Getenv("INTERFACE_NAME"))
+       if err != nil {
+               Logger.Info("Interface name is not able to resolve " + err.Error())
+               return ip.String()
+       }
+       item, err := itf.Addrs()
+       if err != nil {
+               Logger.Info("IP address is not able to resolve " + err.Error())
+               return ip.String()
+       }
+       for _, addr := range item {
+               switch v := addr.(type) {
+               case *net.IPNet:
+                       if !v.IP.IsLinkLocalUnicast() {
+                               ip = v.IP
+                       }
+               }
+       }
+       return ip.String()
+}
+
 func RunWithParams(c MessageConsumer, sdlcheck bool) {
        Rmr = NewRMRClient()
 
        Rmr.SetReadyCB(XappReadyCb, nil)
-
-       host := fmt.Sprintf(":%d", GetPortData("http").Port)
+       ipString := getIpAdress()
+       var host string
+       if ipString == "<nil>" {
+               host = fmt.Sprintf(":%d", GetPortData("http").Port)
+       } else {
+               host = fmt.Sprintf("%s:%d", ipString, GetPortData("http").Port)
+       }
        go http.ListenAndServe(host, Resource.router)
        Logger.Info(fmt.Sprintf("Xapp started, listening on: %s", host))