Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add systemd service commands #28

Merged
merged 1 commit into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,27 @@ tunein play "alternativeradio.us"
tunein play s221580
```

## 🧙 Systemd Service

Tunein daemon can be started as a systemd service. To enable and start the service, run the following command:

```bash
tunein service install
```

To disable and stop the service, run the following command:

```bash
tunein service uninstall
```

To check the status of the service, run the following command:

```bash
tunein service status
```


## API Documentation
[https://buf.build/tsiry/tuneinserverapis/docs/main:tunein.v1alpha1](https://buf.build/tsiry/tuneinserverapis/docs/main:tunein.v1alpha1)

Expand Down
26 changes: 26 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod player;
mod provider;
mod search;
mod server;
mod service;
mod tags;
mod tui;
mod types;
Expand Down Expand Up @@ -58,6 +59,22 @@ A simple CLI to listen to radio stations"#,
.about("Start the server")
.arg(arg!([port] "The port to listen on").default_value("8090")),
)
.subcommand(
Command::new("service")
.about("Manage systemd service for tunein-cli server")
.subcommand(
Command::new("install")
.about("Install systemd service for tunein-cli server")
)
.subcommand(
Command::new("uninstall")
.about("Uninstall systemd service for tunein-cli server")
)
.subcommand(
Command::new("status")
.about("Check status of tunein-cli systemd service")
)
)
}

#[tokio::main]
Expand Down Expand Up @@ -93,6 +110,15 @@ async fn main() -> Result<(), Error> {
let port = port.parse::<u16>().unwrap();
server::exec(port).await?;
}
Some(("service", sub_m)) => match sub_m.subcommand() {
Some(("install", _)) => service::install()?,
Some(("uninstall", _)) => service::uninstall()?,
Some(("status", _)) => service::status()?,
_ => {
println!("Invalid subcommand. Use `tunein service --help` for more information");
std::process::exit(1);
}
},
_ => unreachable!(),
}

Expand Down
11 changes: 11 additions & 0 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ pub mod playback;

pub async fn exec(port: u16) -> Result<(), Error> {
let addr: SocketAddr = format!("0.0.0.0:{}", port).parse().unwrap();
println!(
"{}",
r#"
______ ____ _______ ____
/_ __/_ _____ ___ / _/__ / ___/ / / _/
/ / / // / _ \/ -_)/ // _ \ / /__/ /___/ /
/_/ \_,_/_//_/\__/___/_//_/ \___/____/___/

"#
.bright_green()
);
println!("Listening on {}", addr.cyan());
Server::builder()
.accept_http1(true)
Expand Down
111 changes: 111 additions & 0 deletions src/service.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
use std::{path::Path, process::Command};

use anyhow::Error;

const SERVICE_TEMPLATE: &str = include_str!("./systemd/tunein.service");

pub fn install() -> Result<(), Error> {
if cfg!(not(target_os = "linux")) {
println!("This command is only supported on Linux");
std::process::exit(1);
}

let home = std::env::var("HOME")?;
let service_path: &str = &format!("{}/.config/systemd/user/tunein.service", home);
std::fs::create_dir_all(format!("{}/.config/systemd/user", home))
.expect("Failed to create systemd user directory");

if Path::new(service_path).exists() {
println!("Service file already exists. Nothing to install.");
return Ok(());
}

let tunein_path = std::env::current_exe()?;

let service_template: &str = &SERVICE_TEMPLATE.replace(
"ExecStart=/usr/bin/tunein",
&format!("ExecStart={}", tunein_path.display()),
);

std::fs::write(service_path, service_template).expect("Failed to write service file");

Command::new("systemctl")
.arg("--user")
.arg("daemon-reload")
.status()?;

Command::new("systemctl")
.arg("--user")
.arg("enable")
.arg("tunein")
.status()?;

Command::new("systemctl")
.arg("--user")
.arg("start")
.arg("tunein")
.status()?;

println!("✅ Tunein service installed successfully!");

Ok(())
}

pub fn uninstall() -> Result<(), Error> {
if cfg!(not(target_os = "linux")) {
println!("This command is only supported on Linux");
std::process::exit(1);
}

let home = std::env::var("HOME")?;
let service_path: &str = &format!("{}/.config/systemd/user/tunein.service", home);

if Path::new(service_path).exists() {
Command::new("systemctl")
.arg("--user")
.arg("stop")
.arg("tunein")
.status()?;

Command::new("systemctl")
.arg("--user")
.arg("disable")
.arg("tunein")
.status()?;

std::fs::remove_file(service_path).expect("Failed to remove service file");

Command::new("systemctl")
.arg("--user")
.arg("daemon-reload")
.status()?;

println!("✅ Tunein service uninstalled successfully!");
} else {
println!("Service file does not exist. Nothing to uninstall.");
}

Ok(())
}

pub fn status() -> Result<(), Error> {
if cfg!(not(target_os = "linux")) {
println!("This command is only supported on Linux");
std::process::exit(1);
}

let home = std::env::var("HOME")?;
let service_path: &str = &format!("{}/.config/systemd/user/tunein.service", home);

if Path::new(service_path).exists() {
Command::new("systemctl")
.arg("--user")
.arg("status")
.arg("tunein")
.status()?;
} else {
println!("Service file does not exist. Tunein service is not installed.");
}

Ok(())
}
10 changes: 10 additions & 0 deletions src/systemd/tunein.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[Unit]
Description=TuneIn Server Daemon
After=network.target

[Service]
ExecStart=/usr/bin/tunein server
Restart=always

[Install]
WantedBy=default.target