Skip to content

Commit 0a5d048

Browse files
committed
Admin API to get the version of the service
1 parent 377ef1d commit 0a5d048

File tree

3 files changed

+105
-1
lines changed

3 files changed

+105
-1
lines changed

crates/handlers/src/admin/v1/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use aide::axum::{
1111
routing::{get_with, post_with},
1212
};
1313
use axum::extract::{FromRef, FromRequestParts};
14-
use mas_data_model::{BoxRng, SiteConfig};
14+
use mas_data_model::{AppVersion, BoxRng, SiteConfig};
1515
use mas_matrix::HomeserverConnection;
1616
use mas_policy::PolicyFactory;
1717

@@ -28,13 +28,15 @@ mod user_emails;
2828
mod user_registration_tokens;
2929
mod user_sessions;
3030
mod users;
31+
mod version;
3132

3233
pub fn router<S>() -> ApiRouter<S>
3334
where
3435
S: Clone + Send + Sync + 'static,
3536
Arc<dyn HomeserverConnection>: FromRef<S>,
3637
PasswordManager: FromRef<S>,
3738
SiteConfig: FromRef<S>,
39+
AppVersion: FromRef<S>,
3840
Arc<PolicyFactory>: FromRef<S>,
3941
BoxRng: FromRequestParts<S>,
4042
CallContext: FromRequestParts<S>,
@@ -44,6 +46,10 @@ where
4446
"/site-config",
4547
get_with(self::site_config::handler, self::site_config::doc),
4648
)
49+
.api_route(
50+
"/version",
51+
get_with(self::version::handler, self::version::doc),
52+
)
4753
.api_route(
4854
"/compat-sessions",
4955
get_with(self::compat_sessions::list, self::compat_sessions::list_doc),
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2025 New Vector Ltd.
2+
//
3+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
4+
// Please see LICENSE files in the repository root for full details.
5+
6+
use aide::transform::TransformOperation;
7+
use axum::{Json, extract::State};
8+
use mas_data_model::AppVersion;
9+
use schemars::JsonSchema;
10+
use serde::Serialize;
11+
12+
use crate::admin::call_context::CallContext;
13+
14+
#[derive(Serialize, JsonSchema)]
15+
pub struct Version {
16+
/// The semver version of the app
17+
pub version: &'static str,
18+
}
19+
20+
pub fn doc(operation: TransformOperation) -> TransformOperation {
21+
operation
22+
.id("version")
23+
.tag("server")
24+
.summary("Get the version currently running")
25+
.response_with::<200, Json<Version>, _>(|t| t.example(Version { version: "v1.0.0" }))
26+
}
27+
28+
#[tracing::instrument(name = "handler.admin.v1.version", skip_all)]
29+
pub async fn handler(
30+
_: CallContext,
31+
State(AppVersion(version)): State<mas_data_model::AppVersion>,
32+
) -> Json<Version> {
33+
Json(Version { version })
34+
}
35+
36+
#[cfg(test)]
37+
mod tests {
38+
use hyper::{Request, StatusCode};
39+
use insta::assert_json_snapshot;
40+
use sqlx::PgPool;
41+
42+
use crate::test_utils::{RequestBuilderExt, ResponseExt, TestState, setup};
43+
44+
#[sqlx::test(migrator = "mas_storage_pg::MIGRATOR")]
45+
async fn test_add_user(pool: PgPool) {
46+
setup();
47+
let mut state = TestState::from_pool(pool).await.unwrap();
48+
let token = state.token_with_scope("urn:mas:admin").await;
49+
50+
let request = Request::get("/api/admin/v1/version").bearer(&token).empty();
51+
52+
let response = state.request(request).await;
53+
54+
assert_eq!(response.status(), StatusCode::OK);
55+
let body: serde_json::Value = response.json();
56+
assert_json_snapshot!(body, @r#"
57+
{
58+
"version": "v0.0.0-test"
59+
}
60+
"#);
61+
}
62+
}

docs/api/spec.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,30 @@
5050
}
5151
}
5252
},
53+
"/api/admin/v1/version": {
54+
"get": {
55+
"tags": [
56+
"server"
57+
],
58+
"summary": "Get the version currently running",
59+
"operationId": "version",
60+
"responses": {
61+
"200": {
62+
"description": "",
63+
"content": {
64+
"application/json": {
65+
"schema": {
66+
"$ref": "#/components/schemas/Version"
67+
},
68+
"example": {
69+
"version": "v1.0.0"
70+
}
71+
}
72+
}
73+
}
74+
}
75+
}
76+
},
5377
"/api/admin/v1/compat-sessions": {
5478
"get": {
5579
"tags": [
@@ -3710,6 +3734,18 @@
37103734
}
37113735
}
37123736
},
3737+
"Version": {
3738+
"type": "object",
3739+
"required": [
3740+
"version"
3741+
],
3742+
"properties": {
3743+
"version": {
3744+
"description": "The semver version of the app",
3745+
"type": "string"
3746+
}
3747+
}
3748+
},
37133749
"PaginationParams": {
37143750
"type": "object",
37153751
"properties": {

0 commit comments

Comments
 (0)