From fe98ff4d1e80c384aeb8415d9dd8a10af7ea45bd Mon Sep 17 00:00:00 2001
From: Travis Ralston <travpc@gmail.com>
Date: Sat, 28 Dec 2019 20:37:21 -0700
Subject: [PATCH] Add /_matrix/media/version for identifying the version of the
 repo

---
 api/custom/version.go      | 18 ++++++++++++++++++
 api/webserver/webserver.go |  4 ++++
 2 files changed, 22 insertions(+)
 create mode 100644 api/custom/version.go

diff --git a/api/custom/version.go b/api/custom/version.go
new file mode 100644
index 00000000..5715f78e
--- /dev/null
+++ b/api/custom/version.go
@@ -0,0 +1,18 @@
+package custom
+
+import (
+	"net/http"
+
+	"github.com/turt2live/matrix-media-repo/api"
+	"github.com/turt2live/matrix-media-repo/common/rcontext"
+	"github.com/turt2live/matrix-media-repo/common/version"
+)
+
+func GetVersion(r *http.Request, rctx rcontext.RequestContext, user api.UserInfo) interface{} {
+	return &api.DoNotCacheResponse{
+		Payload: map[string]string{
+			"Version":   version.Version,
+			"GitCommit": version.GitCommit,
+		},
+	}
+}
diff --git a/api/webserver/webserver.go b/api/webserver/webserver.go
index b54cb2a8..a657c4e3 100644
--- a/api/webserver/webserver.go
+++ b/api/webserver/webserver.go
@@ -71,10 +71,14 @@ func Init() *sync.WaitGroup {
 	startImportHandler := handler{api.RepoAdminRoute(custom.StartImport), "start_import", counter, false}
 	appendToImportHandler := handler{api.RepoAdminRoute(custom.AppendToImport), "append_to_import", counter, false}
 	stopImportHandler := handler{api.RepoAdminRoute(custom.StopImport), "stop_import", counter, false}
+	versionHandler := handler{api.AccessTokenOptionalRoute(custom.GetVersion), "get_version", counter, false}
 
 	routes := make(map[string]route)
 	versions := []string{"r0", "v1", "unstable"} // r0 is typically clients and v1 is typically servers. v1 is deprecated.
 
+	// Things that don't need a version
+	routes["/_matrix/media/version"] = route{"GET", versionHandler}
+
 	for _, version := range versions {
 		// Standard routes we have to handle
 		routes["/_matrix/media/"+version+"/upload"] = route{"POST", uploadHandler}
-- 
GitLab