Skip to content

Commit d9fdfd0

Browse files
author
Mike Johanson
committed
feature(version): Add /api/version endpoint to SDK
This endpoint will provide both version of the SDK that is in use as well as the version of the application service. These version values are intended to be set at build time leveraging -ldflags. closes #152 Signed-off-by: Mike Johanson <motifmike@gmail.com>
1 parent 317fead commit d9fdfd0

6 files changed

Lines changed: 40 additions & 4 deletions

File tree

examples/simple-filter-xml/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const (
3333
var counter int
3434

3535
func main() {
36+
3637
// 1) First thing to do is to create an instance of the EdgeX SDK and initialize it.
3738
edgexSdk := &appsdk.AppFunctionsSDK{ServiceKey: serviceKey}
3839
if err := edgexSdk.Initialize(); err != nil {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.12
55
require (
66
bitbucket.org/bertimus9/systemstat v0.0.0-20180207000608-0eeff89b0690
77
github.com/eclipse/paho.mqtt.golang v1.2.0
8-
github.com/edgexfoundry/go-mod-core-contracts v0.1.0
8+
github.com/edgexfoundry/go-mod-core-contracts v0.1.14
99
github.com/edgexfoundry/go-mod-messaging v0.1.11
1010
github.com/edgexfoundry/go-mod-registry v0.1.0
1111
github.com/go-stack/stack v1.8.0 // indirect

internal/constants.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,9 @@ const (
2525
ApiPingRoute = "/api/v1/ping"
2626
LogDurationKey = "duration"
2727
)
28+
29+
// SDKVersion indicates the version of the SDK - will be overwritten by build
30+
var SDKVersion string = "0.0.0"
31+
32+
// ApplicationVersion indicates the version of the application itself, not the SDK - will be overwritten by build
33+
var ApplicationVersion string = "0.0.0"

internal/webserver/server.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import (
2121
"fmt"
2222
"net/http"
2323

24-
"github.com/edgexfoundry/app-functions-sdk-go/internal/telemetry"
25-
24+
"github.com/edgexfoundry/app-functions-sdk-go/internal"
2625
"github.com/edgexfoundry/app-functions-sdk-go/internal/common"
26+
"github.com/edgexfoundry/app-functions-sdk-go/internal/telemetry"
2727
"github.com/edgexfoundry/go-mod-core-contracts/clients"
2828
"github.com/edgexfoundry/go-mod-core-contracts/clients/logger"
2929

@@ -68,6 +68,19 @@ func (webserver *WebServer) metricsHandler(writer http.ResponseWriter, _ *http.R
6868

6969
return
7070
}
71+
func (webserver *WebServer) versionHandler(writer http.ResponseWriter, _ *http.Request) {
72+
type Version struct {
73+
Version string `json:"version"`
74+
SDKVersion string `json:"sdk_version"`
75+
}
76+
version := Version{
77+
Version: internal.ApplicationVersion,
78+
SDKVersion: internal.SDKVersion,
79+
}
80+
webserver.encode(version, writer)
81+
82+
return
83+
}
7184

7285
// ConfigureStandardRoutes loads up some default routes
7386
func (webserver *WebServer) ConfigureStandardRoutes() {
@@ -83,6 +96,8 @@ func (webserver *WebServer) ConfigureStandardRoutes() {
8396
// Metrics
8497
webserver.router.HandleFunc(clients.ApiMetricsRoute, webserver.metricsHandler).Methods(http.MethodGet)
8598

99+
// Version
100+
webserver.router.HandleFunc(clients.ApiVersionRoute, webserver.versionHandler).Methods(http.MethodGet)
86101
}
87102

88103
// SetupTriggerRoute adds a route to handle trigger pipeline from HTTP request

internal/webserver/server_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,21 @@ func TestConfigureAndPingRoute(t *testing.T) {
5454
assert.Equal(t, "pong", body)
5555

5656
}
57+
func TestConfigureAndVersionRoute(t *testing.T) {
5758

59+
webserver := WebServer{
60+
LoggingClient: logClient,
61+
}
62+
webserver.ConfigureStandardRoutes()
63+
64+
req, _ := http.NewRequest("GET", clients.ApiVersionRoute, nil)
65+
rr := httptest.NewRecorder()
66+
webserver.router.ServeHTTP(rr, req)
67+
68+
body := rr.Body.String()
69+
assert.Equal(t, "{\"version\":\"0.0.0\",\"sdk_version\":\"0.0.0\"}\n", body)
70+
71+
}
5872
func TestConfigureAndConfigRoute(t *testing.T) {
5973

6074
webserver := WebServer{

makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ MICROSERVICES=examples/simple-filter-xml/simple-filter-xml examples/simple-cbor-
77

88
VERSION=$(shell cat ./VERSION)
99

10-
GOFLAGS=-ldflags "-X app-functions-sdk-go.Version=$(VERSION)"
10+
GOFLAGS=-ldflags "-X github.com/edgexfoundry/app-functions-sdk-go/internal.SDKVersion=$(VERSION) -X github.com/edgexfoundry/app-functions-sdk-go/internal.ApplicationVersion=$(VERSION)"
1111

1212
GIT_SHA=$(shell git rev-parse HEAD)
1313

0 commit comments

Comments
 (0)