-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from amiller68/board-stream
streaming bones
- Loading branch information
Showing
22 changed files
with
344 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use askama::Template; | ||
use axum::{ | ||
extract::{Path, State}, | ||
response::{IntoResponse, Response}, | ||
}; | ||
use sqlx::types::Uuid; | ||
|
||
use crate::api::models::ApiGameBoard; | ||
use crate::database::models::{Game, GameBoard, GameError}; | ||
use crate::AppState; | ||
|
||
pub async fn handler( | ||
State(state): State<AppState>, | ||
Path(game_id): Path<Uuid>, | ||
) -> Result<impl IntoResponse, ReadBoardError> { | ||
let mut conn = state.database().acquire().await?; | ||
if !Game::exists(&mut conn, game_id).await? { | ||
return Err(ReadBoardError::NotFound); | ||
} | ||
|
||
let game_board = GameBoard::latest(&mut conn, game_id).await?; | ||
|
||
tracing::info!("read game board: {:?}", game_board); | ||
|
||
let api_game_board = ApiGameBoard::from(game_board); | ||
|
||
Ok(TemplateApiGameBoard { api_game_board }) | ||
} | ||
|
||
#[derive(Template)] | ||
#[template(path = "game_board.html")] | ||
struct TemplateApiGameBoard { | ||
api_game_board: ApiGameBoard, | ||
} | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
pub enum ReadBoardError { | ||
#[error("sqlx error: {0}")] | ||
Sqlx(#[from] sqlx::Error), | ||
#[error("game error: {0}")] | ||
Game(#[from] GameError), | ||
#[error("game not found")] | ||
NotFound, | ||
} | ||
|
||
impl IntoResponse for ReadBoardError { | ||
fn into_response(self) -> Response { | ||
match self { | ||
ReadBoardError::NotFound => { | ||
let body = format!("{}", self); | ||
(axum::http::StatusCode::NOT_FOUND, body).into_response() | ||
} | ||
_ => { | ||
let body = format!("{}", self); | ||
(axum::http::StatusCode::INTERNAL_SERVER_ERROR, body).into_response() | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use std::convert::Infallible; | ||
use std::time::Duration; | ||
|
||
use axum::{ | ||
extract::Path, | ||
response::{sse::Event, Sse}, | ||
Extension, | ||
}; | ||
use serde::Serialize; | ||
use sqlx::types::Uuid; | ||
use tokio::sync::broadcast::Sender; | ||
use tokio_stream::wrappers::BroadcastStream; | ||
use tokio_stream::{Stream, StreamExt as _}; | ||
|
||
pub type GameUpdateStream = Sender<GameUpdate>; | ||
|
||
#[derive(Clone, Serialize, Debug)] | ||
pub struct GameUpdate; | ||
|
||
pub async fn handler( | ||
Path(game_id): Path<Uuid>, | ||
Extension(tx): Extension<GameUpdateStream>, | ||
) -> Sse<impl Stream<Item = Result<Event, Infallible>>> { | ||
let rx = tx.subscribe(); | ||
|
||
let stream = BroadcastStream::new(rx); | ||
|
||
// Catch all updata events for this game | ||
Sse::new( | ||
stream | ||
.map(move |_| Event::default().event(format!("game-update-{}", game_id))) | ||
.map(Ok), | ||
) | ||
.keep_alive( | ||
axum::response::sse::KeepAlive::new() | ||
.interval(Duration::from_secs(60)) | ||
.text("keep-alive-text"), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
pub mod games; | ||
mod models; | ||
pub mod models; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
mod api_board; | ||
mod api_game; | ||
mod api_game_board; | ||
mod api_game_item; | ||
|
||
pub use api_board::ApiGameBoard; | ||
pub use api_game::ApiGame; | ||
pub use api_game_board::ApiGameBoard; | ||
pub use api_game_item::ApiGameItem; |
Oops, something went wrong.