This is an automated email from the ASF dual-hosted git repository. dewrich pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-trafficcontrol.git
commit f50ff79dbf185ec7299ee2ff73597a69e0e450d4 Author: Dan Kirkwood <dang...@apache.org> AuthorDate: Fri Mar 9 14:45:49 2018 -0700 add /api/1.3/version endpoint and `-version` option on cmd line --- traffic_ops/build/traffic_ops.spec | 2 +- traffic_ops/traffic_ops_golang/routes.go | 4 +++ .../traffic_ops_golang/traffic_ops_golang.go | 14 ++++++-- traffic_ops/traffic_ops_golang/utils/version.go | 42 ++++++++++++++++++++++ traffic_ops/traffic_ops_golang/wrappers.go | 3 +- 5 files changed, 61 insertions(+), 4 deletions(-) diff --git a/traffic_ops/build/traffic_ops.spec b/traffic_ops/build/traffic_ops.spec index ef80939..326bcf6 100644 --- a/traffic_ops/build/traffic_ops.spec +++ b/traffic_ops/build/traffic_ops.spec @@ -104,7 +104,7 @@ Built: %(date) by %{getenv: USER} #echo "go getting at $(pwd)" && \ #go get -d -v && \ echo "go building at $(pwd)" && \ - go build -ldflags "-B 0x`git rev-parse HEAD`" \ + go build -ldflags "-X main.version=%{version}-%{release} -B 0x`git rev-parse HEAD`" \ ) || { echo "Could not build go program at $(pwd): $!"; exit 1; } %install diff --git a/traffic_ops/traffic_ops_golang/routes.go b/traffic_ops/traffic_ops_golang/routes.go index ff59cc3..8294f14 100644 --- a/traffic_ops/traffic_ops_golang/routes.go +++ b/traffic_ops/traffic_ops_golang/routes.go @@ -45,6 +45,7 @@ import ( "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/status" "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/systeminfo" "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/types" + "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/utils" "github.com/basho/riak-go-client" ) @@ -186,6 +187,9 @@ func Routes(d ServerData) ([]Route, http.Handler, error) { //System {1.2, http.MethodGet, `system/info/?(\.json)?$`, systeminfo.Handler(d.DB), auth.PrivLevelReadOnly, Authenticated, nil}, + + //Version + {1.3, http.MethodGet, `version$`, utils.VersionHandler(), auth.PrivLevelReadOnly, Authenticated, nil}, } return routes, proxyHandler, nil } diff --git a/traffic_ops/traffic_ops_golang/traffic_ops_golang.go b/traffic_ops/traffic_ops_golang/traffic_ops_golang.go index af61ae8..60347b7 100644 --- a/traffic_ops/traffic_ops_golang/traffic_ops_golang.go +++ b/traffic_ops/traffic_ops_golang/traffic_ops_golang.go @@ -29,20 +29,30 @@ import ( "time" "github.com/apache/incubator-trafficcontrol/lib/go-log" + "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/utils" "github.com/jmoiron/sqlx" _ "github.com/lib/pq" ) -// Version ... -const Version = "0.1" +// set the version at build time: `go build -X "main.version=..."` +var version = "development" + +func init() { + utils.SetVersion(version) +} func main() { + showVersion := flag.Bool("version", false, "Show version and exit") configFileName := flag.String("cfg", "", "The config file path") dbConfigFileName := flag.String("dbcfg", "", "The db config file path") riakConfigFileName := flag.String("riakcfg", "", "The riak config file path") flag.Parse() + if *showVersion { + fmt.Println(utils.Version) + os.Exit(0) + } if len(os.Args) < 2 { flag.Usage() os.Exit(1) diff --git a/traffic_ops/traffic_ops_golang/utils/version.go b/traffic_ops/traffic_ops_golang/utils/version.go new file mode 100644 index 0000000..e798bba --- /dev/null +++ b/traffic_ops/traffic_ops_golang/utils/version.go @@ -0,0 +1,42 @@ +package utils + +/* + * 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. + */ + +import ( + "encoding/json" + "net/http" +) + +// Version allows access to the version identified at build time +var Version = "development" + +// SetVersion sets the version string from main so other packages can access it +// Set the Version string at build time using `go build -X "main.version=xxx"` +func SetVersion(v string) { + Version = v +} + +// VersionHandler returns the version number set at compile time +func VersionHandler() http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + m := map[string]string{"version": Version} + json.NewEncoder(w).Encode(m) + } +} diff --git a/traffic_ops/traffic_ops_golang/wrappers.go b/traffic_ops/traffic_ops_golang/wrappers.go index 812936b..cbc5d7a 100644 --- a/traffic_ops/traffic_ops_golang/wrappers.go +++ b/traffic_ops/traffic_ops_golang/wrappers.go @@ -37,11 +37,12 @@ import ( tc "github.com/apache/incubator-trafficcontrol/lib/go-tc" "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/auth" "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/tocookie" + "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/utils" "github.com/jmoiron/sqlx" ) // ServerName - the server identifier -const ServerName = "traffic_ops_golang" + "/" + Version +var ServerName = "traffic_ops_golang" + "/" + utils.Version // AuthBase ... type AuthBase struct { -- To stop receiving notification emails like this one, please contact dewr...@apache.org.