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