Skip to content

Commit da780f2

Browse files
committed
git-bundle-web-server: add '--port' option
Introduce a '--port' string option to allow users to specify a custom port on which 'git-bundle-web-server' should be hosted. The default value is '8080' (the current hardcoded value), and there is additional validation to make sure it is a 16-bit unsigned int. Signed-off-by: Victoria Dye <[email protected]>
1 parent 7a24556 commit da780f2

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

cmd/git-bundle-web-server/main.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import (
77
"net/http"
88
"os"
99
"os/signal"
10+
"strconv"
1011
"strings"
1112
"sync"
1213
"syscall"
1314

15+
"github.com/github/git-bundle-server/internal/argparse"
1416
"github.com/github/git-bundle-server/internal/core"
1517
)
1618

@@ -90,12 +92,22 @@ func startServer(server *http.Server, serverWaitGroup *sync.WaitGroup) {
9092
}
9193

9294
func main() {
95+
parser := argparse.NewArgParser("git-bundle-web-server [--port <port>]")
96+
port := parser.String("port", "8080", "The port on which the server should be hosted")
97+
parser.Parse(os.Args[1:])
98+
99+
// Additional option validation
100+
p, err := strconv.Atoi(*port)
101+
if err != nil || p < 0 || p > 65535 {
102+
parser.Usage("Invalid port '%s'.", *port)
103+
}
104+
93105
// Configure the server
94106
mux := http.NewServeMux()
95107
mux.HandleFunc("/", serve)
96108
server := &http.Server{
97109
Handler: mux,
98-
Addr: ":8080",
110+
Addr: ":" + *port,
99111
}
100112
serverWaitGroup := &sync.WaitGroup{}
101113

0 commit comments

Comments
 (0)