Skip to content

Commit

Permalink
switch to listen-address
Browse files Browse the repository at this point in the history
  • Loading branch information
gi8lino committed Aug 9, 2023
1 parent 0b28ccf commit 1481db4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ The primary function is to utilize the `serve` command to initiate a web server
```

Flags:
- `-H, --hostname`: Specify the hostname to listen on (Default: `localhost`).
- `-p, --port`: Specify the port to listen on (Default: `8080`).
- `--listen-address`: The address to listen on for HTTP requests. (Default: `:8080`).

Examples:

Expand Down
12 changes: 9 additions & 3 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"github.com/spf13/viper"
)

var listenAddress string

// serveCmd represents the serve command
var serveCmd = &cobra.Command{
Use: "serve",
Expand Down Expand Up @@ -77,13 +79,17 @@ Endpoints:
log.Fatalf("Unable to redact config: %s", err)
}

server.RunServer(config.App.Server.Hostname, config.App.Server.Port)
hostname, port, err := config.ExtractHostAndPort(listenAddress)
if err != nil {
log.Fatalf("Unable to extract hostname and port: %s", err)
}

server.RunServer(hostname, port)
},
}

func init() {
rootCmd.AddCommand(serveCmd)

serveCmd.Flags().StringVarP(&config.App.Server.Hostname, "hostname", "H", "localhost", "Hostname to listen on")
serveCmd.Flags().IntVarP(&config.App.Server.Port, "port", "p", 8080, "Port to listen on")
serveCmd.Flags().StringVar(&listenAddress, "listen-address", ":8080", "The address to listen on for HTTP requests.")
}
1 change: 1 addition & 0 deletions deploy/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ spec:
args:
- serve
- --config=/config/config.yaml
- --listen-address=0.0.0.0:8080
envFrom:
- secretRef:
name: certalert-password-envs
Expand Down
21 changes: 20 additions & 1 deletion internal/config/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package config

import "certalert/internal/certificates"
import (
"certalert/internal/certificates"
"net"
"strconv"
)

// DeepCopy returns a deep copy of the config
func (c Config) DeepCopy() Config {
Expand Down Expand Up @@ -42,3 +46,18 @@ func (c Config) DeepCopy() Config {

return newConfig
}

// ExtractHostAndPort extracts the hostname and port from the listen address
func ExtractHostAndPort(address string) (string, int, error) {
host, portStr, err := net.SplitHostPort(address)
if err != nil {
return "", 0, err
}

port, err := strconv.Atoi(portStr)
if err != nil {
return "", 0, err
}

return host, port, nil
}

0 comments on commit 1481db4

Please sign in to comment.