From 6633087578a0c9d2cd5cd2a96f041938429e388e Mon Sep 17 00:00:00 2001 From: Laura Date: Fri, 20 Jun 2025 23:47:22 +0200 Subject: [PATCH] allow custom port --- client/main.go | 2 +- server/main.go | 39 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/client/main.go b/client/main.go index 7b93432..190ac8b 100644 --- a/client/main.go +++ b/client/main.go @@ -25,7 +25,7 @@ var ( func main() { app := &cli.Command{ Name: "up", - Usage: "UP client", + Usage: "up client", Version: Version, ArgsUsage: " ", UsageText: "up [options] ", diff --git a/server/main.go b/server/main.go index 1bfc1d0..53b0e0f 100644 --- a/server/main.go +++ b/server/main.go @@ -1,14 +1,18 @@ package main import ( + "context" "crypto/tls" + "fmt" "net/http" + "os" "time" "github.com/coalaura/logger" "github.com/coalaura/up/internal" "github.com/go-chi/chi/v5" "github.com/patrickmn/go-cache" + "github.com/urfave/cli/v3" ) const ( @@ -45,8 +49,36 @@ func init() { } func main() { + app := &cli.Command{ + Name: "up", + Usage: "up server", + Version: Version, + Flags: []cli.Flag{ + &cli.UintFlag{ + Name: "port", + Aliases: []string{"p"}, + Usage: "custom port", + Value: 7966, + }, + }, + Action: run, + EnableShellCompletion: true, + UseShortOptionHandling: true, + Suggest: true, + } + + err := app.Run(context.Background(), os.Args) + log.MustPanic(err) +} + +func run(_ context.Context, cmd *cli.Command) error { log.Printf("up server %s\n", Version) + port := cmd.Uint("port") + if port <= 0 || port > 65534 { + port = 7966 + } + authorized, err := LoadAuthorizedKeys() log.MustPanic(err) @@ -66,13 +98,14 @@ func main() { r.Post("/receive", HandleReceiveRequest) srv := &http.Server{ - Addr: ":7966", + Addr: fmt.Sprintf(":%d", port), Handler: r, TLSConfig: &tls.Config{ MinVersion: tls.VersionTLS12, }, } - log.Println("Server listening on :7966") - srv.ListenAndServeTLS("cert.pem", "key.pem") + log.Printf("Server listening on :%d\n", port) + + return srv.ListenAndServeTLS("cert.pem", "key.pem") }