Skip to content

Commit 6eaa4d2

Browse files
committed
Refactor default listen port
If both tls-cert-file and tls-key-file are set, assume the intent to use TLS. If TLS is enabled, default to port 8443. Otherwise, 8080.
1 parent d7e008a commit 6eaa4d2

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

llama-swap.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var (
2828
func main() {
2929
// Define a command-line flag for the port
3030
configPath := flag.String("config", "config.yaml", "config file name")
31-
listenStr := flag.String("listen", ":8080", "listen ip/port")
31+
listenStr := flag.String("listen", "", "listen ip/port")
3232
certFile := flag.String("tls-cert-file", "", "TLS certificate file")
3333
keyFile := flag.String("tls-key-file", "", "TLS key file")
3434
showVersion := flag.Bool("version", false, "show version of build")
@@ -41,12 +41,6 @@ func main() {
4141
os.Exit(0)
4242
}
4343

44-
if (*certFile != "" && *keyFile == "") ||
45-
(*certFile == "" && *keyFile != "") {
46-
fmt.Println("Error: Both --tls-cert-file and --tls-key-file must be provided for TLS.")
47-
os.Exit(1)
48-
}
49-
5044
conf, err := config.LoadConfig(*configPath)
5145
if err != nil {
5246
fmt.Printf("Error loading config: %v\n", err)
@@ -63,6 +57,24 @@ func main() {
6357
gin.SetMode(gin.ReleaseMode)
6458
}
6559

60+
// Validate TLS flags.
61+
var useTLS bool
62+
if (*certFile != "" && *keyFile == "") ||
63+
(*certFile == "" && *keyFile != "") {
64+
fmt.Println("Error: Both --tls-cert-file and --tls-key-file must be provided for TLS.")
65+
os.Exit(1)
66+
} else {
67+
useTLS = true
68+
}
69+
// Set default ports.
70+
if *listenStr == "" {
71+
defaultPort := ":8080"
72+
if useTLS {
73+
defaultPort = ":8443"
74+
}
75+
listenStr = &defaultPort
76+
}
77+
6678
// Setup channels for server management
6779
exitChan := make(chan struct{})
6880
sigChan := make(chan os.Signal, 1)
@@ -177,7 +189,7 @@ func main() {
177189
// Start server
178190
go func() {
179191
var err error
180-
if *certFile != "" && *keyFile != "" {
192+
if useTLS {
181193
fmt.Printf("llama-swap listening with TLS on https://%s\n", *listenStr)
182194
err = srv.ListenAndServeTLS(*certFile, *keyFile)
183195
} else {

0 commit comments

Comments
 (0)