pub struct LazySock { /* private fields */ }
Expand description
The main LazySock server struct.
Implementations§
Source§impl LazySock
impl LazySock
Sourcepub fn with_log_callback<F>(self, callback: F) -> Self
pub fn with_log_callback<F>(self, callback: F) -> Self
Sets a custom log callback function.
Sourcepub fn with_prompt_callback<F>(self, callback: F) -> Self
pub fn with_prompt_callback<F>(self, callback: F) -> Self
Sets a custom prompt callback function.
Sourcepub fn with_cleanup_on_exit(self, cleanup: bool) -> Self
pub fn with_cleanup_on_exit(self, cleanup: bool) -> Self
Configures whether to clean up the socket file on exit.
Sourcepub async fn route<F>(&self, method: Method, path: &str, handler: F)
pub async fn route<F>(&self, method: Method, path: &str, handler: F)
Registers a handler for a specific method and path.
Examples found in repository?
examples/demo.rs (lines 11-13)
6async fn main() -> Result<(), Box<dyn std::error::Error>> {
7 let server = lazy_sock!("/tmp/lazy-sock-demo.sock");
8
9 // Route for GET /
10 server
11 .route(Method::Get, "/", |_req| {
12 Response::json(r#"{"message": "Hello, World!", "status": "success"}"#)
13 })
14 .await;
15
16 // Route for GET /health
17 server
18 .route(Method::Get, "/health", |_req| {
19 Response::json(r#"{"status": "healthy"}"#)
20 })
21 .await;
22
23 // Route for POST /echo
24 server
25 .route(Method::Post, "/echo", |req| match req.body_string() {
26 Ok(body) if !body.is_empty() => Response::json(&format!(r#"{{"echo": "{}"}}"#, body)),
27 Ok(_) => Response::new(400).with_text("Request body is empty"),
28 Err(_) => Response::new(400).with_text("Invalid UTF-8 in request body"),
29 })
30 .await;
31
32 // Route for GET /html
33 server
34 .route(Method::Get, "/html", |_req| {
35 Response::html(
36 r#"
37 <!DOCTYPE html>
38 <html>
39 <head><title>Lazy Sock Demo</title></head>
40 <body><h1>Hello from Lazy Sock!</h1></body>
41 </html>
42 "#,
43 )
44 })
45 .await;
46
47 println!("Lazy Sock Demo Server Starting...");
48 println!("Socket: /tmp/lazy-sock-demo.sock");
49 println!("Try these commands:");
50 println!(" curl --unix-socket /tmp/lazy-sock-demo.sock http://localhost/");
51 println!(" curl --unix-socket /tmp/lazy-sock-demo.sock http://localhost/health");
52 println!(" curl --unix-socket /tmp/lazy-sock-demo.sock http://localhost/html");
53 println!(
54 " curl --unix-socket /tmp/lazy-sock-demo.sock -X POST http://localhost/echo -d 'Hello from curl!'"
55 );
56 println!("Press Ctrl+C to stop");
57 println!();
58
59 // Run the server
60 server.run().await?;
61
62 println!("Server stopped gracefully.");
63 Ok(())
64}
Sourcepub async fn run(self) -> Result<(), Box<dyn Error>>
pub async fn run(self) -> Result<(), Box<dyn Error>>
Starts the server and listens for incoming connections.
Examples found in repository?
examples/demo.rs (line 60)
6async fn main() -> Result<(), Box<dyn std::error::Error>> {
7 let server = lazy_sock!("/tmp/lazy-sock-demo.sock");
8
9 // Route for GET /
10 server
11 .route(Method::Get, "/", |_req| {
12 Response::json(r#"{"message": "Hello, World!", "status": "success"}"#)
13 })
14 .await;
15
16 // Route for GET /health
17 server
18 .route(Method::Get, "/health", |_req| {
19 Response::json(r#"{"status": "healthy"}"#)
20 })
21 .await;
22
23 // Route for POST /echo
24 server
25 .route(Method::Post, "/echo", |req| match req.body_string() {
26 Ok(body) if !body.is_empty() => Response::json(&format!(r#"{{"echo": "{}"}}"#, body)),
27 Ok(_) => Response::new(400).with_text("Request body is empty"),
28 Err(_) => Response::new(400).with_text("Invalid UTF-8 in request body"),
29 })
30 .await;
31
32 // Route for GET /html
33 server
34 .route(Method::Get, "/html", |_req| {
35 Response::html(
36 r#"
37 <!DOCTYPE html>
38 <html>
39 <head><title>Lazy Sock Demo</title></head>
40 <body><h1>Hello from Lazy Sock!</h1></body>
41 </html>
42 "#,
43 )
44 })
45 .await;
46
47 println!("Lazy Sock Demo Server Starting...");
48 println!("Socket: /tmp/lazy-sock-demo.sock");
49 println!("Try these commands:");
50 println!(" curl --unix-socket /tmp/lazy-sock-demo.sock http://localhost/");
51 println!(" curl --unix-socket /tmp/lazy-sock-demo.sock http://localhost/health");
52 println!(" curl --unix-socket /tmp/lazy-sock-demo.sock http://localhost/html");
53 println!(
54 " curl --unix-socket /tmp/lazy-sock-demo.sock -X POST http://localhost/echo -d 'Hello from curl!'"
55 );
56 println!("Press Ctrl+C to stop");
57 println!();
58
59 // Run the server
60 server.run().await?;
61
62 println!("Server stopped gracefully.");
63 Ok(())
64}
Auto Trait Implementations§
impl Freeze for LazySock
impl !RefUnwindSafe for LazySock
impl Send for LazySock
impl Sync for LazySock
impl Unpin for LazySock
impl !UnwindSafe for LazySock
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