1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use futures_batch::ChunksTimeoutStreamExt;
use notify::Watcher;
use std::{convert::Infallible, path::PathBuf, sync::Arc};
use tokio::sync::{Mutex, Notify};
use tokio_stream::StreamExt;
use which::which;
pub struct Config {
pub host: std::net::IpAddr,
pub port: u16,
pub child_host: std::net::IpAddr,
pub child_port: u16,
pub watch_paths: Vec<PathBuf>,
pub ignore_paths: Vec<PathBuf>,
pub command: String,
}
pub async fn run(config: Config) {
let Config {
host,
port,
child_host,
child_port,
watch_paths,
ignore_paths,
command,
} = config;
let addr = std::net::SocketAddr::new(host, port);
let child_addr = std::net::SocketAddr::new(child_host, child_port);
let cwd = std::env::current_dir().unwrap();
let watch_paths: Vec<PathBuf> = watch_paths.into_iter().map(|path| cwd.join(path)).collect();
let ignore_paths: Vec<PathBuf> = ignore_paths
.into_iter()
.map(|path| cwd.join(path))
.collect();
enum State {
Ground,
Building {
notify: Arc<Notify>,
child: Option<std::process::Child>,
},
Running {
child: Option<std::process::Child>,
},
}
let state: Arc<Mutex<State>> = Arc::new(Mutex::new(State::Ground));
let (watch_events_tx, watch_events_rx) = tokio::sync::mpsc::unbounded_channel();
watch_events_tx.send(()).unwrap();
let mut watcher = notify::recommended_watcher(move |_: notify::Result<notify::Event>| {
watch_events_tx.send(()).unwrap();
})
.unwrap();
let mut walk_builder = ignore::WalkBuilder::new(watch_paths.first().unwrap());
for watch_path in watch_paths.iter().skip(1) {
walk_builder.add(watch_path);
}
walk_builder.filter_entry(move |entry| {
let path = entry.path();
let ignored = ignore_paths
.iter()
.any(|ignore_path| path.starts_with(ignore_path));
!ignored
});
let walk = walk_builder.build();
for entry in walk {
let entry = entry.unwrap();
let path = entry.path();
watcher
.watch(path, notify::RecursiveMode::NonRecursive)
.unwrap();
}
tokio::spawn({
let state = state.clone();
async move {
let mut watch_events =
tokio_stream::wrappers::UnboundedReceiverStream::new(watch_events_rx)
.chunks_timeout(1_000_000, std::time::Duration::from_millis(10));
while watch_events.next().await.is_some() {
if let State::Running { child } = &mut *state.lock().await {
let mut child = child.take().unwrap();
child.kill().ok();
child.wait().unwrap();
}
let notify = Arc::new(Notify::new());
let sh = which("sh").unwrap();
let child = std::process::Command::new(sh)
.args(vec!["-c", &command])
.env("HOST", &child_host.to_string())
.env("PORT", &child_port.to_string())
.spawn()
.unwrap();
*state.lock().await = State::Building {
notify: notify.clone(),
child: Some(child),
};
loop {
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
if let State::Building { child, .. } = &mut *state.lock().await {
if let Ok(Some(_)) | Err(_) = child.as_mut().unwrap().try_wait() {
break;
}
}
if tokio::net::TcpStream::connect(&child_addr).await.is_ok() {
break;
}
}
let child = if let State::Building { child, .. } = &mut *state.lock().await {
child.take().unwrap()
} else {
panic!()
};
*state.lock().await = State::Running { child: Some(child) };
notify.notify_waiters();
}
}
});
let handler = move |state: Arc<Mutex<State>>, mut request: http::Request<hyper::Body>| async move {
let notify = if let State::Building { notify, .. } = &mut *state.lock().await {
Some(notify.clone())
} else {
None
};
if let Some(notify) = notify {
notify.notified().await;
}
let child_authority = format!("{}:{}", child_host, child_port);
let child_authority = http::uri::Authority::from_maybe_shared(child_authority).unwrap();
*request.uri_mut() = http::Uri::builder()
.scheme("http")
.authority(child_authority)
.path_and_query(request.uri().path_and_query().unwrap().clone())
.build()
.unwrap();
hyper::Client::new()
.request(request)
.await
.unwrap_or_else(|_| {
http::Response::builder()
.status(http::StatusCode::SERVICE_UNAVAILABLE)
.body(hyper::Body::from("service unavailable"))
.unwrap()
})
};
let service = hyper::service::make_service_fn(|_| {
let state = state.clone();
async move {
Ok::<_, Infallible>(hyper::service::service_fn(
move |request: http::Request<hyper::Body>| {
let state = state.clone();
async move { Ok::<_, Infallible>(handler(state, request).await) }
},
))
}
});
hyper::Server::bind(&addr).serve(service).await.unwrap();
}