Skip to content

Commit

Permalink
fix: add handler for new attachments (#1185)
Browse files Browse the repository at this point in the history
  • Loading branch information
matteojug authored Jan 7, 2025
1 parent 8b81f26 commit 83b316c
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 16 deletions.
29 changes: 22 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions signer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ test-case = "3.1"
test-log = { version = "0.2.16", default-features = false, features = ["trace"] }
toml_edit = "0.22.22"
tempfile = "3.6"
tower = { version = "0.5.2", features = ["util"] }

[build-dependencies]
tonic-build.workspace = true
Expand Down
7 changes: 4 additions & 3 deletions signer/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//! This module contains functions and structs for the Signer API.
//!
pub mod new_block;
pub mod status;
mod new_block;
mod router;
mod status;

pub use new_block::new_block_handler;
pub use status::status_handler;
pub use router::get_router;

/// A struct with state data necessary for runtime operation.
#[derive(Debug, Clone)]
Expand Down
59 changes: 59 additions & 0 deletions signer/src/api/router.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//! This module contains the default router for the signers api
//!
use axum::{
routing::{get, post},
Router,
};

use crate::context::Context;

use axum::http::StatusCode;

use super::{new_block, status, ApiState};

async fn new_attachment_handler() -> StatusCode {
StatusCode::OK
}

/// Return the default router
pub fn get_router<C: Context + 'static>() -> Router<ApiState<C>> {
Router::new()
.route("/", get(status::status_handler))
.route("/new_block", post(new_block::new_block_handler))
// TODO: remove this once https://github.com/stacks-network/stacks-core/issues/5558
// is addressed
.route("/attachments/new", post(new_attachment_handler))
}

#[cfg(test)]
mod tests {
use axum::{
body::Body,
http::{Method, Request, StatusCode},
Router,
};
use tower::ServiceExt;

use crate::{
api::{router::get_router, ApiState},
testing::context::TestContext,
};

#[tokio::test]
async fn test_new_attachment() {
let context = TestContext::default_mocked();

let state = ApiState { ctx: context.clone() };
let app: Router = get_router().with_state(state);

let request = Request::builder()
.uri("/attachments/new")
.method(Method::POST)
.body(Body::empty())
.unwrap();

let response = app.oneshot(request).await.unwrap();
assert_eq!(response.status(), StatusCode::OK);
}
}
7 changes: 1 addition & 6 deletions signer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ use std::time::Duration;

use axum::http::Request;
use axum::http::Response;
use axum::routing::get;
use axum::routing::post;
use axum::Router;
use cfg_if::cfg_if;
use clap::Parser;
use clap::ValueEnum;
Expand Down Expand Up @@ -247,9 +244,7 @@ async fn run_api(ctx: impl Context + 'static) -> Result<(), Error> {
let request_id = Arc::new(AtomicU64::new(0));

// Build the signer API application
let app = Router::new()
.route("/", get(api::status_handler))
.route("/new_block", post(api::new_block_handler))
let app = api::get_router()
.layer(
TraceLayer::new_for_http()
.make_span_with(|request: &Request<_>| {
Expand Down

0 comments on commit 83b316c

Please sign in to comment.