diff --git a/Dockerfile b/Dockerfile index f91e885..bbedfe2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,3 +10,4 @@ COPY server deploy/config.toml / EXPOSE 3360 8080 8081 8890 8891 ENTRYPOINT ["/server"] +CMD ["run"] diff --git a/cmd/client/main_test.go b/cmd/client/main_test.go index 71de419..1fd34aa 100644 --- a/cmd/client/main_test.go +++ b/cmd/client/main_test.go @@ -13,14 +13,14 @@ func TestExecution(t *testing.T) { t.Run("success", func(t *testing.T) { exit = func(code int) { assert.Equal(t, 0, code) } stderr, stdout = io.Discard, io.Discard - os.Args = []string{"root", "version"} + os.Args = []string{"version"} main() }) t.Run("failure", func(t *testing.T) { exit = func(code int) { assert.Equal(t, 1, code) } stderr, stdout = io.Discard, io.Discard - os.Args = []string{"root", "unknown"} + os.Args = []string{"unknown"} main() }) diff --git a/cmd/server/main_test.go b/cmd/server/main_test.go index 71de419..1fd34aa 100644 --- a/cmd/server/main_test.go +++ b/cmd/server/main_test.go @@ -13,14 +13,14 @@ func TestExecution(t *testing.T) { t.Run("success", func(t *testing.T) { exit = func(code int) { assert.Equal(t, 0, code) } stderr, stdout = io.Discard, io.Discard - os.Args = []string{"root", "version"} + os.Args = []string{"version"} main() }) t.Run("failure", func(t *testing.T) { exit = func(code int) { assert.Equal(t, 1, code) } stderr, stdout = io.Discard, io.Discard - os.Args = []string{"root", "unknown"} + os.Args = []string{"unknown"} main() }) diff --git a/deploy/Dockerfile b/deploy/Dockerfile index b349f0f..4c0bf87 100644 --- a/deploy/Dockerfile +++ b/deploy/Dockerfile @@ -30,3 +30,4 @@ COPY --from=builder /service/server /service/deploy/config.toml / EXPOSE 3360 8080 8081 8890 8891 ENTRYPOINT ["/server"] +CMD ["run"] diff --git a/internal/command/client.go b/internal/command/client.go index b510fec..76fa6e1 100644 --- a/internal/command/client.go +++ b/internal/command/client.go @@ -37,10 +37,10 @@ func NewClient(cnf *config.Service) *cobra.Command { return v.Unmarshal(cnf) }, } + flags := command.PersistentFlags() flags.StringVarP(&path, "config", "c", "config.toml", "path to config file") flags.StringVar(&cnf.Server.Connect.Address, "host", "", "remote rpc host") - command.AddCommand( Hello(&cnf.Server.Connect), Sign(&cnf.Server.Connect), diff --git a/internal/command/client_test.go b/internal/command/client_test.go index 85a6bd4..122682a 100644 --- a/internal/command/client_test.go +++ b/internal/command/client_test.go @@ -6,11 +6,12 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - . "go.octolab.org/template/service/internal/command" + "go.octolab.org/template/service/internal/command" + "go.octolab.org/template/service/internal/config" ) func TestNewClient(t *testing.T) { - root := NewClient() + root := command.NewClient(new(config.Service)) require.NotNil(t, root) assert.NotEmpty(t, root.Use) assert.NotEmpty(t, root.Short) diff --git a/internal/command/server.go b/internal/command/server.go index c8d1238..f8ad953 100644 --- a/internal/command/server.go +++ b/internal/command/server.go @@ -44,37 +44,51 @@ func NewServer(cnf *config.Service) *cobra.Command { } return v.Unmarshal(cnf) }, + } + + flags := command.PersistentFlags() + flags.StringVarP(&path, "config", "c", "config.toml", "path to config file") + command.AddCommand( + Run(&cnf.Server), + ) + return &command +} +func Run(cnf *config.Server) *cobra.Command { + command := cobra.Command{ + Use: "run", + Short: "run the server", + Long: "Start listening required protocols.", RunE: func(cmd *cobra.Command, args []string) error { - if cnf.Server.Twirp.IsEnabled() { + if cnf.Twirp.IsEnabled() { twirp := server.Twirp() mux := http.NewServeMux() mux.Handle(twirp.PathPrefix(), twirp) go func() { - cmd.Println("twirp server starts listening", cnf.Server.Twirp.BaseURL()) + cmd.Println("twirp server starts listening", cnf.Twirp.BaseURL()) cmd.Println("twirp server status:", http.ListenAndServe( - cnf.Server.Twirp.Address, + cnf.Twirp.Address, mux, )) }() } - if cnf.Server.GRPC.IsEnabled() { - listener, err := net.Listen("tcp", cnf.Server.GRPC.Address) + if cnf.GRPC.IsEnabled() { + listener, err := net.Listen("tcp", cnf.GRPC.Address) if err != nil { return err } srv := grpc.NewServer() v1.RegisterGreeterServiceServer(srv, new(server.GRPC)) go func() { - cmd.Println("grpc server starts listening", "tcp://"+cnf.Server.GRPC.Address) + cmd.Println("grpc server starts listening", "tcp://"+cnf.GRPC.Address) cmd.Println("grpc server status:", srv.Serve(listener)) }() - if cnf.Server.Gateway.IsEnabled() { + if cnf.Gateway.IsEnabled() { mux := runtime.NewServeMux() if err := v1.RegisterGreeterServiceHandlerFromEndpoint( cmd.Context(), mux, - cnf.Server.GRPC.Address, + cnf.GRPC.Address, []grpc.DialOption{ grpc.WithTransportCredentials(insecure.NewCredentials()), }, @@ -82,20 +96,20 @@ func NewServer(cnf *config.Service) *cobra.Command { return err } go func() { - cmd.Println("gateway starts listening", cnf.Server.Gateway.BaseURL()) + cmd.Println("gateway starts listening", cnf.Gateway.BaseURL()) cmd.Println("gateway status:", http.ListenAndServe( - cnf.Server.Gateway.Address, + cnf.Gateway.Address, mux, )) }() } } - if cnf.Server.Profile.IsEnabled() { + if cnf.Profile.IsEnabled() { mux := http.DefaultServeMux go func() { - cmd.Println("profiler starts listening", cnf.Server.Profile.BaseURL()) + cmd.Println("profiler starts listening", cnf.Profile.BaseURL()) cmd.Println("profiler status:", http.ListenAndServe( - cnf.Server.Profile.Address, + cnf.Profile.Address, mux, )) }() @@ -105,11 +119,11 @@ func NewServer(cnf *config.Service) *cobra.Command { path, handler := v1connect.NewGreeterServiceHandler(new(server.Connect)) mux.Handle(path, handler) srv := &http.Server{ - Addr: cnf.Server.Connect.Address, + Addr: cnf.Connect.Address, Handler: h2c.NewHandler(mux, new(http2.Server)), } go func() { - cmd.Println("rpc server starts listening", cnf.Server.Connect.BaseURL()) + cmd.Println("rpc server starts listening", cnf.Connect.BaseURL()) cmd.Println("rpc server status:", srv.ListenAndServe()) }() err := sync.Termination().Wait(cmd.Context()) @@ -120,10 +134,7 @@ func NewServer(cnf *config.Service) *cobra.Command { return err }, } - flags := command.PersistentFlags() - flags.StringVarP(&path, "config", "c", "config.toml", "path to config file") - flags.StringVar(&cnf.Server.Connect.Address, "host", "", "remote rpc host") - - command.AddCommand( /* add related commands */ ) + flags := command.Flags() + flags.StringVar(&cnf.Connect.Address, "host", "", "remote rpc host") return &command } diff --git a/internal/command/server_test.go b/internal/command/server_test.go index a5c3ca5..590d6ab 100644 --- a/internal/command/server_test.go +++ b/internal/command/server_test.go @@ -6,11 +6,12 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - . "go.octolab.org/template/service/internal/command" + "go.octolab.org/template/service/internal/command" + "go.octolab.org/template/service/internal/config" ) func TestNewServer(t *testing.T) { - root := NewServer() + root := command.NewServer(new(config.Service)) require.NotNil(t, root) assert.NotEmpty(t, root.Use) assert.NotEmpty(t, root.Short)