pub struct Response {
pub status: u16,
pub body: String,
pub headers: HashMap<String, String>,
}Fields§
§status: u16§body: String§headers: HashMap<String, String>Implementations§
Source§impl Response
impl Response
pub fn new(status: u16) -> Response
pub fn status(&mut self, status: u16) -> &mut Self
pub fn body<T: AsRef<str>>(&mut self, body: T) -> &mut Self
pub fn header<K: AsRef<str>, V: AsRef<str>>( &mut self, name: K, value: V, ) -> &mut Self
pub fn headers(&mut self, headers: HashMap<String, String>) -> &mut Self
Sourcepub fn ok<T: Serialize>(data: &T) -> Result<Response, ServerError>
pub fn ok<T: Serialize>(data: &T) -> Result<Response, ServerError>
Examples found in repository?
examples/routing.rs (line 35)
18fn main() {
19 let mut app = Server::new();
20
21 // Basic GET route
22 app.get("/", |_req| async {
23 Response::text("Welcome to Axeon API server!")
24 });
25
26 // Route with path parameter
27 app.get("/users/:id", |req| async move {
28 let user_id = req.params.get("id").unwrap();
29 Response::text(format!("User ID: {}", user_id))
30 });
31
32 // POST request with JSON body
33 app.post("/users", |req| async move {
34 match req.body.json::<User>() {
35 Some(user) => Response::ok(&user),
36 None => Err(ServerError::BadRequest("Invalid JSON body".to_string())),
37 }
38 });
39
40 // Group routes under /api prefix
41 let mut api = Router::new();
42 api.get("/status", |_req| async {
43 Response::ok(&serde_json::json!({
44 "status": "operational",
45 "version": "1.0.0"
46 }))
47 });
48
49 // Mount the API router to the main server
50 app.mount("/api", api);
51
52 app.listen("127.0.0.1:3000")
53 .expect("Server failed to start")
54}pub fn created<T: Serialize>(data: &T) -> Result<Response, ServerError>
pub fn accepted<T: Serialize>(data: &T) -> Result<Response, ServerError>
pub fn no_content() -> Response
pub fn bad_request<T: Serialize>(data: &T) -> Result<Response, ServerError>
pub fn forbidden<T: Serialize>(data: &T) -> Result<Response, ServerError>
pub fn not_found<T: Serialize>(data: &T) -> Result<Response, ServerError>
pub fn error(err: ServerError) -> Response
pub fn stream(&mut self, content_type: &str) -> &mut Self
pub fn with_cors(&mut self, origin: &str) -> &mut Self
pub fn send(&self)
Sourcepub fn text<T: AsRef<str>>(content: T) -> Result<Response, ServerError>
pub fn text<T: AsRef<str>>(content: T) -> Result<Response, ServerError>
Examples found in repository?
More examples
examples/middleware.rs (line 65)
57fn main() {
58 let mut app = Server::new();
59
60 // Apply logger middleware globally
61 app.middleware(Logger);
62
63 // Public route - no auth required
64 app.get("/public", |_req| async {
65 Response::text("This is a public endpoint")
66 });
67
68 // Protected routes with auth middleware
69 let mut protected = Router::new();
70 protected.middleware(AuthMiddleware);
71
72 protected.get("/profile", |_req| async {
73 ok_json!({
74 "name": "User",
75 "email": "user@example.com"
76 })
77 });
78
79 app.mount("/api", protected);
80
81 app.listen("127.0.0.1:3000")
82 .expect("Server failed to start");
83}examples/routing.rs (line 23)
18fn main() {
19 let mut app = Server::new();
20
21 // Basic GET route
22 app.get("/", |_req| async {
23 Response::text("Welcome to Axeon API server!")
24 });
25
26 // Route with path parameter
27 app.get("/users/:id", |req| async move {
28 let user_id = req.params.get("id").unwrap();
29 Response::text(format!("User ID: {}", user_id))
30 });
31
32 // POST request with JSON body
33 app.post("/users", |req| async move {
34 match req.body.json::<User>() {
35 Some(user) => Response::ok(&user),
36 None => Err(ServerError::BadRequest("Invalid JSON body".to_string())),
37 }
38 });
39
40 // Group routes under /api prefix
41 let mut api = Router::new();
42 api.get("/status", |_req| async {
43 Response::ok(&serde_json::json!({
44 "status": "operational",
45 "version": "1.0.0"
46 }))
47 });
48
49 // Mount the API router to the main server
50 app.mount("/api", api);
51
52 app.listen("127.0.0.1:3000")
53 .expect("Server failed to start")
54}pub fn html<T: AsRef<str>>(content: T) -> Result<Response, ServerError>
pub fn xml<T: AsRef<str>>(content: T) -> Result<Response, ServerError>
pub fn redirect(location: &str) -> Result<Response, ServerError>
pub fn permanent_redirect(location: &str) -> Result<Response, ServerError>
pub fn method_not_allowed( allowed_methods: &[&str], ) -> Result<Response, ServerError>
pub fn with_cache_control(&mut self, directive: &str) -> &mut Self
pub fn no_cache(&mut self) -> &mut Self
pub fn with_security_headers(&mut self) -> &mut Self
pub fn conflict<T: Serialize>(data: &T) -> Result<Response, ServerError>
pub fn unprocessable_entity<T: Serialize>( data: &T, ) -> Result<Response, ServerError>
pub fn too_many_requests<T: Serialize>( data: &T, ) -> Result<Response, ServerError>
pub fn file_download(&mut self, filename: &str, content_type: &str) -> &mut Self
pub fn vary(&mut self, headers: &[&str]) -> &mut Self
pub fn with_gzip(&mut self) -> &mut Self
pub fn with_brotli(&mut self) -> &mut Self
pub fn with_language(&mut self, lang: &str) -> &mut Self
pub fn with_api_version(&mut self, version: &str) -> &mut Self
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Response
impl RefUnwindSafe for Response
impl Send for Response
impl Sync for Response
impl Unpin for Response
impl UnwindSafe for Response
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more