diff --git a/api/build.sh b/api/build.sh index 70ed743447..6cd249ad22 100755 --- a/api/build.sh +++ b/api/build.sh @@ -20,7 +20,7 @@ set -e VERSION=$(cat ./api/VERSION) GITHASH=$(cat ./.githash 2> /dev/null || HASH="ref: HEAD"; while [[ $HASH == ref\:* ]]; do HASH="$(cat ".git/$(echo $HASH | cut -d \ -f 2)")"; done; echo ${HASH:0:7}) -GOLDFLAGS="-X github.com/apisix/manager-api/cmd.Version=${VERSION} -X github.com/apisix/manager-api/cmd.GitHash=${GITHASH}" +GOLDFLAGS="-X github.com/apisix/manager-api/internal/utils.version=${VERSION} -X github.com/apisix/manager-api/internal/utils.gitHash=${GITHASH}" # Enter dry-run mode if [ "$1" == "--dry-run" ]; then diff --git a/api/cmd/managerapi.go b/api/cmd/managerapi.go index d0dc1d02e1..419dc87cfe 100644 --- a/api/cmd/managerapi.go +++ b/api/cmd/managerapi.go @@ -73,6 +73,8 @@ func NewManagerAPICommand() *cobra.Command { return nil }) + GitHash, Version = utils.GetHashAndVersion() + droplet.Option.Orchestrator = func(mws []droplet.Middleware) []droplet.Middleware { var newMws []droplet.Middleware // default middleware order: resp_reshape, auto_input, traffic_log @@ -90,6 +92,7 @@ func NewManagerAPICommand() *cobra.Command { log.Errorf("init stores fail: %w", err) panic(err) } + // routes r := internal.SetUpRouter() addr := fmt.Sprintf("%s:%d", conf.ServerHost, conf.ServerPort) @@ -146,7 +149,7 @@ func newStopCommand() *cobra.Command { if syscall.ENOENT.Error() != err.Error() { fmt.Fprintf(os.Stderr, "failed to get manager-api pid: %s\n", err) } else { - fmt.Fprintf(os.Stderr, "pid path %s not found, is manager-api running?\n", conf.PIDPath) + fmt.Fprintf(os.Stderr, "pid path %s not found, is manager-api running?\n", conf.PIDPath) } return } diff --git a/api/internal/handler/tool/tool.go b/api/internal/handler/tool/tool.go new file mode 100644 index 0000000000..def441b1ff --- /dev/null +++ b/api/internal/handler/tool/tool.go @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package tool + +import ( + "github.com/gin-gonic/gin" + "github.com/shiningrush/droplet" + wgin "github.com/shiningrush/droplet/wrapper/gin" + + "github.com/apisix/manager-api/internal/handler" + "github.com/apisix/manager-api/internal/utils" +) + +type Handler struct { +} + +type InfoOutput struct { + Hash string `json:"commit_hash"` + Version string `json:"version"` +} + +func NewHandler() (handler.RouteRegister, error) { + return &Handler{}, nil +} + +func (h *Handler) ApplyRoute(r *gin.Engine) { + r.GET("/version", wgin.Wraps(h.Version)) +} + +func (h *Handler) Version(_ droplet.Context) (interface{}, error) { + hash, version := utils.GetHashAndVersion() + return &InfoOutput{ + Hash: hash, + Version: version, + }, nil +} diff --git a/api/internal/handler/tool/tool_test.go b/api/internal/handler/tool/tool_test.go new file mode 100644 index 0000000000..9754cca4b7 --- /dev/null +++ b/api/internal/handler/tool/tool_test.go @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package tool + +import ( + "testing" + + "github.com/shiningrush/droplet" + "github.com/stretchr/testify/assert" + + "github.com/apisix/manager-api/internal/utils" +) + +func TestInfo_Get(t *testing.T) { + h := Handler{} + ctx := droplet.NewContext() + + hash, version := utils.GetHashAndVersion() + + ret, err := h.Version(ctx) + assert.Nil(t, err) + assert.Equal(t, &InfoOutput{ + Hash: hash, + Version: version, + }, ret) +} diff --git a/api/internal/route.go b/api/internal/route.go index ac78a36002..26fe1a41b9 100644 --- a/api/internal/route.go +++ b/api/internal/route.go @@ -41,6 +41,7 @@ import ( "github.com/apisix/manager-api/internal/handler/server_info" "github.com/apisix/manager-api/internal/handler/service" "github.com/apisix/manager-api/internal/handler/ssl" + "github.com/apisix/manager-api/internal/handler/tool" "github.com/apisix/manager-api/internal/handler/upstream" "github.com/apisix/manager-api/internal/log" ) @@ -76,6 +77,7 @@ func SetUpRouter() *gin.Engine { label.NewHandler, data_loader.NewHandler, data_loader.NewImportHandler, + tool.NewHandler, } for i := range factories { diff --git a/api/internal/utils/version.go b/api/internal/utils/version.go new file mode 100644 index 0000000000..d4bec4ab00 --- /dev/null +++ b/api/internal/utils/version.go @@ -0,0 +1,27 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package utils + +var ( + gitHash string + version string +) + +// get the hash and version +func GetHashAndVersion() (string, string) { + return gitHash, version +} diff --git a/api/test/e2e/version_test.go b/api/test/e2e/version_test.go new file mode 100644 index 0000000000..f486e3ddfd --- /dev/null +++ b/api/test/e2e/version_test.go @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package e2e + +import ( + "net/http" + "testing" +) + +func TestInfo(t *testing.T) { + tests := []HttpTestCase{ + { + Desc: "get info", + Object: ManagerApiExpect(t), + Method: http.MethodGet, + Path: "/version", + ExpectStatus: http.StatusOK, + ExpectBody: []string{"commit_hash", "\"version\""}, + }, + } + + for _, tc := range tests { + testCaseCheck(tc, t) + } +} diff --git a/api/test/shell/cli_test.sh b/api/test/shell/cli_test.sh index c272c367dd..5ec96cc1a6 100755 --- a/api/test/shell/cli_test.sh +++ b/api/test/shell/cli_test.sh @@ -52,7 +52,7 @@ clean_logfile() { trap clean_up EXIT export GO111MODULE=on -go build -o ./manager-api -ldflags "-X github.com/apisix/manager-api/cmd.Version=${VERSION} -X github.com/apisix/manager-api/cmd.GitHash=${GITHASH}" ./cmd/manager +go build -o ./manager-api -ldflags "-X github.com/apisix/manager-api/internal/utils.version=${VERSION} -X github.com/apisix/manager-api/internal/utils.gitHash=${GITHASH}" ./cmd/manager # default level: warn, path: logs/error.log