-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[back-2425] Light weight APi #12
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking about this earlier. I believe we'll need to remove the version from the id and make it a separate field of the metadata. Otherwise it'll be really hard to query for an existing premint when we get an update. Not for this PR necessarily, just wanted to put it out there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good call out