From 22348bfa35f009e62abe2f30863e0434079a1fe2 Mon Sep 17 00:00:00 2001 From: Owen Jacobson Date: Wed, 18 Sep 2024 22:49:38 -0400 Subject: Remove the HTML client, and expose a JSON API. This API structure fell out of a conversation with Kit. Described loosely: kit: ok kit: Here's what I'm picturing in a client kit: list channels, make-new-channel, zero to one active channels, post-to-active. kit: login/sign-up, logout owen: you will likely also want "am I logged in" here kit: sure, whoami --- src/channel/routes.rs | 68 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 53 insertions(+), 15 deletions(-) (limited to 'src/channel/routes.rs') diff --git a/src/channel/routes.rs b/src/channel/routes.rs index 847e0b4..383ec58 100644 --- a/src/channel/routes.rs +++ b/src/channel/routes.rs @@ -1,23 +1,43 @@ use axum::{ - extract::{Form, Path, State}, + extract::{Json, Path, State}, http::StatusCode, - response::{IntoResponse, Redirect, Response}, - routing::post, + response::{IntoResponse, Response}, + routing::{get, post}, Router, }; -use super::app::EventsError; +use super::app::{self, EventsError}; use crate::{ app::App, clock::RequestedAt, error::InternalError, - repo::{channel, login::Login}, + repo::{ + channel::{self, Channel}, + login::Login, + }, }; pub fn router() -> Router { Router::new() - .route("/create", post(on_create)) - .route("/:channel/send", post(on_send)) + .route("/api/channels", get(list_channels)) + .route("/api/channels", post(on_create)) + .route("/api/channels/:channel", post(on_send)) +} + +async fn list_channels(State(app): State, _: Login) -> Result { + let channels = app.channels().all().await?; + let response = Channels(channels); + + Ok(response) +} + +struct Channels(Vec); + +impl IntoResponse for Channels { + fn into_response(self) -> Response { + let Self(channels) = self; + Json(channels).into_response() + } } #[derive(serde::Deserialize)] @@ -28,11 +48,29 @@ struct CreateRequest { async fn on_create( State(app): State, _: Login, // requires auth, but doesn't actually care who you are - Form(form): Form, -) -> Result { - app.channels().create(&form.name).await?; + Json(form): Json, +) -> Result, CreateError> { + let channel = app + .channels() + .create(&form.name) + .await + .map_err(CreateError)?; - Ok(Redirect::to("/")) + Ok(Json(channel)) +} + +struct CreateError(app::CreateError); + +impl IntoResponse for CreateError { + fn into_response(self) -> Response { + let Self(error) = self; + match error { + duplicate @ app::CreateError::DuplicateName(_) => { + (StatusCode::BAD_REQUEST, duplicate.to_string()).into_response() + } + other => InternalError::from(other).into_response(), + } + } } #[derive(serde::Deserialize)] @@ -45,15 +83,15 @@ async fn on_send( RequestedAt(sent_at): RequestedAt, State(app): State, login: Login, - Form(form): Form, -) -> Result { + Json(form): Json, +) -> Result { app.channels() .send(&login, &channel, &form.message, &sent_at) .await // Could impl `From` here, but it's more code and this is used once. .map_err(ErrorResponse)?; - Ok(Redirect::to(&format!("/{}", channel))) + Ok(StatusCode::ACCEPTED) } struct ErrorResponse(EventsError); @@ -65,7 +103,7 @@ impl IntoResponse for ErrorResponse { not_found @ EventsError::ChannelNotFound(_) => { (StatusCode::NOT_FOUND, not_found.to_string()).into_response() } - EventsError::DatabaseError(error) => InternalError::from(error).into_response(), + other => InternalError::from(other).into_response(), } } } -- cgit v1.2.3