Skip to content

Commit

Permalink
you can set up service IP and Port for K8S runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
powerkimhub committed Jul 16, 2021
1 parent 84d7e3e commit 6353a4b
Showing 1 changed file with 48 additions and 16 deletions.
64 changes: 48 additions & 16 deletions api-runtime/rest-runtime/CBSpiderRuntime.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"crypto/subtle"
"fmt"
"time"
"strings"

"net/http"
"os"
Expand Down Expand Up @@ -57,7 +58,7 @@ func init() {
cr.MiddleStartTime = currentTime.Format("2006.01.02.15:04:05")
cr.ShortStartTime = fmt.Sprintf("T%02d:%02d:%02d", currentTime.Hour(), currentTime.Minute(), currentTime.Second())
cr.HostIPorName = getHostIPorName()
cr.ServicePort = ":1024"
cr.ServicePort = getServicePort()
}

// REST API Return struct for boolean type
Expand All @@ -80,28 +81,59 @@ type SimpleMsg struct {
Message string `json:"message" example:"Any message"`
}

//====== temporary trick for shared public IP Host or VirtualBox VM, etc.
//====== user can setup spider server's IP manually.

// unset # default: like 'curl ifconfig.co':1024
// LOCALHOST="OFF" # default: like 'curl ifconfig.co':1024
// LOCALHOST="ON" # => localhost:1024
// LOCALHOST="1.2.3.4" # => 1.2.3.4:1024
// LOCALHOST="1.2.3.4:31024" # => 1.2.3.4:31024
func getHostIPorName() string {
if os.Getenv("LOCALHOST") == "ON" {

hostEnv := os.Getenv("LOCALHOST")

if hostEnv == "ON" {
return "localhost"
}

// temporary trick for shared public IP Host or VirtualBox VM, etc.
// user can setup spider server's IP manually.
if os.Getenv("LOCALHOST") != "OFF" {
return os.Getenv("LOCALHOST")
}
if hostEnv == "" || hostEnv=="OFF" {
return getPublicIP()
}

ip, err := pubip.Get()
if err != nil {
cblog.Error(err)
hostName, err := os.Hostname()
if err != nil {
cblog.Error(err)
}
return hostName
strs := strings.Split(hostEnv, ":")
return strs[0]
}

func getPublicIP() string {
ip, err := pubip.Get()
if err != nil {
cblog.Error(err)
hostName, err := os.Hostname()
if err != nil {
cblog.Error(err)
}
return hostName
}

return ip.String()
}

func getServicePort() string {
servicePort := ":1024"

hostEnv := os.Getenv("LOCALHOST")
if hostEnv == "" || hostEnv=="OFF" || hostEnv=="ON" {
return servicePort
}

strs := strings.Split(hostEnv, ":")
if len(strs) < 2 {
return servicePort
}
servicePort = ":" + strs[1]

return ip.String()
return servicePort
}

func RunServer() {
Expand Down

0 comments on commit 6353a4b

Please sign in to comment.