MCPcopy Create free account
hub / github.com/Licoy/fetch-github-hosts / start_server_task

Function start_server_task

src-tauri/src/services.rs:105–155  ·  view source on GitHub ↗
(
    app: AppHandle,
    port: u16,
    interval_minutes: u32,
    stop_rx: oneshot::Receiver<()>,
)

Source from the content-addressed store, hash-verified

103/// Start the server mode: resolve DNS and serve hosts via HTTP
104#[cfg(feature = "gui")]
105pub async fn start_server_task(
106 app: AppHandle,
107 port: u16,
108 interval_minutes: u32,
109 stop_rx: oneshot::Receiver<()>,
110) {
111 let emit_log = |key: &str, params: Option<serde_json::Value>, level: &str| {
112 let _ = app.emit("server-log", log_payload(key, params, level));
113 };
114
115 // Initial fetch
116 match server_fetch_hosts().await {
117 Ok(_) => emit_log("server.fetchSuccess", None, "success"),
118 Err(e) => emit_log("server.fetchFail", Some(serde_json::json!({"error": e})), "error"),
119 }
120
121 // Shared shutdown signal via tokio::sync::watch
122 let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(false);
123
124 // Start HTTP server in background
125 let app_clone = app.clone();
126 let http_handle = tokio::spawn(async move {
127 start_http_server(port, app_clone, shutdown_rx).await;
128 });
129
130 let interval = std::time::Duration::from_secs(interval_minutes as u64 * 60);
131 let mut interval_timer = tokio::time::interval(interval);
132 interval_timer.tick().await;
133
134 tokio::select! {
135 _ = async {
136 loop {
137 interval_timer.tick().await;
138 match server_fetch_hosts().await {
139 Ok(_) => emit_log("server.fetchSuccess", None, "success"),
140 Err(e) => emit_log("server.fetchFail", Some(serde_json::json!({"error": e})), "error"),
141 }
142 }
143 } => {}
144 _ = stop_rx => {
145 emit_log("server.stopSuccess", None, "info");
146 // Signal shutdown to HTTP server gracefully
147 let _ = shutdown_tx.send(true);
148 // Wait for HTTP server to finish (with timeout)
149 let _ = tokio::time::timeout(
150 std::time::Duration::from_secs(3),
151 http_handle,
152 ).await;
153 }
154 }
155}
156
157/// Server-side: resolve DNS for GitHub domains, save to files
158pub async fn server_fetch_hosts() -> Result<(), String> {

Callers 2

runFunction · 0.85
start_serverFunction · 0.85

Calls 3

log_payloadFunction · 0.85
server_fetch_hostsFunction · 0.85
start_http_serverFunction · 0.85

Tested by

no test coverage detected