Skip to content

Commit b7f5d1f

Browse files
committed
Identity routes
1 parent a5ec537 commit b7f5d1f

File tree

3 files changed

+52
-14
lines changed

3 files changed

+52
-14
lines changed

crates/client-api/src/routes/identity.rs

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::time::Duration;
22

33
use axum::extract::{Path, State};
44
use axum::response::IntoResponse;
5+
use axum::routing::MethodRouter;
56
use http::header::CONTENT_TYPE;
67
use http::StatusCode;
78
use serde::{Deserialize, Serialize};
@@ -64,12 +65,12 @@ impl<'de> serde::Deserialize<'de> for IdentityForUrl {
6465

6566
#[derive(Deserialize)]
6667
pub struct GetDatabasesParams {
67-
identity: IdentityForUrl,
68+
pub identity: IdentityForUrl,
6869
}
6970

7071
#[derive(Debug, Clone, Serialize, Deserialize)]
7172
pub struct GetDatabasesResponse {
72-
identities: Vec<Identity>,
73+
pub identities: Vec<Identity>,
7374
}
7475

7576
pub async fn get_databases<S: ControlStateDelegate>(
@@ -135,15 +136,46 @@ pub async fn get_public_key<S: NodeDelegate>(State(ctx): State<S>) -> axum::resp
135136
))
136137
}
137138

138-
pub fn router<S>() -> axum::Router<S>
139+
/// A struct to allow customization of the `/identity` routes.
140+
pub struct IdentityRoutes<S> {
141+
/// POST /identity
142+
pub create_post: MethodRouter<S>,
143+
/// GET /identity/public-key
144+
pub public_key_get: MethodRouter<S>,
145+
/// POST /identity/websocket-tocken
146+
pub websocket_token_post: MethodRouter<S>,
147+
/// GET /identity/:identity/verify
148+
pub verify_get: MethodRouter<S>,
149+
/// GET /identity/:identity/databases
150+
pub databases_get: MethodRouter<S>,
151+
}
152+
153+
impl<S> Default for IdentityRoutes<S>
154+
where
155+
S: NodeDelegate + ControlStateDelegate + Clone + 'static,
156+
{
157+
fn default() -> Self {
158+
use axum::routing::{get, post};
159+
Self {
160+
create_post: post(create_identity::<S>),
161+
public_key_get: get(get_public_key::<S>),
162+
websocket_token_post: post(create_websocket_token::<S>),
163+
verify_get: get(validate_token),
164+
databases_get: get(get_databases::<S>),
165+
}
166+
}
167+
}
168+
169+
impl<S> IdentityRoutes<S>
139170
where
140171
S: NodeDelegate + ControlStateDelegate + Clone + 'static,
141172
{
142-
use axum::routing::{get, post};
143-
axum::Router::new()
144-
.route("/", post(create_identity::<S>))
145-
.route("/public-key", get(get_public_key::<S>))
146-
.route("/websocket-token", post(create_websocket_token::<S>))
147-
.route("/:identity/verify", get(validate_token))
148-
.route("/:identity/databases", get(get_databases::<S>))
173+
pub fn into_router(self) -> axum::Router<S> {
174+
axum::Router::new()
175+
.route("/", self.create_post)
176+
.route("/public-key", self.public_key_get)
177+
.route("/websocket-token", self.websocket_token_post)
178+
.route("/:identity/verify", self.verify_get)
179+
.route("/:identity/databases", self.databases_get)
180+
}
149181
}

crates/client-api/src/routes/mod.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,26 @@ pub mod metrics;
1212
pub mod prometheus;
1313
pub mod subscribe;
1414

15-
use self::database::DatabaseRoutes;
15+
use self::{database::DatabaseRoutes, identity::IdentityRoutes};
1616

1717
/// This API call is just designed to allow clients to determine whether or not they can
1818
/// establish a connection to SpacetimeDB. This API call doesn't actually do anything.
1919
pub async fn ping(_auth: crate::auth::SpacetimeAuthHeader) {}
2020

2121
#[allow(clippy::let_and_return)]
22-
pub fn router<S>(ctx: &S, database_routes: DatabaseRoutes<S>, extra: axum::Router<S>) -> axum::Router<S>
22+
pub fn router<S>(
23+
ctx: &S,
24+
database_routes: DatabaseRoutes<S>,
25+
identity_routes: IdentityRoutes<S>,
26+
extra: axum::Router<S>,
27+
) -> axum::Router<S>
2328
where
2429
S: NodeDelegate + ControlStateDelegate + Authorization + Clone + 'static,
2530
{
2631
use axum::routing::get;
2732
let router = axum::Router::new()
2833
.nest("/database", database_routes.into_router(ctx.clone()))
29-
.nest("/identity", identity::router())
34+
.nest("/identity", identity_routes.into_router())
3035
.nest("/energy", energy::router())
3136
.nest("/prometheus", prometheus::router())
3237
.nest("/metrics", metrics::router())

crates/standalone/src/subcommands/start.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use spacetimedb_client_api::routes::identity::IdentityRoutes;
12
use spacetimedb_pg::pg_server;
23
use std::sync::Arc;
34

@@ -185,7 +186,7 @@ pub async fn exec(args: &ArgMatches, db_cores: JobCores) -> anyhow::Result<()> {
185186
db_routes.db_put = db_routes.db_put.layer(DefaultBodyLimit::disable());
186187
db_routes.pre_publish = db_routes.pre_publish.layer(DefaultBodyLimit::disable());
187188
let extra = axum::Router::new().nest("/health", spacetimedb_client_api::routes::health::router());
188-
let service = router(&ctx, db_routes, extra).with_state(ctx.clone());
189+
let service = router(&ctx, db_routes, IdentityRoutes::default(), extra).with_state(ctx.clone());
189190

190191
let tcp = TcpListener::bind(listen_addr).await.context(format!(
191192
"failed to bind the SpacetimeDB server to '{listen_addr}', please check that the address is valid and not already in use"

0 commit comments

Comments
 (0)