nip/cmd/main.go

72 lines
1.6 KiB
Go

package main
import (
"fmt"
"log"
"os"
"git.lovezsh.com/lovezsh/nip/internal/agent"
"git.lovezsh.com/lovezsh/nip/internal/server"
"github.com/urfave/cli/v2"
)
func main() {
app := &cli.App{
Commands: []*cli.Command{
{
Name: "server",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "addr",
Usage: "listen address",
Value: ":3000",
EnvVars: []string{"NIP_SERVER_ADDR"},
},
&cli.StringFlag{
Name: "token",
Required: true,
EnvVars: []string{"NIP_SERVER_TOKEN"},
},
},
Action: func(ctx *cli.Context) error {
s := server.New(ctx.String("token"), ctx.String("addr"))
log.Printf("server listen at: %s", ctx.String("addr"))
return s.ListenAndServe()
},
},
{
Name: "agent",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "addr",
Usage: "listen address",
Value: ":4000",
EnvVars: []string{"NIP_AGENT_ADDR"},
},
&cli.StringFlag{
Name: "token",
Required: true,
EnvVars: []string{"NIP_AGENT_TOKEN"},
},
&cli.StringFlag{
Name: "server",
Value: "server address",
Required: true,
EnvVars: []string{"NIP_AGENT_SERVER_ADDR"},
},
},
Action: func(ctx *cli.Context) error {
a := agent.New(ctx.String("addr"), ctx.String("token"), ctx.String("server"))
log.Printf("agent listen at %s", ctx.String("addr"))
log.Printf("using proxy server %s", ctx.String("server"))
return a.ListenAndServe()
},
},
},
}
if err := app.Run(os.Args); err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err)
os.Exit(1)
}
}