You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by de...@apache.org on 2018/03/13 19:17:42 UTC
[incubator-trafficcontrol] 03/05: api/1.3/about.json endpoint
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 31d4209afc429625e92f3f02c08673cb65db34d5
Author: Dan Kirkwood <da...@apache.org>
AuthorDate: Mon Mar 12 14:33:37 2018 -0600
api/1.3/about.json endpoint
---
traffic_ops/traffic_ops_golang/routes.go | 6 +--
.../traffic_ops_golang/traffic_ops_golang.go | 4 +-
traffic_ops/traffic_ops_golang/utils/about.go | 60 ++++++++++++++++++----
.../utils/{about.go => about_test.go} | 36 ++++++++-----
traffic_ops/traffic_ops_golang/wrappers.go | 2 +-
5 files changed, 78 insertions(+), 30 deletions(-)
diff --git a/traffic_ops/traffic_ops_golang/routes.go b/traffic_ops/traffic_ops_golang/routes.go
index 8294f14..fbe0f7f 100644
--- a/traffic_ops/traffic_ops_golang/routes.go
+++ b/traffic_ops/traffic_ops_golang/routes.go
@@ -67,6 +67,9 @@ func Routes(d ServerData) ([]Route, http.Handler, error) {
proxyHandler := rootHandler(d)
routes := []Route{
+ //About
+ {1.3, http.MethodGet, `about/?(\.json)?$`, utils.AboutHandler(), auth.PrivLevelReadOnly, Authenticated, nil},
+
// Proxied routes
//CDNs
// explicitly passed to legacy system until fully implemented. Auth handled by legacy system.
@@ -187,9 +190,6 @@ 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 60347b7..56edf91 100644
--- a/traffic_ops/traffic_ops_golang/traffic_ops_golang.go
+++ b/traffic_ops/traffic_ops_golang/traffic_ops_golang.go
@@ -39,7 +39,7 @@ import (
var version = "development"
func init() {
- utils.SetVersion(version)
+ utils.About.TrafficOpsRPMVersion = version
}
func main() {
@@ -50,7 +50,7 @@ func main() {
flag.Parse()
if *showVersion {
- fmt.Println(utils.Version)
+ fmt.Println(utils.About.TrafficOpsRPMVersion)
os.Exit(0)
}
if len(os.Args) < 2 {
diff --git a/traffic_ops/traffic_ops_golang/utils/about.go b/traffic_ops/traffic_ops_golang/utils/about.go
index e798bba..d55b03c 100644
--- a/traffic_ops/traffic_ops_golang/utils/about.go
+++ b/traffic_ops/traffic_ops_golang/utils/about.go
@@ -22,21 +22,61 @@ package utils
import (
"encoding/json"
"net/http"
+ "runtime"
+ "strings"
)
-// Version allows access to the version identified at build time
-var Version = "development"
+// about allows access to the version info identified at build time
+type about struct {
+ CommitHash string `json:"commitHash,omitempty"`
+ Commits string `json:"commits,omitempty"`
+ GoVersion string `json:"goVersion,omitempty"`
+ TrafficOpsName string `json:"name,omitempty"`
+ TrafficOpsRPMVersion string `json:"trafficOpsRPMVersion,omitempty"`
+ TrafficOpsVersion string `json:"trafficOpsVersion,omitempty"`
+}
+
+// About contains version info to be exposed by `api/.../about.json` endpoint
+var About about
+
+func splitRPMVersion(v string) (string, string, string, string) {
-// 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
+ if v == "" {
+ return "UnknownVersion", "", "", ""
+ }
+ // RPM version is something like traffic_ops-2.3.0-8765.a0b1c3d4
+ // -- if not of that form, Name, Version, Commits, CommitHash may be missing
+ s := strings.SplitN(v, "-", 3)
+ if len(s) >= 3 {
+ // 3rd field is commits.hash
+ t := strings.SplitN(s[2], ".", 2)
+ s = append(s[0:2], t...)
+ }
+ for cap(s) < 4 {
+ s = append(s, "")
+ }
+ return s[0], s[1], s[2], s[3]
}
-// VersionHandler returns the version number set at compile time
-func VersionHandler() http.HandlerFunc {
+func init() {
+ // name, version, commits, hash -- parts of rpm version string
+ n, v, c, h := splitRPMVersion(About.TrafficOpsRPMVersion)
+ About = about{
+ GoVersion: runtime.Version(),
+ TrafficOpsRPMVersion: About.TrafficOpsRPMVersion,
+ TrafficOpsName: n,
+ TrafficOpsVersion: v,
+ Commits: c,
+ CommitHash: h,
+ }
+
+}
+
+// AboutHandler returns info about running Traffic Ops
+func AboutHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
- m := map[string]string{"version": Version}
- json.NewEncoder(w).Encode(m)
+ w.Header().Set("Content-Type", "application/json")
+
+ json.NewEncoder(w).Encode(About)
}
}
diff --git a/traffic_ops/traffic_ops_golang/utils/about.go b/traffic_ops/traffic_ops_golang/utils/about_test.go
similarity index 54%
copy from traffic_ops/traffic_ops_golang/utils/about.go
copy to traffic_ops/traffic_ops_golang/utils/about_test.go
index e798bba..bcbc444 100644
--- a/traffic_ops/traffic_ops_golang/utils/about.go
+++ b/traffic_ops/traffic_ops_golang/utils/about_test.go
@@ -20,23 +20,31 @@ package utils
*/
import (
- "encoding/json"
- "net/http"
+ "testing"
)
-// 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
+var testSet = map[string][]string{
+ "test0-0.1.0-1234.01ab23cd": []string{"test0", "0.1.0", "1234", "01ab23cd"},
+ "test1-0.2.0": []string{"test1", "0.2.0", "", ""},
+ "test2": []string{"test2", "", "", ""},
}
-// 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)
+func TestSplitRPMVersion(t *testing.T) {
+ for s, e := range testSet {
+ t.Logf("Testing %s %v", s, e)
+ n, v, c, h := splitRPMVersion(s)
+
+ if n != e[0] {
+ t.Errorf("expected name '%s', got '%s'", n, e[0])
+ }
+ if v != e[1] {
+ t.Errorf("expected version '%s', got '%s'", v, e[1])
+ }
+ if c != e[2] {
+ t.Errorf("expected commits '%s', got '%s'", c, e[2])
+ }
+ if h != e[3] {
+ t.Errorf("expected commitHash '%s', got '%s'", h, e[3])
+ }
}
}
diff --git a/traffic_ops/traffic_ops_golang/wrappers.go b/traffic_ops/traffic_ops_golang/wrappers.go
index cbc5d7a..a65eb6e 100644
--- a/traffic_ops/traffic_ops_golang/wrappers.go
+++ b/traffic_ops/traffic_ops_golang/wrappers.go
@@ -42,7 +42,7 @@ import (
)
// ServerName - the server identifier
-var ServerName = "traffic_ops_golang" + "/" + utils.Version
+var ServerName = "traffic_ops_golang" + "/" + utils.About.TrafficOpsVersion
// AuthBase ...
type AuthBase struct {
--
To stop receiving notification emails like this one, please contact
dewrich@apache.org.