From d4e18eb82f206d01be0fc115859e302b7248005f Mon Sep 17 00:00:00 2001 From: xiadanni Date: Wed, 29 Sep 2021 05:29:21 +0800 Subject: [PATCH] rubik: add version and ping handler Signed-off-by: xiadanni Signed-off-by: jiangpengfei --- Makefile | 16 ++++++++++++- VERSION | 1 + api/api.go | 9 +++++++ pkg/httpserver/server.go | 36 ++++++++++++++++++++++++++++ pkg/version/version.go | 51 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 VERSION create mode 100644 pkg/version/version.go diff --git a/Makefile b/Makefile index ab28f7e..e7be2ad 100644 --- a/Makefile +++ b/Makefile @@ -13,9 +13,23 @@ CWD=$(realpath .) TMP_DIR := /tmp/rubik_tmpdir +VERSION_FILE := ./VERSION +TEST_FILE := ./TEST +VERSION := $(shell cat $(VERSION_FILE) | awk -F"-" '{print $$1}') +RELEASE := $(shell cat $(VERSION_FILE) | awk -F"-" '{print $$2}') +BUILD_TIME := $(shell date "+%Y-%m-%d") +USAGE := $(shell [ -f $(TEST_FILE) ] && echo 'TestOnly') +GIT_COMMIT := $(if $(shell git rev-parse --short HEAD),$(shell git rev-parse --short HEAD),$(shell cat ./git-commit | head -c 7)) DEBUG_FLAGS := -gcflags="all=-N -l" -LD_FLAGS := -ldflags '-buildid=none -tmpdir=$(TMP_DIR) -extldflags=-ftrapv -extldflags=-Wl,-z,relro,-z,now -linkmode=external -extldflags=-static' +LD_FLAGS := -ldflags '-buildid=none -tmpdir=$(TMP_DIR) \ + -X isula.org/rubik/pkg/version.GitCommit=$(GIT_COMMIT) \ + -X isula.org/rubik/pkg/version.BuildTime=$(BUILD_TIME) \ + -X isula.org/rubik/pkg/version.Version=$(VERSION) \ + -X isula.org/rubik/pkg/version.Release=$(RELEASE) \ + -X isula.org/rubik/pkg/version.Usage=$(USAGE) \ + -extldflags=-ftrapv \ + -extldflags=-Wl,-z,relro,-z,now -linkmode=external -extldflags=-static' export GO111MODULE=off diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..48d014e --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +0.0.1-1 diff --git a/api/api.go b/api/api.go index ecd5dce..eff33c5 100644 --- a/api/api.go +++ b/api/api.go @@ -30,3 +30,12 @@ type SetQosResponse struct { ErrCode int `json:"code"` Message string `json:"msg"` } + +// VersionResponse is version response for http responser +type VersionResponse struct { + Version string `json:"Version"` + Release string `json:"Release"` + GitCommit string `json:"Commit"` + BuildTime string `json:"BuildTime"` + Usage string `json:"Usage,omitempty"` +} diff --git a/pkg/httpserver/server.go b/pkg/httpserver/server.go index b177aae..0b83275 100644 --- a/pkg/httpserver/server.go +++ b/pkg/httpserver/server.go @@ -24,6 +24,7 @@ import ( "isula.org/rubik/api" "isula.org/rubik/pkg/constant" + "isula.org/rubik/pkg/version" "isula.org/rubik/pkg/workerpool" ) @@ -101,6 +102,39 @@ func RootHandler(w http.ResponseWriter, r *http.Request) { writeRootResponse(ctx, w, constant.DefaultSucceedCode, "") } +func ping(ctx context.Context, w http.ResponseWriter, r *http.Request) { + writeResponse(ctx, w, []byte("ok")) +} + +// PingHandler is used for check if rubik is still alive or not +func PingHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != "GET" { + http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) + return + } + + ctx, cancel := newContext(handleTimeout) + ping(ctx, w, r) + cancel() +} + +func versionHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { + msg, _ := json.Marshal(api.VersionResponse{Version: version.Version, Release: version.Release, + GitCommit: version.GitCommit, BuildTime: version.BuildTime, Usage: version.Usage}) + writeResponse(ctx, w, msg) +} + +// VersionHandler is used for check if rubik version +func VersionHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != "GET" { + http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) + return + } + ctx, cancel := newContext(handleTimeout) + versionHandler(ctx, w, r) + cancel() +} + func writeResponse(ctx context.Context, w http.ResponseWriter, data []byte) { w.WriteHeader(http.StatusOK) w.Write(data) @@ -120,6 +154,8 @@ func setupHandler() *http.ServeMux { mux := http.NewServeMux() mux.HandleFunc("/", RootHandler) + mux.HandleFunc("/ping", PingHandler) + mux.HandleFunc("/version", VersionHandler) return mux } diff --git a/pkg/version/version.go b/pkg/version/version.go new file mode 100644 index 0000000..c77820f --- /dev/null +++ b/pkg/version/version.go @@ -0,0 +1,51 @@ +// Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. +// rubik licensed under the Mulan PSL v2. +// You can use this software according to the terms and conditions of the Mulan PSL v2. +// You may obtain a copy of Mulan PSL v2 at: +// http://license.coscl.org.cn/MulanPSL2 +// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +// PURPOSE. +// See the Mulan PSL v2 for more details. +// Author: Xiang Li +// Create: 2021-04-25 +// Description: version releated + +// Package version is for version check +package version + +import ( + "fmt" + "os" + "runtime" +) + +var ( + // Version represents rubik version + Version string + // Release represents rubik release number + Release string + // GitCommit represents git commit number + GitCommit string + // BuildTime represents build time + BuildTime string + // Usage represents usage for this release + Usage string +) + +func init() { + var showVersion bool + if len(os.Args) == 2 && os.Args[1] == "-v" { + showVersion = true + } + + if showVersion { + fmt.Println("Version: ", Version) + fmt.Println("Release: ", Release) + fmt.Println("Go Version: ", runtime.Version()) + fmt.Println("Git Commit: ", GitCommit) + fmt.Println("Built: ", BuildTime) + fmt.Println("OS/Arch: ", runtime.GOOS+"/"+runtime.GOARCH) + os.Exit(0) + } +} -- Gitee