-
Notifications
You must be signed in to change notification settings - Fork 2
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 #12 from ourzora/erik/back-2425-api
[back-2425] Light weight APi
- Loading branch information
Showing
9 changed files
with
331 additions
and
158 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,73 @@ | ||
use crate::config::Config; | ||
use crate::controller::{ControllerCommands, ControllerInterface, DBQuery}; | ||
use crate::storage; | ||
use crate::types::PremintTypes; | ||
use axum::extract::State; | ||
use axum::handler::Handler; | ||
use axum::http::StatusCode; | ||
use axum::routing::{get, post}; | ||
use axum::{Json, Router}; | ||
use sqlx::SqlitePool; | ||
use tokio::net::TcpListener; | ||
|
||
#[derive(Clone)] | ||
pub struct AppState { | ||
pub db: SqlitePool, | ||
pub controller: ControllerInterface, | ||
} | ||
pub async fn make_router(config: &Config, controller: ControllerInterface) -> Router { | ||
let (snd, recv) = tokio::sync::oneshot::channel(); | ||
controller | ||
.send_command(ControllerCommands::Query(DBQuery::Direct(snd))) | ||
.await | ||
.unwrap(); | ||
let db = recv.await.unwrap().expect("Failed to get db"); | ||
Router::new() | ||
.route("/health", get(health)) | ||
.route("/list-all", get(list_all)) | ||
.route("/submit-premint", post(submit_premint)) | ||
.with_state(AppState { db, controller }) | ||
} | ||
|
||
pub async fn start_api(config: &Config, router: Router) -> eyre::Result<()> { | ||
let addr = format!("{}:{}", config.initial_network_ip(), config.api_port); | ||
let listener = TcpListener::bind(addr.clone()).await.unwrap(); | ||
|
||
tracing::info!(address = addr, "Starting API server"); | ||
tokio::spawn(async move { | ||
axum::serve(listener, router) | ||
.await | ||
.expect("API Server failed"); | ||
}); | ||
Ok(()) | ||
} | ||
|
||
async fn list_all( | ||
State(state): State<AppState>, | ||
) -> Result<Json<Vec<PremintTypes>>, (StatusCode, String)> { | ||
match storage::list_all(&state.db).await { | ||
Ok(premints) => Ok(Json(premints)), | ||
Err(_e) => Err(( | ||
StatusCode::INTERNAL_SERVER_ERROR, | ||
"Failed to list all premints".to_string(), | ||
)), | ||
} | ||
} | ||
|
||
async fn health() -> &'static str { | ||
"OK" | ||
} | ||
|
||
async fn submit_premint( | ||
State(state): State<AppState>, | ||
Json(premint): Json<PremintTypes>, | ||
) -> (StatusCode, String) { | ||
match state | ||
.controller | ||
.send_command(ControllerCommands::Broadcast { message: premint }) | ||
.await | ||
{ | ||
Ok(()) => (StatusCode::OK, "Premint submitted".to_string()), | ||
Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()), | ||
} | ||
} |
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,3 +1,4 @@ | ||
pub mod api; | ||
pub mod chain; | ||
pub mod config; | ||
pub mod controller; | ||
|
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