wisski-cloud-distillery/internal/config/ports.go
2023-03-08 11:27:19 +01:00

33 lines
1 KiB
Go

package config
import (
"fmt"
"golang.org/x/exp/slices"
)
type ListenConfig struct {
// Ports are the public addresses to bind to.
// Each address is automatically multiplexed to serve http, https and ssh traffic.
// This should typically be port 80 and port 443.
Ports []uint16 `yaml:"ports" default:"80" validate:"ports"`
// AdvertisedSSHPort is the port that shows up as the ssh port in various places in the interface.
// It is automaticalled added to the ports to listen to.
AdvertisedSSHPort uint16 `yaml:"advertise_ssh" default:"80" validate:"port"`
}
// ComposePorts returns a list of ports to be used within a docker-compose.yml file.
// These can be used to forward all ports to the internal port.
func (lc ListenConfig) ComposePorts(internal string) []string {
// sort and uniquify ports
ports := append([]uint16{lc.AdvertisedSSHPort}, lc.Ports...)
slices.Sort(ports)
ports = slices.Compact(ports)
forwards := make([]string, len(ports))
for i, port := range ports {
forwards[i] = fmt.Sprintf("%d:%s", port, internal)
}
return forwards
}