Add Store support and unsigned app releases
This commit is contained in:
57
crates/sf-core/src/dial.rs
Normal file
57
crates/sf-core/src/dial.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
//! DIAL glue: apps with a `[dial]` section in their manifest (YouTube) can be
|
||||
//! started from a phone.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use sf_dial::{AppState, Config, Handler, LaunchParams};
|
||||
|
||||
use crate::apps::Core;
|
||||
|
||||
struct Dial(Arc<Core>);
|
||||
|
||||
impl Dial {
|
||||
fn app_for(&self, dial_name: &str) -> Option<String> {
|
||||
self.0.apps().into_iter().find(|a| a.dial.as_ref().is_some_and(|d| d.name == dial_name)).map(|a| a.id)
|
||||
}
|
||||
}
|
||||
|
||||
impl Handler for Dial {
|
||||
fn state(&self, name: &str) -> Option<AppState> {
|
||||
let id = self.app_for(name)?;
|
||||
Some(if self.0.is_running(&id) { AppState::Running } else { AppState::Stopped })
|
||||
}
|
||||
|
||||
fn launch(&self, name: &str, params: LaunchParams) -> Result<()> {
|
||||
let id = self.app_for(name).context("unknown app")?;
|
||||
log::info!("dial: {name} -> {id} from {}", params.peer);
|
||||
let core = self.0.clone();
|
||||
let query = params.query();
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = core.launch(&id, Some(query), true).await {
|
||||
log::error!("dial launch {id}: {e:#}");
|
||||
}
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn stop(&self, name: &str) -> bool {
|
||||
match self.app_for(name) {
|
||||
Some(id) => self.0.stop(&id),
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn run(core: Arc<Core>, port: u16) -> Result<()> {
|
||||
let settings = sf_ipc::Settings::load(&sf_ipc::paths::settings_file()).unwrap_or_default();
|
||||
let mut config = Config { http_port: port, ..Default::default() };
|
||||
if let Some(name) = settings.get("device_name").filter(|n| !n.trim().is_empty()) {
|
||||
config.friendly_name = name.trim().to_string();
|
||||
}
|
||||
if let Ok(id) = std::fs::read_to_string("/etc/machine-id") {
|
||||
config.uuid = sf_dial::uuid_from_machine_id(&id);
|
||||
}
|
||||
log::info!("dial: \"{}\" uuid:{}", config.friendly_name, config.uuid);
|
||||
sf_dial::run(config, Dial(core)).await
|
||||
}
|
||||
Reference in New Issue
Block a user