Skip to content

Commit

Permalink
add calls type; add better build version info and /version api
Browse files Browse the repository at this point in the history
  • Loading branch information
cpoile committed Nov 10, 2023
1 parent d41f502 commit f45900b
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 12 deletions.
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ GO=go

# Set version variables for LDFLAGS
GIT_VERSION ?= $(shell git describe --tags --always --dirty)
GIT_HASH ?= $(shell git rev-parse HEAD)
BUILD_HASH = $(shell git rev-parse --short HEAD)
BUILD_TAG_LATEST = $(shell git describe --tags --match 'v*' --abbrev=0)
BUILD_TAG_CURRENT = $(shell git tag --points-at HEAD)
DATE_FMT = +'%Y-%m-%dT%H:%M:%SZ'
SOURCE_DATE_EPOCH ?= $(shell git log -1 --pretty=%ct)
ifdef SOURCE_DATE_EPOCH
Expand All @@ -23,7 +25,7 @@ ifeq ($(DIFF), 1)
endif

PP_PKG=github.com/mattermost/mattermost-push-proxy/internal/version
LDFLAGS="-X $(PP_PKG).gitVersion=$(GIT_VERSION) -X $(PP_PKG).gitCommit=$(GIT_HASH) -X $(PP_PKG).gitTreeState=$(GIT_TREESTATE) -X $(PP_PKG).buildDate=$(BUILD_DATE)"
LDFLAGS="-X $(PP_PKG).gitVersion=$(GIT_VERSION) -X $(PP_PKG).buildHash=$(BUILD_HASH) -X $(PP_PKG).buildTagLatest=$(BUILD_TAG_LATEST) -X $(PP_PKG).buildTagCurrent=$(BUILD_TAG_CURRENT) -X $(PP_PKG).gitTreeState=$(GIT_TREESTATE) -X $(PP_PKG).buildDate=$(BUILD_DATE)"

DIST_ROOT=dist
DIST_PATH=$(DIST_ROOT)/mattermost-push-proxy
Expand Down Expand Up @@ -161,7 +163,7 @@ clean:

run:
@echo Starting go web server
$(GO) run $(GOFLAGS) -ldflags '$(LDFLAGS)' main.go
$(GO) run $(GOFLAGS) -ldflags $(LDFLAGS) main.go

build-swagger:
npm run validate
Expand Down
33 changes: 28 additions & 5 deletions internal/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ var (
// Output of "git describe". The prerequisite is that the branch should be
// tagged using the correct versioning strategy.
gitVersion string = "devel"
// SHA1 from git, output of $(git rev-parse HEAD)
gitCommit = "unknown"
// short SHA1 from git, output of $(git rev-parse --short HEAD)
buildHash = "unknown"
// the most recent v* tag in the current branch (or its ancestors)
buildTagLatest = "unknown"
// the current commit's v* tag
buildTagCurrent = "unknown"
// State of git tree, either "clean" or "dirty"
gitTreeState = "unknown"
// Build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
Expand All @@ -33,7 +37,8 @@ func GetVersion() error {

type Info struct {
GitVersion string
GitCommit string
BuildHash string
BuildVersion string
GitTreeState string
BuildDate string
GoVersion string
Expand All @@ -44,9 +49,26 @@ type Info struct {
func VersionInfo() Info {
// These variables typically come from -ldflags settings and in
// their absence fallback to the global defaults set above.

// Create the semver version based on the state of the current commit or its branch.
// Use the first version we find.
var version string
tags := strings.Fields(buildTagCurrent)
for _, t := range tags {
if strings.HasPrefix(t, "v") {
version = t
break
}
}
if version == "" {
version = buildTagLatest + "+" + buildHash
}
version = strings.TrimPrefix(version, "v")

return Info{
GitVersion: gitVersion,
GitCommit: gitCommit,
BuildHash: buildHash,
BuildVersion: version,
GitTreeState: gitTreeState,
BuildDate: buildDate,
GoVersion: runtime.Version(),
Expand All @@ -61,7 +83,8 @@ func (i *Info) String() string {
w := tabwriter.NewWriter(&b, 0, 0, 2, ' ', 0)

fmt.Fprintf(w, "GitVersion:\t%s\n", i.GitVersion)
fmt.Fprintf(w, "GitCommit:\t%s\n", i.GitCommit)
fmt.Fprintf(w, "BuildHash:\t%s\n", i.BuildHash)
fmt.Fprintf(w, "BuildVersion:\t%s\n", i.BuildVersion)
fmt.Fprintf(w, "GitTreeState:\t%s\n", i.GitTreeState)
fmt.Fprintf(w, "BuildDate:\t%s\n", i.BuildDate)
fmt.Fprintf(w, "GoVersion:\t%s\n", i.GoVersion)
Expand Down
4 changes: 2 additions & 2 deletions server/android_notification_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (me *AndroidNotificationServer) SendNotification(msg *PushNotification) Pus
data["sender_id"] = msg.SenderID
data["sender_name"] = "Someone"
data["team_id"] = msg.TeamID
} else if pushType == PushTypeMessage || pushType == PushTypeSession {
} else if pushType == PushTypeMessage || pushType == PushTypeSession || pushType == PushTypeCalls {
data["team_id"] = msg.TeamID
data["sender_id"] = msg.SenderID
data["sender_name"] = msg.SenderName
Expand All @@ -162,7 +162,7 @@ func (me *AndroidNotificationServer) SendNotification(msg *PushNotification) Pus
ctx, cancel := context.WithTimeout(context.Background(), me.sendTimeout)
defer cancel()

me.logger.Infof("Sending android push notification for device=%v type=%v ackId=%v", me.AndroidPushSettings.Type, msg.Type, msg.AckID)
me.logger.Infof("Sending android push notification for device=%v type=%v ackId=%v", me.AndroidPushSettings.Type, pushType, msg.AckID)

start := time.Now()
_, err := me.client.Send(ctx, fcmMsg)
Expand Down
4 changes: 2 additions & 2 deletions server/apple_notification_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ func (me *AppleNotificationServer) SendNotification(msg *PushNotification) PushR
data.ContentAvailable()
} else {
switch msg.Type {
case PushTypeMessage, PushTypeSession:
case PushTypeMessage, PushTypeSession, PushTypeCalls:
data.Category(msg.Category)
data.Sound("default")
data.Custom("version", msg.Version)
data.MutableContent()
if msg.Type == PushTypeMessage {
if msg.Type == PushTypeMessage || msg.Type == PushTypeCalls {
data.ContentAvailable()
}

Expand Down
1 change: 1 addition & 0 deletions server/push_notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
PushTypeUpdateBadge = "update_badge"
PushTypeSession = "session"
PushTypeTest = "test"
PushTypeCalls = "calls"

PushMessageV2 = "v2"

Expand Down
17 changes: 17 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func (s *Server) Start() {
handler := th.Throttle(router)

router.HandleFunc("/", root).Methods("GET")
router.HandleFunc("/version", s.version).Methods("GET")

metricCompatibleSendNotificationHandler := s.handleSendNotification
metricCompatibleAckNotificationHandler := s.handleAckNotification
Expand Down Expand Up @@ -150,6 +151,22 @@ func root(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("<html><body>Mattermost Push Proxy</body></html>"))
}

func (s *Server) version(w http.ResponseWriter, _ *http.Request) {
info := version.VersionInfo()
ret := map[string]interface{}{
"version": info.BuildVersion,
"hash": info.BuildHash,
}

w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(ret); err != nil {
s.logger.Errorf("Failed to write response: %v", err)
if s.metrics != nil {
s.metrics.incrementBadRequest()
}
}
}

func (s *Server) responseTimeMiddleware(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
Expand Down

0 comments on commit f45900b

Please sign in to comment.