Response

Struct Response 

Source
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

Source

pub fn new(status: u16) -> Response

Source

pub fn status(&mut self, status: u16) -> &mut Self

Source

pub fn body<T: AsRef<str>>(&mut self, body: T) -> &mut Self

Source

pub fn header<K: AsRef<str>, V: AsRef<str>>( &mut self, name: K, value: V, ) -> &mut Self

Source

pub fn headers(&mut self, headers: HashMap<String, String>) -> &mut Self

Source

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}
Source

pub fn created<T: Serialize>(data: &T) -> Result<Response, ServerError>

Source

pub fn accepted<T: Serialize>(data: &T) -> Result<Response, ServerError>

Source

pub fn no_content() -> Response

Source

pub fn bad_request<T: Serialize>(data: &T) -> Result<Response, ServerError>

Source

pub fn unauthorized<T: Serialize>(data: &T) -> Result<Response, ServerError>

Source

pub fn forbidden<T: Serialize>(data: &T) -> Result<Response, ServerError>

Source

pub fn not_found<T: Serialize>(data: &T) -> Result<Response, ServerError>

Source

pub fn error(err: ServerError) -> Response

Source

pub fn stream(&mut self, content_type: &str) -> &mut Self

Source

pub fn with_cors(&mut self, origin: &str) -> &mut Self

Source

pub fn send(&self)

Source

pub fn text<T: AsRef<str>>(content: T) -> Result<Response, ServerError>

Examples found in repository?
examples/hello_world.rs (line 12)
8fn main() {
9    let mut app = Server::new();
10
11    // Add a route that handles GET requests to "/"
12    app.get("/", |_req| async { Response::text("Hello, World!") });
13
14    app.listen("127.0.0.1:3000")
15        .expect("Server failed to start");
16}
More examples
Hide additional 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}
Source

pub fn html<T: AsRef<str>>(content: T) -> Result<Response, ServerError>

Source

pub fn xml<T: AsRef<str>>(content: T) -> Result<Response, ServerError>

Source

pub fn redirect(location: &str) -> Result<Response, ServerError>

Source

pub fn permanent_redirect(location: &str) -> Result<Response, ServerError>

Source

pub fn method_not_allowed( allowed_methods: &[&str], ) -> Result<Response, ServerError>

Source

pub fn with_cache_control(&mut self, directive: &str) -> &mut Self

Source

pub fn no_cache(&mut self) -> &mut Self

Source

pub fn with_security_headers(&mut self) -> &mut Self

Source

pub fn conflict<T: Serialize>(data: &T) -> Result<Response, ServerError>

Source

pub fn unprocessable_entity<T: Serialize>( data: &T, ) -> Result<Response, ServerError>

Source

pub fn too_many_requests<T: Serialize>( data: &T, ) -> Result<Response, ServerError>

Source

pub fn service_unavailable<T: Serialize>( data: &T, ) -> Result<Response, ServerError>

Source

pub fn file_download(&mut self, filename: &str, content_type: &str) -> &mut Self

Source

pub fn vary(&mut self, headers: &[&str]) -> &mut Self

Source

pub fn with_gzip(&mut self) -> &mut Self

Source

pub fn with_brotli(&mut self) -> &mut Self

Source

pub fn with_language(&mut self, lang: &str) -> &mut Self

Source

pub fn with_api_version(&mut self, version: &str) -> &mut Self

Trait Implementations§

Source§

impl Debug for Response

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.