Skip to content

Commit

Permalink
dynamic the port, so that you could open multiple software to serve d…
Browse files Browse the repository at this point in the history
…ifferent folders
  • Loading branch information
yingshaoxo committed Jun 28, 2021
1 parent ea9da2e commit 3039c43
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 53 deletions.
1 change: 1 addition & 0 deletions Tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ build() {
#go get github.com/gobuffalo/packr/v2/...
go get github.com/GeertJohan/go.rice
go get github.com/GeertJohan/go.rice/rice
go get github.com/skip2/go-qrcode/...

cd ..
cd src
Expand Down
3 changes: 3 additions & 0 deletions client/src/Const.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
var expected_host = ""
/*
if (window.location.port != '5000') {
expected_host = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + '5000': '') + "/"
} else {
expected_host = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: '') + "/"
}
*/
expected_host = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: '') + "/"

export const HOST = expected_host
2 changes: 2 additions & 0 deletions src/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN
github.com/nkovacs/streamquote v0.0.0-20170412213628-49af9bddb229 h1:E2B8qYyeSgv5MXpmzZXRNp8IAQ4vjxIjhpAf5hv/tAg=
github.com/nkovacs/streamquote v0.0.0-20170412213628-49af9bddb229/go.mod h1:0aYXnNPJ8l7uZxf45rWW1a/uME32OF0rhiYGNQ2oF2E=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand Down
41 changes: 30 additions & 11 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"time"

Expand All @@ -32,6 +33,21 @@ func openBrowser(url string) {
}
}

func findAvailablePort(port int) string {
for true {
postStr := strconv.Itoa(port)
connection, err := net.Listen("tcp", ":"+postStr)
if err != nil {
fmt.Fprintf(os.Stderr, "Can't listen on port %q: %s", port, err)
port += 1
} else {
connection.Close()
break
}
}
return strconv.Itoa(port)
}

func main() {
// get cli args
dir, _ := os.Getwd()
Expand Down Expand Up @@ -82,6 +98,7 @@ func main() {
c.JSON(http.StatusOK, file_dict)
runtime.GC()
})

}

local_address := "127.0.0.1"
Expand All @@ -99,7 +116,9 @@ func main() {
}
}

var target_url = "http://" + local_address + ":5000/ui"
port := findAvailablePort(5000)

var target_url = "http://" + local_address + ":" + port + "/ui"
go func() {
fmt.Printf("\n\n-------------------------------\n\n")

Expand All @@ -117,17 +136,17 @@ func main() {
}()

// Start and run the server
router.Run(":5000")
router.Run(":" + port)

/*
s := &http.Server{
Addr: ":5000",
Handler: router,
ReadTimeout: 1 * time.Second,
WriteTimeout: 1 * time.Second,
IdleTimeout: 1 * time.Second,
MaxHeaderBytes: 1 << 20,
}
s.ListenAndServe()
s := &http.Server{
Addr: ":5000",
Handler: router,
ReadTimeout: 1 * time.Second,
WriteTimeout: 1 * time.Second,
IdleTimeout: 1 * time.Second,
MaxHeaderBytes: 1 << 20,
}
s.ListenAndServe()
*/
}
84 changes: 42 additions & 42 deletions src/rice-box.go

Large diffs are not rendered by default.

0 comments on commit 3039c43

Please sign in to comment.