netbird/relay/cmd/main.go

61 lines
1.0 KiB
Go
Raw Normal View History

2024-05-17 20:24:06 +02:00
package main
import (
"os"
2024-05-27 09:42:27 +02:00
"os/signal"
"syscall"
2024-05-23 13:24:02 +02:00
2024-05-17 20:24:06 +02:00
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
2024-05-17 20:24:06 +02:00
"github.com/netbirdio/netbird/relay/server"
2024-05-27 09:42:27 +02:00
"github.com/netbirdio/netbird/util"
2024-05-17 20:24:06 +02:00
)
var (
listenAddress string
rootCmd = &cobra.Command{
Use: "relay",
Short: "Relay service",
Long: "Relay service for Netbird agents",
Run: execute,
}
)
2024-05-17 20:24:06 +02:00
func init() {
_ = util.InitLog("trace", "console")
rootCmd.PersistentFlags().StringVarP(&listenAddress, "listen-address", "l", ":1235", "listen address")
2024-05-17 20:24:06 +02:00
}
2024-05-27 09:42:27 +02:00
func waitForExitSignal() {
osSigs := make(chan os.Signal, 1)
signal.Notify(osSigs, syscall.SIGINT, syscall.SIGTERM)
_ = <-osSigs
}
2024-05-17 20:24:06 +02:00
func execute(cmd *cobra.Command, args []string) {
2024-05-17 20:24:06 +02:00
srv := server.NewServer()
err := srv.Listen(listenAddress)
2024-05-17 20:24:06 +02:00
if err != nil {
log.Errorf("failed to bind server: %s", err)
os.Exit(1)
}
2024-05-26 22:14:33 +02:00
2024-05-27 09:42:27 +02:00
waitForExitSignal()
err = srv.Close()
if err != nil {
log.Errorf("failed to close server: %s", err)
os.Exit(1)
}
2024-05-17 20:24:06 +02:00
}
func main() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}