mirror of
https://github.com/actix/examples
synced 2025-06-26 09:17:41 +02:00
restructure folders
This commit is contained in:
8
unix-socket/Cargo.toml
Normal file
8
unix-socket/Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "unix-socket"
|
||||
version = "1.0.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
env_logger = "0.9.0"
|
||||
actix-web = "4.0.0-rc.1"
|
17
unix-socket/README.md
Normal file
17
unix-socket/README.md
Normal file
@ -0,0 +1,17 @@
|
||||
## Unix domain socket example
|
||||
|
||||
```bash
|
||||
cd other/unix-socket
|
||||
cargo run
|
||||
|
||||
# in another shell
|
||||
curl --unix-socket /tmp/actix-uds.socket http://localhost/
|
||||
Hello world!
|
||||
```
|
||||
|
||||
Although this will only one thread for handling incoming connections
|
||||
according to the [documentation](https://actix.github.io/actix-web/actix_web/struct.HttpServer.html#method.bind_uds).
|
||||
|
||||
And it does not delete the socket file (`/tmp/actix-uds.socket`) when stopping
|
||||
the server, so it will fail to start next time you run it unless you delete
|
||||
the socket file manually.
|
32
unix-socket/src/main.rs
Normal file
32
unix-socket/src/main.rs
Normal file
@ -0,0 +1,32 @@
|
||||
use actix_web::{middleware, web, App, HttpRequest, HttpServer};
|
||||
|
||||
async fn index(_req: HttpRequest) -> &'static str {
|
||||
"Hello world!"
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
#[cfg(unix)]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
::std::env::set_var("RUST_LOG", "actix_server=info,actix_web=info");
|
||||
env_logger::init();
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
// enable logger - always register Actix Web Logger middleware last
|
||||
.wrap(middleware::Logger::default())
|
||||
.service(
|
||||
web::resource("/index.html")
|
||||
.route(web::get().to(|| async { "Hello world!" })),
|
||||
)
|
||||
.service(web::resource("/").to(index))
|
||||
})
|
||||
.bind_uds("/tmp/actix-uds.socket")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
||||
#[cfg(not(unix))]
|
||||
fn main() -> std::io::Result<()> {
|
||||
println!("not supported");
|
||||
Ok(())
|
||||
}
|
Reference in New Issue
Block a user