Skip to content

Commit c360d3d

Browse files
committed
fix: version command
1 parent 3db4654 commit c360d3d

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

api/api_impl/server.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package apiimpl
33
import (
44
"context"
55
"errors"
6+
"fmt"
67

78
"net"
89
"net/http"
@@ -29,9 +30,6 @@ func SetupAPI(lc fx.Lifecycle, apiConfig *config.APIConfig, controller APIContro
2930
return err
3031
}
3132

32-
// Clear out the servers array in the swagger spec, that skips validating
33-
// that server names match. We don't know how this thing will be run.
34-
swagger.Servers = nil
3533
// This is how you set up a basic chi router
3634
r := chi.NewRouter()
3735

@@ -58,9 +56,13 @@ func SetupAPI(lc fx.Lifecycle, apiConfig *config.APIConfig, controller APIContro
5856
return nil
5957
},
6058
},
59+
SilenceServersWarning: true,
6160
}),
6261
)
63-
62+
chi.Walk(r, func(method string, route string, handler http.Handler, middlewares ...func(http.Handler) http.Handler) error {
63+
fmt.Printf("[%s]: '%s' has %d middlewares\n", method, route, len(middlewares))
64+
return nil
65+
})
6466
api.HandlerFromMuxWithBaseURL(controller, r, APIV1Prefix)
6567

6668
url, err := url.Parse(apiConfig.Listen)

cmd/helper.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cmd
2+
3+
import (
4+
"github.com/jiaozifs/jiaozifs/api"
5+
"github.com/jiaozifs/jiaozifs/config"
6+
)
7+
8+
func GetDefaultClient() (*api.Client, error) {
9+
swagger, err := api.GetSwagger()
10+
if err != nil {
11+
return nil, err
12+
}
13+
14+
//get runtime version
15+
cfg, err := config.LoadConfig(cfgFile)
16+
if err != nil {
17+
return nil, err
18+
}
19+
return api.NewClient(cfg.API.Listen + swagger.Servers[0].URL)
20+
}

cmd/version.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ package cmd
33
import (
44
"fmt"
55

6-
"github.com/jiaozifs/jiaozifs/api"
7-
apiimpl "github.com/jiaozifs/jiaozifs/api/api_impl"
8-
"github.com/jiaozifs/jiaozifs/config"
96
"github.com/jiaozifs/jiaozifs/version"
7+
8+
"github.com/jiaozifs/jiaozifs/api"
109
"github.com/spf13/cobra"
1110
)
1211

@@ -23,12 +22,7 @@ var versionCmd = &cobra.Command{
2322
fmt.Println("Version ", version.UserVersion())
2423
fmt.Println("API Version ", swagger.Info.Version)
2524

26-
//get runtime version
27-
cfg, err := config.LoadConfig(cfgFile)
28-
if err != nil {
29-
return err
30-
}
31-
client, err := api.NewClient(cfg.API.Listen + apiimpl.APIV1Prefix)
25+
client, err := GetDefaultClient()
3226
if err != nil {
3327
return err
3428
}
@@ -37,10 +31,12 @@ var versionCmd = &cobra.Command{
3731
if err != nil {
3832
return err
3933
}
34+
4035
okResp, err := api.ParseGetVersionResponse(versionResp)
4136
if err != nil {
4237
return err
4338
}
39+
4440
if okResp.JSON200 == nil {
4541
return fmt.Errorf("request version fail %d %s", okResp.HTTPResponse.StatusCode, okResp.HTTPResponse.Body)
4642
}

0 commit comments

Comments
 (0)