use std::collections::HashMap;
use anyhow::{bail, format_err, Error};
-use futures::{FutureExt, TryFutureExt};
use lazy_static::lazy_static;
use proxmox::api::{api, router::SubdirMap, Router, RpcEnvironmentType, UserInformation};
use proxmox::list_subdirs_api_method;
use proxmox_rest_server::{ApiAuth, ApiConfig, AuthError, RestServer};
-
// Create a Dummy User info and auth system
// Normally this would check and authenticate the user
struct DummyUserInfo;
// the api to clients
proxmox_rest_server::daemon::create_daemon(
([127, 0, 0, 1], 65000).into(),
- move |listener, ready| {
+ move |listener| {
let incoming = hyper::server::conn::AddrIncoming::from_listener(listener)?;
- Ok(ready
- .and_then(|_| hyper::Server::builder(incoming)
+ Ok(async move {
+
+ hyper::Server::builder(incoming)
.serve(rest_server)
- .map_err(Error::from)
- )
- .map_err(|err| eprintln!("ERR: {}", err))
- .map(|test| println!("OK: {}", test.is_ok())))
+ .await?;
+
+ Ok(())
+ })
},
"example_server",
).await?;
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
use std::os::unix::ffi::OsStrExt;
use std::panic::UnwindSafe;
-use std::pin::Pin;
-use std::task::{Context, Poll};
use std::path::PathBuf;
use anyhow::{bail, format_err, Error};
}
}
-pub struct NotifyReady;
-
-impl Future for NotifyReady {
- type Output = Result<(), Error>;
-
- fn poll(self: Pin<&mut Self>, _cx: &mut Context) -> Poll<Result<(), Error>> {
- systemd_notify(SystemdNotify::Ready)?;
- Poll::Ready(Ok(()))
- }
-}
-
/// This creates a future representing a daemon which reloads itself when receiving a SIGHUP.
/// If this is started regularly, a listening socket is created. In this case, the file descriptor
/// number will be remembered in `PROXMOX_BACKUP_LISTEN_FD`.
/// If the variable already exists, its contents will instead be used to restore the listening
/// socket. The finished listening socket is then passed to the `create_service` function which
-/// can be used to setup the TLS and the HTTP daemon.
+/// can be used to setup the TLS and the HTTP daemon. The returned future has to call
+/// [systemd_notify] with [SystemdNotify::Ready] when the service is ready.
pub async fn create_daemon<F, S>(
address: std::net::SocketAddr,
create_service: F,
service_name: &str,
) -> Result<(), Error>
where
- F: FnOnce(tokio::net::TcpListener, NotifyReady) -> Result<S, Error>,
- S: Future<Output = ()> + Unpin,
+ F: FnOnce(tokio::net::TcpListener) -> Result<S, Error>,
+ S: Future<Output = Result<(), Error>>,
{
let mut reloader = Reloader::new()?;
move || async move { Ok(tokio::net::TcpListener::bind(&address).await?) },
).await?;
- let server_future = create_service(listener, NotifyReady)?;
+ let service = create_service(listener)?;
+
+ let service = async move {
+ if let Err(err) = service.await {
+ log::error!("server error: {}", err);
+ }
+ };
+
+ let server_future = Box::pin(service);
let shutdown_future = crate::shutdown_future();
let finish_future = match future::select(server_future, shutdown_future).await {
// http server future:
let server = daemon::create_daemon(
([127,0,0,1], 82).into(),
- move |listener, ready| {
+ move |listener| {
let incoming = hyper::server::conn::AddrIncoming::from_listener(listener)?;
- Ok(ready
- .and_then(|_| hyper::Server::builder(incoming)
+ Ok(async {
+ daemon::systemd_notify(daemon::SystemdNotify::Ready)?;
+
+ hyper::Server::builder(incoming)
.serve(rest_server)
.with_graceful_shutdown(proxmox_rest_server::shutdown_future())
.map_err(Error::from)
- )
- .map(|e| {
- if let Err(e) = e {
- eprintln!("server error: {}", e);
- }
- })
- )
+ .await
+ })
},
"proxmox-backup.service",
);
let server = daemon::create_daemon(
([0,0,0,0,0,0,0,0], 8007).into(),
- move |listener, ready| {
+ move |listener| {
let connections = accept_connections(listener, acceptor, debug);
let connections = hyper::server::accept::from_stream(ReceiverStream::new(connections));
- Ok(ready
- .and_then(|_| hyper::Server::builder(connections)
+ Ok(async {
+ daemon::systemd_notify(daemon::SystemdNotify::Ready)?;
+
+ hyper::Server::builder(connections)
.serve(rest_server)
.with_graceful_shutdown(proxmox_rest_server::shutdown_future())
.map_err(Error::from)
- )
- .map_err(|err| eprintln!("server error: {}", err))
- .map(|_| ())
- )
+ .await
+ })
},
"proxmox-backup-proxy.service",
);