nws/cmd/exit/exit.go

55 lines
1.2 KiB
Go
Raw Normal View History

2024-07-22 21:00:21 +00:00
package main
import (
"github.com/asmogo/nws/config"
"github.com/asmogo/nws/exit"
"github.com/spf13/cobra"
2024-07-22 21:00:21 +00:00
)
const (
usagePort = "set the https reverse proxy port"
usageTarget = "set https reverse proxy target (your local service)"
)
2024-07-22 21:00:21 +00:00
func main() {
2024-07-28 17:56:42 +00:00
var httpsPort int32
var httpTarget string
rootCmd := &cobra.Command{Use: "exit", Run: startExitNode}
2024-07-23 19:49:15 +00:00
rootCmd.Flags().Int32VarP(&httpsPort, "port", "p", 0, usagePort)
rootCmd.Flags().StringVarP(&httpTarget, "target", "t", "", usageTarget)
err := rootCmd.Execute()
if err != nil {
panic(err)
}
}
// updateConfigFlag updates the configuration with the provided flags.
2024-07-28 17:56:42 +00:00
func updateConfigFlag(cmd *cobra.Command, cfg *config.ExitConfig) error {
httpsPort, err := cmd.Flags().GetInt32("port")
if err != nil {
return err
}
httpTarget, err := cmd.Flags().GetString("target")
if err != nil {
return err
}
cfg.HttpsPort = httpsPort
cfg.HttpsTarget = httpTarget
2024-07-28 17:56:42 +00:00
return nil
}
func startExitNode(cmd *cobra.Command, args []string) {
2024-07-22 21:00:21 +00:00
// load the configuration
// from the environment
cfg, err := config.LoadConfig[config.ExitConfig]()
if err != nil {
panic(err)
}
2024-07-28 17:56:42 +00:00
updateConfigFlag(cmd, cfg)
ctx := cmd.Context()
2024-07-22 21:00:21 +00:00
exitNode := exit.NewExit(ctx, cfg)
exitNode.ListenAndServe(ctx)
}