-
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.
- Loading branch information
Showing
5 changed files
with
96 additions
and
62 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,26 @@ | ||
mod create_expo_push_token; | ||
mod create_notifications; | ||
mod delete_expo_push_token; | ||
mod get_root; | ||
|
||
#[cfg(test)] | ||
pub(crate) use self::create_expo_push_token::CreateExpoPushTokenRequestBody; | ||
|
||
use std::{collections::HashSet, sync::Arc}; | ||
|
||
use axum::{ | ||
extract::{Path, State}, | ||
http::StatusCode, | ||
routing, Json, Router, | ||
}; | ||
use expo_push_notification_client::{Expo, ExpoClientOptions, ExpoPushMessage}; | ||
use axum::Router; | ||
use tokio::sync::Mutex; | ||
|
||
#[derive(Clone, Debug, Default)] | ||
pub struct App { | ||
pub expo_push_tokens: Arc<Mutex<HashSet<String>>>, | ||
expo_push_tokens: Arc<Mutex<HashSet<String>>>, | ||
} | ||
|
||
pub fn route(app: App) -> Router { | ||
Router::new() | ||
.route("/", routing::get(get_root)) | ||
.route("/expo_push_tokens", routing::post(create_expo_push_token)) | ||
.route( | ||
"/expo_push_tokens/:expo_push_token", | ||
routing::delete(delete_expo_push_token), | ||
) | ||
.route("/notifications", routing::post(create_notification)) | ||
.merge(create_expo_push_token::route()) | ||
.merge(create_notifications::route()) | ||
.merge(delete_expo_push_token::route()) | ||
.merge(get_root::route()) | ||
.with_state(app) | ||
} | ||
|
||
#[derive(Debug, serde::Deserialize, serde::Serialize)] | ||
pub struct CreateExpoPushTokenRequestBody { | ||
pub expo_push_token: String, | ||
} | ||
|
||
pub async fn create_expo_push_token( | ||
State(App { expo_push_tokens }): State<App>, | ||
Json(CreateExpoPushTokenRequestBody { expo_push_token }): Json<CreateExpoPushTokenRequestBody>, | ||
) -> StatusCode { | ||
let mut expo_push_tokens = expo_push_tokens.lock().await; | ||
// TODO: already exists | ||
expo_push_tokens.insert(expo_push_token); | ||
StatusCode::CREATED | ||
} | ||
|
||
pub async fn delete_expo_push_token( | ||
State(App { expo_push_tokens }): State<App>, | ||
Path(expo_push_token): Path<String>, | ||
) -> StatusCode { | ||
let mut expo_push_tokens = expo_push_tokens.lock().await; | ||
// TODO: not found | ||
expo_push_tokens.remove(&expo_push_token); | ||
StatusCode::NO_CONTENT | ||
} | ||
|
||
pub async fn create_notification( | ||
State(App { expo_push_tokens }): State<App>, | ||
) -> Result<StatusCode, StatusCode> { | ||
let expo_client = Expo::new(ExpoClientOptions { | ||
// TODO | ||
access_token: None, | ||
}); | ||
let expo_push_tokens = expo_push_tokens.lock().await; | ||
expo_client | ||
.send_push_notifications( | ||
ExpoPushMessage::builder(expo_push_tokens.clone()) | ||
.build() | ||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?, | ||
) | ||
.await | ||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; | ||
Ok(StatusCode::CREATED) | ||
} | ||
|
||
pub async fn get_root() -> &'static str { | ||
"OK" | ||
} |
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,22 @@ | ||
use axum::{extract::State, http::StatusCode, routing, Json, Router}; | ||
|
||
use super::App; | ||
|
||
pub fn route() -> Router<App> { | ||
Router::new().route("/expo_push_tokens", routing::post(handler)) | ||
} | ||
|
||
#[derive(Debug, serde::Deserialize, serde::Serialize)] | ||
pub struct CreateExpoPushTokenRequestBody { | ||
pub expo_push_token: String, | ||
} | ||
|
||
async fn handler( | ||
State(App { expo_push_tokens }): State<App>, | ||
Json(CreateExpoPushTokenRequestBody { expo_push_token }): Json<CreateExpoPushTokenRequestBody>, | ||
) -> StatusCode { | ||
let mut expo_push_tokens = expo_push_tokens.lock().await; | ||
// TODO: already exists | ||
expo_push_tokens.insert(expo_push_token); | ||
StatusCode::CREATED | ||
} |
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,25 @@ | ||
use axum::{extract::State, http::StatusCode, routing, Router}; | ||
use expo_push_notification_client::{Expo, ExpoClientOptions, ExpoPushMessage}; | ||
|
||
use super::App; | ||
|
||
pub fn route() -> Router<App> { | ||
Router::new().route("/notifications", routing::post(handler)) | ||
} | ||
|
||
async fn handler(State(App { expo_push_tokens }): State<App>) -> Result<StatusCode, StatusCode> { | ||
let expo_client = Expo::new(ExpoClientOptions { | ||
// TODO | ||
access_token: None, | ||
}); | ||
let expo_push_tokens = expo_push_tokens.lock().await; | ||
expo_client | ||
.send_push_notifications( | ||
ExpoPushMessage::builder(expo_push_tokens.clone()) | ||
.build() | ||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?, | ||
) | ||
.await | ||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; | ||
Ok(StatusCode::CREATED) | ||
} |
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,24 @@ | ||
use axum::{ | ||
extract::{Path, State}, | ||
http::StatusCode, | ||
routing, Router, | ||
}; | ||
|
||
use super::App; | ||
|
||
pub fn route() -> Router<App> { | ||
Router::new().route( | ||
"/expo_push_tokens/:expo_push_token", | ||
routing::delete(handler), | ||
) | ||
} | ||
|
||
async fn handler( | ||
State(App { expo_push_tokens }): State<App>, | ||
Path(expo_push_token): Path<String>, | ||
) -> StatusCode { | ||
let mut expo_push_tokens = expo_push_tokens.lock().await; | ||
// TODO: not found | ||
expo_push_tokens.remove(&expo_push_token); | ||
StatusCode::NO_CONTENT | ||
} |
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,11 @@ | ||
use axum::{routing, Router}; | ||
|
||
use super::App; | ||
|
||
async fn get_root() -> &'static str { | ||
"OK" | ||
} | ||
|
||
pub fn route() -> Router<App> { | ||
Router::new().route("/", routing::get(get_root)) | ||
} |