Mercurial > crates > systemd-socket
comparison README.md @ 0:a65053246c29
Initial commit
| author | Martin Habovstiak <martin.habovstiak@gmail.com> |
|---|---|
| date | Thu, 26 Nov 2020 22:53:35 +0100 |
| parents | |
| children | 0edcde404b02 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:a65053246c29 |
|---|---|
| 1 # systemd socket | |
| 2 | |
| 3 A convenience crate for optionally supporting systemd socket activation. | |
| 4 | |
| 5 ## About | |
| 6 | |
| 7 The goal of this crate is to make socket activation with systemd in your project trivial. | |
| 8 It provides a replacement for `std::net::SocketAddr` that allows parsing the bind address from string just like the one from `std` | |
| 9 but on top of that also allows `systemd://socket_name` format that tells it to use systemd activation with given socket name. | |
| 10 Then it provides a method to bind the address which will return the socket from systemd if available. | |
| 11 | |
| 12 The provided type supports conversions from various types of strings and also `serde` and `parse_arg` via feature flag. | |
| 13 Thanks to this the change to your code should be minimal - parsing will continue to work, it'll just allow a new format. | |
| 14 You only need to change the code to use `SocketAddr::bind()` instead of `TcpListener::bind()` for binding. | |
| 15 | |
| 16 Further, the crate also provides convenience methods for binding `tokio` 0.2, 0.3, and `async_std` sockets. | |
| 17 | |
| 18 ## Example | |
| 19 | |
| 20 ```rust | |
| 21 use systemd_socket::SocketAddr; | |
| 22 use std::convert::TryFrom; | |
| 23 use std::io::Write; | |
| 24 | |
| 25 let mut args = std::env::args_os(); | |
| 26 let program_name = args.next().expect("unknown program name"); | |
| 27 let socket_addr = args.next().expect("missing socket address"); | |
| 28 let socket_addr = SocketAddr::try_from(socket_addr).expect("failed to parse socket address"); | |
| 29 let socket = socket_addr.bind().expect("failed to bind socket"); | |
| 30 | |
| 31 loop { | |
| 32 let _ = socket | |
| 33 .accept() | |
| 34 .expect("failed to accept connection") | |
| 35 .0 | |
| 36 .write_all(b"Hello world!") | |
| 37 .map_err(|err| eprintln!("Failed to send {}", err)); | |
| 38 } | |
| 39 ``` | |
| 40 | |
| 41 ## Features | |
| 42 | |
| 43 * `serde` - implements `serde::Deserialize` for `SocketAddr` | |
| 44 * `parse_arg` - implements `parse_arg::ParseArg` for `SocketAddr` | |
| 45 * `tokio_0_2` - adds `bind_tokio_0_2` convenience method to `SocketAddr` | |
| 46 * `tokio_0_3` - adds `bind_tokio_0_3` convenience method to `SocketAddr` | |
| 47 * `async_std` - adds `bind_async_std` convenience method to `SocketAddr` | |
| 48 | |
| 49 ## License | |
| 50 | |
| 51 MITNFA |
