pub struct Server {
pub max_connections: usize,
pub keep_alive: Duration,
/* private fields */
}Expand description
The main application struct that represents your web server.
§Example
use axeon::{ok_json, Server};
fn main() {
let mut app = Server::new();
// Add a route
app.get("/", |_req| async {
ok_json!({ "message": "Hello" })
});
// Start the server
app.listen("127.0.0.1:3000").unwrap();
}Fields§
§max_connections: usize§keep_alive: DurationImplementations§
Source§impl Server
impl Server
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new Application instance
Examples found in repository?
More examples
examples/middleware.rs (line 58)
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 19)
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 max_connections(&mut self, max_connections: usize) -> &mut Self
pub fn keep_alive(&mut self, keep_alive: Duration) -> &mut Self
pub fn plugins<T>(&mut self, plugin: T) -> &mut Self
pub fn on_error<F>(&mut self, handler: F) -> &mut Self
Sourcepub fn get<F, R>(&mut self, path: &str, handler: F)
pub fn get<F, R>(&mut self, path: &str, handler: F)
Registers a GET route handler
§Arguments
path- The URL path to matchhandler- The async handler function
Examples found in repository?
More examples
examples/middleware.rs (lines 64-66)
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 (lines 22-24)
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}Sourcepub fn post<F, R>(&mut self, path: &str, handler: F)
pub fn post<F, R>(&mut self, path: &str, handler: F)
Registers a POST route handler
§Arguments
path- The URL path to matchhandler- The async handler function
Examples found in repository?
examples/routing.rs (lines 33-38)
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}Sourcepub fn put<F, R>(&mut self, path: &str, handler: F)
pub fn put<F, R>(&mut self, path: &str, handler: F)
Registers a PUT route handler
§Arguments
path- The URL path to matchhandler- The async handler function
Sourcepub fn patch<F, R>(&mut self, path: &str, handler: F)
pub fn patch<F, R>(&mut self, path: &str, handler: F)
Registers a PATCH route handler
§Arguments
path- The URL path to matchhandler- The async handler function
Sourcepub fn delete<F, R>(&mut self, path: &str, handler: F)
pub fn delete<F, R>(&mut self, path: &str, handler: F)
Registers a DELETE route handler
§Arguments
path- The URL path to matchhandler- The async handler function
Sourcepub fn head<F, R>(&mut self, path: &str, handler: F)
pub fn head<F, R>(&mut self, path: &str, handler: F)
Registers a HEAD route handler
§Arguments
path- The URL path to matchhandler- The async handler function
Sourcepub fn connect<F, R>(&mut self, path: &str, handler: F)
pub fn connect<F, R>(&mut self, path: &str, handler: F)
Registers a CONNECT route handler
§Arguments
path- The URL path to matchhandler- The async handler function
Sourcepub fn options<F, R>(&mut self, path: &str, handler: F)
pub fn options<F, R>(&mut self, path: &str, handler: F)
Registers an OPTIONS route handler
§Arguments
path- The URL path to matchhandler- The async handler function
Sourcepub fn trace<F, R>(&mut self, path: &str, handler: F)
pub fn trace<F, R>(&mut self, path: &str, handler: F)
Registers a TRACE route handler
§Arguments
path- The URL path to matchhandler- The async handler function
Sourcepub fn middleware(&mut self, middleware: impl Middleware + 'static)
pub fn middleware(&mut self, middleware: impl Middleware + 'static)
Examples found in repository?
examples/middleware.rs (line 61)
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}Sourcepub fn mount(&mut self, path: &str, router: Router)
pub fn mount(&mut self, path: &str, router: Router)
Mounts a router at a specific path
§Arguments
path- The URL path to mount the routerrouter- The router to mount
Examples found in repository?
examples/middleware.rs (line 79)
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}More examples
examples/routing.rs (line 50)
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}Sourcepub fn with_tls<P: AsRef<Path>>(
&mut self,
cert_file: P,
key_file: P,
) -> &mut Self
pub fn with_tls<P: AsRef<Path>>( &mut self, cert_file: P, key_file: P, ) -> &mut Self
Configure TLS for HTTPS support
Sourcepub fn listen(self, addr: &str) -> Result<(), Box<dyn Error>>
pub fn listen(self, addr: &str) -> Result<(), Box<dyn Error>>
Examples found in repository?
More examples
examples/middleware.rs (line 81)
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 52)
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}Sourcepub fn static_dir(&mut self, dir: &str) -> &mut Self
pub fn static_dir(&mut self, dir: &str) -> &mut Self
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Server
impl !RefUnwindSafe for Server
impl Send for Server
impl Sync for Server
impl Unpin for Server
impl !UnwindSafe for Server
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