put router definition into own function
This commit is contained in:
parent
897309a3b9
commit
425ec50a5f
1 changed files with 31 additions and 27 deletions
58
src/main.rs
58
src/main.rs
|
|
@ -167,6 +167,36 @@ fn do_migrations(conn: &mut Connection) -> rusqlite::Result<()> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn app(conn: Connection) -> axum::Router {
|
||||
Router::new()
|
||||
.route("/", get(root))
|
||||
.route("/increase/{id}", post(increase))
|
||||
.route("/decrease/{id}", post(decrease))
|
||||
.route("/set/{id}/to/{amount}", post(set))
|
||||
.layer(
|
||||
TraceLayer::new_for_http().make_span_with(|request: &Request<_>| {
|
||||
let matched_path = request
|
||||
.extensions()
|
||||
.get::<MatchedPath>()
|
||||
.map(MatchedPath::as_str);
|
||||
let request_id = request
|
||||
.extensions()
|
||||
.get::<RequestId>()
|
||||
.map(ToString::to_string)
|
||||
.unwrap_or_else(|| "unknown".into());
|
||||
info_span!(
|
||||
"request",
|
||||
method = ?request.method(),
|
||||
matched_path,
|
||||
uri = ?request.uri(),
|
||||
id = %request_id,
|
||||
)
|
||||
}),
|
||||
)
|
||||
.layer(RequestIdLayer)
|
||||
.with_state(Arc::new(Mutex::new(conn)))
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), std::io::Error> {
|
||||
tracing_subscriber::registry()
|
||||
|
|
@ -197,33 +227,7 @@ async fn main() -> Result<(), std::io::Error> {
|
|||
|
||||
PreparedStatements::check(&conn).expect("failed to prepare sql statements");
|
||||
|
||||
let app = Router::new()
|
||||
.route("/", get(root))
|
||||
.route("/increase/{id}", post(increase))
|
||||
.route("/decrease/{id}", post(decrease))
|
||||
.route("/set/{id}/to/{amount}", post(set))
|
||||
.layer(
|
||||
TraceLayer::new_for_http().make_span_with(|request: &Request<_>| {
|
||||
let matched_path = request
|
||||
.extensions()
|
||||
.get::<MatchedPath>()
|
||||
.map(MatchedPath::as_str);
|
||||
let request_id = request
|
||||
.extensions()
|
||||
.get::<RequestId>()
|
||||
.map(ToString::to_string)
|
||||
.unwrap_or_else(|| "unknown".into());
|
||||
info_span!(
|
||||
"request",
|
||||
method = ?request.method(),
|
||||
matched_path,
|
||||
uri = ?request.uri(),
|
||||
id = %request_id,
|
||||
)
|
||||
}),
|
||||
)
|
||||
.layer(RequestIdLayer)
|
||||
.with_state(Arc::new(Mutex::new(conn)));
|
||||
let app = app(conn);
|
||||
|
||||
let address = "0.0.0.0:3001";
|
||||
let listener = tokio::net::TcpListener::bind(address)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue