Skip to content

Commit b6dcd53

Browse files
committed
fix: broken tests, refactor server command
1 parent 613af15 commit b6dcd53

File tree

8 files changed

+44
-29
lines changed

8 files changed

+44
-29
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ COPY server deploy/config.toml /
1010
EXPOSE 3360 8080 8081 8890 8891
1111

1212
ENTRYPOINT ["/server"]
13+
CMD ["run"]

cmd/client/main_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ func TestExecution(t *testing.T) {
1313
t.Run("success", func(t *testing.T) {
1414
exit = func(code int) { assert.Equal(t, 0, code) }
1515
stderr, stdout = io.Discard, io.Discard
16-
os.Args = []string{"root", "version"}
16+
os.Args = []string{"version"}
1717
main()
1818
})
1919

2020
t.Run("failure", func(t *testing.T) {
2121
exit = func(code int) { assert.Equal(t, 1, code) }
2222
stderr, stdout = io.Discard, io.Discard
23-
os.Args = []string{"root", "unknown"}
23+
os.Args = []string{"unknown"}
2424
main()
2525
})
2626

cmd/server/main_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ func TestExecution(t *testing.T) {
1313
t.Run("success", func(t *testing.T) {
1414
exit = func(code int) { assert.Equal(t, 0, code) }
1515
stderr, stdout = io.Discard, io.Discard
16-
os.Args = []string{"root", "version"}
16+
os.Args = []string{"version"}
1717
main()
1818
})
1919

2020
t.Run("failure", func(t *testing.T) {
2121
exit = func(code int) { assert.Equal(t, 1, code) }
2222
stderr, stdout = io.Discard, io.Discard
23-
os.Args = []string{"root", "unknown"}
23+
os.Args = []string{"unknown"}
2424
main()
2525
})
2626

deploy/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ COPY --from=builder /service/server /service/deploy/config.toml /
3030
EXPOSE 3360 8080 8081 8890 8891
3131

3232
ENTRYPOINT ["/server"]
33+
CMD ["run"]

internal/command/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ func NewClient(cnf *config.Service) *cobra.Command {
3737
return v.Unmarshal(cnf)
3838
},
3939
}
40+
4041
flags := command.PersistentFlags()
4142
flags.StringVarP(&path, "config", "c", "config.toml", "path to config file")
4243
flags.StringVar(&cnf.Server.Connect.Address, "host", "", "remote rpc host")
43-
4444
command.AddCommand(
4545
Hello(&cnf.Server.Connect),
4646
Sign(&cnf.Server.Connect),

internal/command/client_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import (
66
"github.com/stretchr/testify/assert"
77
"github.com/stretchr/testify/require"
88

9-
. "go.octolab.org/template/service/internal/command"
9+
"go.octolab.org/template/service/internal/command"
10+
"go.octolab.org/template/service/internal/config"
1011
)
1112

1213
func TestNewClient(t *testing.T) {
13-
root := NewClient()
14+
root := command.NewClient(new(config.Service))
1415
require.NotNil(t, root)
1516
assert.NotEmpty(t, root.Use)
1617
assert.NotEmpty(t, root.Short)

internal/command/server.go

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,58 +44,72 @@ func NewServer(cnf *config.Service) *cobra.Command {
4444
}
4545
return v.Unmarshal(cnf)
4646
},
47+
}
48+
49+
flags := command.PersistentFlags()
50+
flags.StringVarP(&path, "config", "c", "config.toml", "path to config file")
51+
command.AddCommand(
52+
Run(&cnf.Server),
53+
)
54+
return &command
55+
}
4756

57+
func Run(cnf *config.Server) *cobra.Command {
58+
command := cobra.Command{
59+
Use: "run",
60+
Short: "run the server",
61+
Long: "Start listening required protocols.",
4862
RunE: func(cmd *cobra.Command, args []string) error {
49-
if cnf.Server.Twirp.IsEnabled() {
63+
if cnf.Twirp.IsEnabled() {
5064
twirp := server.Twirp()
5165
mux := http.NewServeMux()
5266
mux.Handle(twirp.PathPrefix(), twirp)
5367
go func() {
54-
cmd.Println("twirp server starts listening", cnf.Server.Twirp.BaseURL())
68+
cmd.Println("twirp server starts listening", cnf.Twirp.BaseURL())
5569
cmd.Println("twirp server status:", http.ListenAndServe(
56-
cnf.Server.Twirp.Address,
70+
cnf.Twirp.Address,
5771
mux,
5872
))
5973
}()
6074
}
61-
if cnf.Server.GRPC.IsEnabled() {
62-
listener, err := net.Listen("tcp", cnf.Server.GRPC.Address)
75+
if cnf.GRPC.IsEnabled() {
76+
listener, err := net.Listen("tcp", cnf.GRPC.Address)
6377
if err != nil {
6478
return err
6579
}
6680
srv := grpc.NewServer()
6781
v1.RegisterGreeterServiceServer(srv, new(server.GRPC))
6882
go func() {
69-
cmd.Println("grpc server starts listening", "tcp://"+cnf.Server.GRPC.Address)
83+
cmd.Println("grpc server starts listening", "tcp://"+cnf.GRPC.Address)
7084
cmd.Println("grpc server status:", srv.Serve(listener))
7185
}()
72-
if cnf.Server.Gateway.IsEnabled() {
86+
if cnf.Gateway.IsEnabled() {
7387
mux := runtime.NewServeMux()
7488
if err := v1.RegisterGreeterServiceHandlerFromEndpoint(
7589
cmd.Context(),
7690
mux,
77-
cnf.Server.GRPC.Address,
91+
cnf.GRPC.Address,
7892
[]grpc.DialOption{
7993
grpc.WithTransportCredentials(insecure.NewCredentials()),
8094
},
8195
); err != nil {
8296
return err
8397
}
8498
go func() {
85-
cmd.Println("gateway starts listening", cnf.Server.Gateway.BaseURL())
99+
cmd.Println("gateway starts listening", cnf.Gateway.BaseURL())
86100
cmd.Println("gateway status:", http.ListenAndServe(
87-
cnf.Server.Gateway.Address,
101+
cnf.Gateway.Address,
88102
mux,
89103
))
90104
}()
91105
}
92106
}
93-
if cnf.Server.Profile.IsEnabled() {
107+
if cnf.Profile.IsEnabled() {
94108
mux := http.DefaultServeMux
95109
go func() {
96-
cmd.Println("profiler starts listening", cnf.Server.Profile.BaseURL())
110+
cmd.Println("profiler starts listening", cnf.Profile.BaseURL())
97111
cmd.Println("profiler status:", http.ListenAndServe(
98-
cnf.Server.Profile.Address,
112+
cnf.Profile.Address,
99113
mux,
100114
))
101115
}()
@@ -105,11 +119,11 @@ func NewServer(cnf *config.Service) *cobra.Command {
105119
path, handler := v1connect.NewGreeterServiceHandler(new(server.Connect))
106120
mux.Handle(path, handler)
107121
srv := &http.Server{
108-
Addr: cnf.Server.Connect.Address,
122+
Addr: cnf.Connect.Address,
109123
Handler: h2c.NewHandler(mux, new(http2.Server)),
110124
}
111125
go func() {
112-
cmd.Println("rpc server starts listening", cnf.Server.Connect.BaseURL())
126+
cmd.Println("rpc server starts listening", cnf.Connect.BaseURL())
113127
cmd.Println("rpc server status:", srv.ListenAndServe())
114128
}()
115129
err := sync.Termination().Wait(cmd.Context())
@@ -120,10 +134,7 @@ func NewServer(cnf *config.Service) *cobra.Command {
120134
return err
121135
},
122136
}
123-
flags := command.PersistentFlags()
124-
flags.StringVarP(&path, "config", "c", "config.toml", "path to config file")
125-
flags.StringVar(&cnf.Server.Connect.Address, "host", "", "remote rpc host")
126-
127-
command.AddCommand( /* add related commands */ )
137+
flags := command.Flags()
138+
flags.StringVar(&cnf.Connect.Address, "host", "", "remote rpc host")
128139
return &command
129140
}

internal/command/server_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import (
66
"github.com/stretchr/testify/assert"
77
"github.com/stretchr/testify/require"
88

9-
. "go.octolab.org/template/service/internal/command"
9+
"go.octolab.org/template/service/internal/command"
10+
"go.octolab.org/template/service/internal/config"
1011
)
1112

1213
func TestNewServer(t *testing.T) {
13-
root := NewServer()
14+
root := command.NewServer(new(config.Service))
1415
require.NotNil(t, root)
1516
assert.NotEmpty(t, root.Use)
1617
assert.NotEmpty(t, root.Short)

0 commit comments

Comments
 (0)