Skip to content

Commit

Permalink
fix: broken tests, refactor server command
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsk committed Nov 19, 2023
1 parent 613af15 commit b6dcd53
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 29 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ COPY server deploy/config.toml /
EXPOSE 3360 8080 8081 8890 8891

ENTRYPOINT ["/server"]
CMD ["run"]
4 changes: 2 additions & 2 deletions cmd/client/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

Expand Down
4 changes: 2 additions & 2 deletions cmd/server/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

Expand Down
1 change: 1 addition & 0 deletions deploy/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ COPY --from=builder /service/server /service/deploy/config.toml /
EXPOSE 3360 8080 8081 8890 8891

ENTRYPOINT ["/server"]
CMD ["run"]
2 changes: 1 addition & 1 deletion internal/command/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
5 changes: 3 additions & 2 deletions internal/command/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
51 changes: 31 additions & 20 deletions internal/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,58 +44,72 @@ 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()),
},
); err != nil {
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,
))
}()
Expand All @@ -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())
Expand All @@ -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
}
5 changes: 3 additions & 2 deletions internal/command/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b6dcd53

Please sign in to comment.