mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-25 00:00:23 +02:00
Reorganize method routers for handlers and services (#405)
* Re-organize method routing for handlers * Re-organize method routing for services * changelog
This commit is contained in:
@@ -3,7 +3,7 @@ mod starwars;
|
||||
use async_graphql::http::{playground_source, GraphQLPlaygroundConfig};
|
||||
use async_graphql::{EmptyMutation, EmptySubscription, Request, Response, Schema};
|
||||
use axum::response::IntoResponse;
|
||||
use axum::{extract::Extension, handler::get, response::Html, AddExtensionLayer, Json, Router};
|
||||
use axum::{extract::Extension, response::Html, routing::get, AddExtensionLayer, Json, Router};
|
||||
use starwars::{QueryRoot, StarWars, StarWarsSchema};
|
||||
|
||||
async fn graphql_handler(schema: Extension<StarWarsSchema>, req: Json<Request>) -> Json<Response> {
|
||||
|
||||
@@ -11,8 +11,8 @@ use axum::{
|
||||
ws::{Message, WebSocket, WebSocketUpgrade},
|
||||
Extension,
|
||||
},
|
||||
handler::get,
|
||||
response::{Html, IntoResponse},
|
||||
routing::get,
|
||||
AddExtensionLayer, Router,
|
||||
};
|
||||
use futures::{sink::SinkExt, stream::StreamExt};
|
||||
|
||||
@@ -8,8 +8,8 @@ use axum::{
|
||||
async_trait,
|
||||
extract::rejection::JsonRejection,
|
||||
extract::{FromRequest, RequestParts},
|
||||
handler::post,
|
||||
http::StatusCode,
|
||||
routing::post,
|
||||
BoxError, Router,
|
||||
};
|
||||
use serde::{de::DeserializeOwned, Deserialize};
|
||||
|
||||
@@ -11,9 +11,9 @@ use axum::{
|
||||
async_trait,
|
||||
body::{Bytes, Full},
|
||||
extract::{Extension, Path},
|
||||
handler::{get, post},
|
||||
http::{Response, StatusCode},
|
||||
response::IntoResponse,
|
||||
routing::{get, post},
|
||||
AddExtensionLayer, Json, Router,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
//! cargo run -p example-form
|
||||
//! ```
|
||||
|
||||
use axum::{extract::Form, handler::get, response::Html, Router};
|
||||
use axum::{extract::Form, response::Html, routing::get, Router};
|
||||
use serde::Deserialize;
|
||||
use std::net::SocketAddr;
|
||||
|
||||
|
||||
@@ -5,9 +5,10 @@
|
||||
//! ```
|
||||
|
||||
use axum::{
|
||||
handler::{get, Handler},
|
||||
handler::Handler,
|
||||
http::StatusCode,
|
||||
response::{Html, IntoResponse},
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
use std::net::SocketAddr;
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
//! kill or ctrl-c
|
||||
//! ```
|
||||
|
||||
use axum::{handler::get, response::Html, Router};
|
||||
use axum::{response::Html, routing::get, Router};
|
||||
use std::net::SocketAddr;
|
||||
|
||||
#[tokio::main]
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
//! cargo run -p example-hello-world
|
||||
//! ```
|
||||
|
||||
use axum::{handler::get, response::Html, Router};
|
||||
use axum::{response::Html, routing::get, Router};
|
||||
use std::net::SocketAddr;
|
||||
|
||||
#[tokio::main]
|
||||
|
||||
@@ -10,9 +10,9 @@ use axum::{
|
||||
async_trait,
|
||||
body::{Bytes, Full},
|
||||
extract::{FromRequest, RequestParts, TypedHeader},
|
||||
handler::{get, post},
|
||||
http::{Response, StatusCode},
|
||||
response::IntoResponse,
|
||||
routing::{get, post},
|
||||
Json, Router,
|
||||
};
|
||||
use headers::{authorization::Bearer, Authorization};
|
||||
|
||||
@@ -10,9 +10,10 @@ use axum::{
|
||||
body::Bytes,
|
||||
error_handling::HandleErrorLayer,
|
||||
extract::{ContentLengthLimit, Extension, Path},
|
||||
handler::{delete, get, Handler},
|
||||
handler::Handler,
|
||||
http::StatusCode,
|
||||
response::IntoResponse,
|
||||
routing::{delete, get},
|
||||
Router,
|
||||
};
|
||||
use std::{
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
//! cargo run -p example-low-level-rustls
|
||||
//! ```
|
||||
|
||||
use axum::{handler::get, Router};
|
||||
use axum::{routing::get, Router};
|
||||
use hyper::server::conn::Http;
|
||||
use std::{fs::File, io::BufReader, sync::Arc};
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
|
||||
use axum::{
|
||||
extract::{ContentLengthLimit, Multipart},
|
||||
handler::get,
|
||||
response::Html,
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
use std::net::SocketAddr;
|
||||
|
||||
@@ -11,9 +11,9 @@ use axum::{
|
||||
async_trait,
|
||||
body::{Bytes, Empty},
|
||||
extract::{Extension, FromRequest, Query, RequestParts, TypedHeader},
|
||||
handler::get,
|
||||
http::{header::SET_COOKIE, HeaderMap, Response},
|
||||
response::{IntoResponse, Redirect},
|
||||
routing::get,
|
||||
AddExtensionLayer, Router,
|
||||
};
|
||||
use oauth2::{
|
||||
|
||||
@@ -7,8 +7,8 @@
|
||||
use axum::{
|
||||
body::{Body, BoxBody, Bytes},
|
||||
error_handling::HandleErrorLayer,
|
||||
handler::post,
|
||||
http::{Request, Response, StatusCode},
|
||||
routing::post,
|
||||
Router,
|
||||
};
|
||||
use std::net::SocketAddr;
|
||||
|
||||
@@ -9,8 +9,8 @@
|
||||
|
||||
use axum::{
|
||||
extract::Extension,
|
||||
handler::get,
|
||||
http::{uri::Uri, Request, Response},
|
||||
routing::get,
|
||||
AddExtensionLayer, Router,
|
||||
};
|
||||
use hyper::{client::HttpConnector, Body};
|
||||
|
||||
@@ -8,13 +8,13 @@ use async_session::{MemoryStore, Session, SessionStore as _};
|
||||
use axum::{
|
||||
async_trait,
|
||||
extract::{Extension, FromRequest, RequestParts},
|
||||
handler::get,
|
||||
http::{
|
||||
self,
|
||||
header::{HeaderMap, HeaderValue},
|
||||
StatusCode,
|
||||
},
|
||||
response::IntoResponse,
|
||||
routing::get,
|
||||
AddExtensionLayer, Router,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@@ -7,9 +7,9 @@
|
||||
use axum::{
|
||||
error_handling::HandleErrorExt,
|
||||
extract::TypedHeader,
|
||||
handler::get,
|
||||
http::StatusCode,
|
||||
response::sse::{Event, Sse},
|
||||
routing::{get, service_method_router as service},
|
||||
Router,
|
||||
};
|
||||
use futures::stream::{self, Stream};
|
||||
@@ -25,15 +25,14 @@ async fn main() {
|
||||
}
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
let static_files_service = axum::service::get(
|
||||
ServeDir::new("examples/sse/assets").append_index_html_on_directories(true),
|
||||
)
|
||||
.handle_error(|error: std::io::Error| {
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("Unhandled internal error: {}", error),
|
||||
)
|
||||
});
|
||||
let static_files_service =
|
||||
service::get(ServeDir::new("examples/sse/assets").append_index_html_on_directories(true))
|
||||
.handle_error(|error: std::io::Error| {
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("Unhandled internal error: {}", error),
|
||||
)
|
||||
});
|
||||
|
||||
// build our application with a route
|
||||
let app = Router::new()
|
||||
|
||||
@@ -4,7 +4,10 @@
|
||||
//! cargo run -p example-static-file-server
|
||||
//! ```
|
||||
|
||||
use axum::{error_handling::HandleErrorExt, http::StatusCode, service, Router};
|
||||
use axum::{
|
||||
error_handling::HandleErrorExt, http::StatusCode, routing::service_method_router as service,
|
||||
Router,
|
||||
};
|
||||
use std::net::SocketAddr;
|
||||
use tower_http::{services::ServeDir, trace::TraceLayer};
|
||||
|
||||
|
||||
@@ -8,9 +8,9 @@ use askama::Template;
|
||||
use axum::{
|
||||
body::{Bytes, Full},
|
||||
extract,
|
||||
handler::get,
|
||||
http::{Response, StatusCode},
|
||||
response::{Html, IntoResponse},
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
use std::{convert::Infallible, net::SocketAddr};
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
//! ```
|
||||
|
||||
use axum::{
|
||||
handler::{get, post},
|
||||
routing::{get, post},
|
||||
Json, Router,
|
||||
};
|
||||
use tower_http::trace::TraceLayer;
|
||||
|
||||
@@ -16,9 +16,9 @@
|
||||
use axum::{
|
||||
error_handling::HandleErrorLayer,
|
||||
extract::{Extension, Path, Query},
|
||||
handler::{get, patch},
|
||||
http::StatusCode,
|
||||
response::IntoResponse,
|
||||
routing::{get, patch},
|
||||
Json, Router,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@@ -7,8 +7,8 @@
|
||||
use axum::{
|
||||
async_trait,
|
||||
extract::{Extension, FromRequest, RequestParts},
|
||||
handler::get,
|
||||
http::StatusCode,
|
||||
routing::get,
|
||||
AddExtensionLayer, Router,
|
||||
};
|
||||
use bb8::{Pool, PooledConnection};
|
||||
|
||||
@@ -6,9 +6,9 @@
|
||||
|
||||
use axum::{
|
||||
body::Bytes,
|
||||
handler::get,
|
||||
http::{HeaderMap, Request, Response},
|
||||
response::Html,
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
use std::{net::SocketAddr, time::Duration};
|
||||
|
||||
@@ -7,8 +7,8 @@
|
||||
use axum::{
|
||||
body::Body,
|
||||
extract::connect_info::{self, ConnectInfo},
|
||||
handler::get,
|
||||
http::{Method, Request, StatusCode, Uri},
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
use futures::ready;
|
||||
|
||||
@@ -14,9 +14,9 @@ use async_trait::async_trait;
|
||||
use axum::{
|
||||
body::{Bytes, Full},
|
||||
extract::{Form, FromRequest, RequestParts},
|
||||
handler::get,
|
||||
http::{Response, StatusCode},
|
||||
response::{Html, IntoResponse},
|
||||
routing::get,
|
||||
BoxError, Router,
|
||||
};
|
||||
use serde::{de::DeserializeOwned, Deserialize};
|
||||
|
||||
@@ -8,9 +8,9 @@ use axum::{
|
||||
async_trait,
|
||||
body::{Bytes, Full},
|
||||
extract::{FromRequest, Path, RequestParts},
|
||||
handler::get,
|
||||
http::{Response, StatusCode},
|
||||
response::IntoResponse,
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
|
||||
@@ -12,9 +12,9 @@ use axum::{
|
||||
ws::{Message, WebSocket, WebSocketUpgrade},
|
||||
TypedHeader,
|
||||
},
|
||||
handler::get,
|
||||
http::StatusCode,
|
||||
response::IntoResponse,
|
||||
routing::{get, service_method_router as service},
|
||||
Router,
|
||||
};
|
||||
use std::net::SocketAddr;
|
||||
@@ -35,7 +35,7 @@ async fn main() {
|
||||
let app = Router::new()
|
||||
.nest(
|
||||
"/",
|
||||
axum::service::get(
|
||||
service::get(
|
||||
ServeDir::new("examples/websockets/assets").append_index_html_on_directories(true),
|
||||
)
|
||||
.handle_error(|error: std::io::Error| {
|
||||
|
||||
Reference in New Issue
Block a user