2828func 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