Skip to content
This repository has been archived by the owner on Oct 8, 2023. It is now read-only.

Commit

Permalink
Merge branch 'master' into stable
Browse files Browse the repository at this point in the history
  • Loading branch information
segler-alex committed Jan 18, 2020
2 parents 1c3ed57 + 528765e commit ae46ccf
Show file tree
Hide file tree
Showing 25 changed files with 338 additions and 178 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.6.5] 2020-01-18
### Added
- Ansible role and example playbook for debian/ubuntu

### Fixed
- Station checks
- Always use UTC time in database
- Faster check insert with mysql 5.7

### Changed
- Default install paths changed
- IPs for clicks are only kept until not needed anymore (default 24 hours)

## [0.6.4] 2020-01-14
### Fixed
- Insert of checks does now ignore duplicates
Expand Down
68 changes: 34 additions & 34 deletions Cargo.lock

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description = "Radio-Browser Server with REST API"
license = "agpl-3.0"
name = "radiobrowser-api-rust"
readme = "README.md"
version = "0.6.4"
version = "0.6.5"
edition = "2018"

[dependencies]
Expand All @@ -26,7 +26,7 @@ serde = "1.0.104"
serde_derive ="1.0.104"
serde_json = "1.0.44"
threadpool = "1.7.1"
toml = "0.5.5"
toml = "0.5.6"
url = "2.1.1"
uuid = { version = "0.8.1", features = ["serde", "v4"] }
website-icon-extract = "0.4.1"
Expand All @@ -42,10 +42,10 @@ Radio-Browser Server with REST API
section = "admin"
priority = "optional"
assets = [
["target/release/radiobrowser-api-rust", "usr/local/bin/radiobrowser", "755"],
["static/*", "usr/local/share/radiobrowser/", "644"],
["init.sql", "usr/local/share/radiobrowser/init.sql", "644"],
["init/*", "etc/sytemd/system/", "644"],
["target/release/radiobrowser-api-rust", "usr/bin/radiobrowser", "755"],
["static/*", "usr/share/radiobrowser/", "644"],
["init.sql", "usr/share/radiobrowser/init.sql", "644"],
["init/*", "etc/systemd/system/", "644"],
["radiobrowser.toml", "etc/radiobrowser/config-example.toml", "644"],
["radiobrowser.toml", "etc/radiobrowser/config.toml", "644"],
]
Expand Down
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ docker stack deploy -c docker-compose-traefik.yml rb
# download distribution
mkdir -p radiobrowser
cd radiobrowser
wget https://github.com/segler-alex/radiobrowser-api-rust/releases/download/0.6.4/radiobrowser-dist.tar.gz
wget https://github.com/segler-alex/radiobrowser-api-rust/releases/download/0.6.5/radiobrowser-dist.tar.gz
tar -zxf radiobrowser-dist.tar.gz

# config database
Expand All @@ -101,9 +101,9 @@ sudo systemctl start radiobrowser
* create database and database user

```bash
wget https://github.com/segler-alex/radiobrowser-api-rust/releases/download/0.6.4/radiobrowser-api-rust_0.6.4_amd64.deb
wget https://github.com/segler-alex/radiobrowser-api-rust/releases/download/0.6.5/radiobrowser-api-rust_0.6.5_amd64.deb
sudo apt install default-mysql-server
sudo dpkg -i radiobrowser-api-rust_0.6.4_amd64.deb
sudo dpkg -i radiobrowser-api-rust_0.6.5_amd64.deb
cat /usr/share/radiobrowser/init.sql | mysql
```

Expand Down Expand Up @@ -184,6 +184,18 @@ Apache config file example
Follow this guide to get a free certificate
<https://certbot.eff.org/>

### Ansible role

```bash
# clone this project
git clone https://github.com/segler-alex/radiobrowser-api-rust.git
cd radiobrowser-api-rust
# checkout stable
git checkout stable
# deploy, change email adress, for ssl with certbot
ansible-playbook -e "[email protected]" -e "version=0.6.5" -e "ansible_python_interpreter=auto" -i "test.example.com,test2.example.com" ansible/playbook.yml
```

## Building

### Distribution tar.gz
Expand Down
5 changes: 5 additions & 0 deletions ansible/inventory
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[servers]
test.api.radio-browser.info

[servers:vars]
ansible_python_interpreter=auto
5 changes: 5 additions & 0 deletions ansible/playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
- hosts: all
remote_user: root
roles:
- radiobrowser
47 changes: 47 additions & 0 deletions ansible/roles/radiobrowser/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
- name: Reload package cache
apt:
update_cache: yes
upgrade: yes
- name: Install apache2 and mysql
apt:
name: apache2,default-mysql-server,python3-pymysql,certbot,python-certbot-apache
- name: Create a new database with name 'radio'
mysql_db:
name: radio
state: present
login_unix_socket: /var/run/mysqld/mysqld.sock
- name: Create database user
mysql_user:
name: radiouser
password: password
priv: 'radio.*:ALL'
state: present
login_unix_socket: /var/run/mysqld/mysqld.sock
- name: Install a .deb package from the internet.
apt:
deb: "https://github.com/segler-alex/radiobrowser-api-rust/releases/download/{{version}}/radiobrowser-api-rust_{{version}}_amd64.deb"
- name: Create www root
file:
state: directory
path: /var/www/radio
- name: Add apache2 virtual host
template:
src: ../templates/radio-browser.conf.j2
dest: /etc/apache2/sites-available/radio-browser.conf
- name: Enable apache2 modules
apache2_module:
state: present
name: proxy_http
- name: Enable site
command: a2ensite radio-browser
- name: Reload service apache2
service:
name: apache2
state: reloaded
- name: Enable radiobrowser service
service:
name: radiobrowser
state: restarted
enabled: yes
- name: Setup certbot
command: certbot --apache --agree-tos -m {{email}} -d {{ansible_fqdn}} -n --redirect
18 changes: 18 additions & 0 deletions ansible/roles/radiobrowser/templates/radio-browser.conf.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<VirtualHost *:80>
ServerName {{ ansible_fqdn }}

ServerAdmin {{ email }}
DocumentRoot /var/www/radio

ErrorLog ${APACHE_LOG_DIR}/error.radio.log
CustomLog ${APACHE_LOG_DIR}/access.radio.log combined

ProxyPass "/" "http://localhost:8080/"
ProxyPassReverse "/" "http://localhost:8080/"

<Directory /var/www/radio/>
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
3 changes: 0 additions & 3 deletions debian/postinst
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# Add user
groupadd -r radiobrowser
useradd --no-log-init -r -g radiobrowser radiobrowser

groupadd --system radiobrowser
useradd --system --no-create-home --home-dir /var/lib/radiobrowser --gid radiobrowser radiobrowser

Expand Down
2 changes: 1 addition & 1 deletion docker-compose-traefik.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: "3.2"
services:
api:
build: ./
image: segleralex/radiobrowser-api-rust:0.6.4
image: segleralex/radiobrowser-api-rust:0.6.5
labels:
- "traefik.enable=true"
- "traefik.http.routers.api.rule=Host(`${SOURCE}`)"
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: "3.0"
services:
api:
build: ./
image: segleralex/radiobrowser-api-rust:0.6.4
image: segleralex/radiobrowser-api-rust:0.6.5
deploy:
replicas: 1
networks:
Expand Down
4 changes: 3 additions & 1 deletion init/radiobrowser.service
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ WantedBy=multi-user.target

[Service]

Environment="RUST_LOG=radiobrowser_api_rust=info"

##############################################################################
## Core requirements
##
Expand All @@ -27,7 +29,7 @@ ProtectSystem=full
ProtectHome=true

# Start main service
ExecStart=/usr/local/bin/radiobrowser -f /etc/radiobrowser/config.toml
ExecStart=/usr/bin/radiobrowser -f /etc/radiobrowser/config.toml

Restart=always
RestartSec=5s
10 changes: 5 additions & 5 deletions install_from_dist.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

set -e

sudo mkdir -p /usr/local/bin
sudo mkdir -p /usr/local/share/radiobrowser
sudo mkdir -p /usr/bin
sudo mkdir -p /usr/share/radiobrowser
sudo mkdir -p /var/log/radiobrowser

sudo cp target/release/radiobrowser-api-rust /usr/local/bin/radiobrowser
sudo cp target/release/radiobrowser-api-rust /usr/bin/radiobrowser
sudo cp init/radiobrowser.service /etc/systemd/system
sudo cp static/* /usr/local/share/radiobrowser/
sudo cp static/* /usr/share/radiobrowser/
sudo cp etc/config-example.toml /etc/radiobrowser/config-example.toml
if [ ! -f /etc/radiobrowser/config.toml ]; then
sudo cp etc/config-example.toml /etc/radiobrowser/config.toml
fi

sudo chmod ugo+x /usr/local/bin/radiobrowser
sudo chmod ugo+x /usr/bin/radiobrowser
sudo groupadd --system radiobrowser
sudo useradd --system --no-create-home --home-dir /var/lib/radiobrowser --gid radiobrowser radiobrowser

Expand Down
7 changes: 5 additions & 2 deletions radiobrowser.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## database connection string (mysql, mariadb)
database = "mysql://radiouser:password@localhost/radio"
## Directory for static and template files
static-files-dir = "/usr/local/share/radiobrowser"
static-files-dir = "/usr/share/radiobrowser"
## Log file path
log-dir = "/var/log/radiobrowser/"

Expand All @@ -24,6 +24,8 @@ threads = 5
server-url = "https://de1.api.radio-browser.info"
## Mirror pull interval in seconds
mirror-pull-interval = 60
## The same ip cannot do clicks for the same stream in this timespan
click-timeout-hours = 24

## Prometheus exporter
## ===================
Expand Down Expand Up @@ -70,7 +72,8 @@ retries = 5
#useragent = "useragent/1.0"

## Mirror from server
#[pullservers]
[pullservers]
[pullservers.alpha]
host = "http://www.radio-browser.info/webservice"
#[pullservers.beta]
#host = "http://de1.api.radio-browser.info"
18 changes: 9 additions & 9 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ fn encode_message(status: Result<String, Box<dyn Error>>, format : &str) -> Resu
})
}

fn encode_station_url<A>(connection_new: &A, station: Option<StationItem>, ip: &str, format : &str) -> Result<rouille::Response, Box<dyn Error>> where A: DbConnection {
fn encode_station_url<A>(connection_new: &A, station: Option<StationItem>, ip: &str, format : &str, hours: u32) -> Result<rouille::Response, Box<dyn Error>> where A: DbConnection {
Ok(match station {
Some(station) => {
let _ = connection_new.increase_clicks(&ip, &station);
let _ = connection_new.increase_clicks(&ip, &station, hours);
let station = station.into();
match format {
"json" => {
Expand Down Expand Up @@ -208,7 +208,7 @@ fn encode_status(status: Status, format : &str, static_dir: &str) -> rouille::Re
}
}

pub fn start<A: 'static + std::clone::Clone>(connection_new: A, host : String, port : i32, threads : usize, server_name: &str, static_dir: &str, log_dir: &str, prometheus_exporter_enabled: bool, prometheus_exporter_prefix: &str) where A: DbConnection, A: std::marker::Send, A: std::marker::Sync {
pub fn start<A: 'static + std::clone::Clone>(connection_new: A, host : String, port : i32, threads : usize, server_name: &str, static_dir: &str, log_dir: &str, prometheus_exporter_enabled: bool, prometheus_exporter_prefix: &str, click_timeout_hours: u32) where A: DbConnection, A: std::marker::Send, A: std::marker::Sync {
let listen_str = format!("{}:{}", host, port);
info!("Listen on {} with {} threads", listen_str, threads);
let x : Option<usize> = Some(threads);
Expand All @@ -217,7 +217,7 @@ pub fn start<A: 'static + std::clone::Clone>(connection_new: A, host : String,
let log_dir = log_dir.to_string();
let prometheus_exporter_prefix = prometheus_exporter_prefix.to_string();
rouille::start_server_with_pool(listen_str, x, move |request| {
handle_connection(&connection_new, request, &y, &static_dir, &log_dir, prometheus_exporter_enabled, &prometheus_exporter_prefix)
handle_connection(&connection_new, request, &y, &static_dir, &log_dir, prometheus_exporter_enabled, &prometheus_exporter_prefix, click_timeout_hours)
});
}

Expand Down Expand Up @@ -281,7 +281,7 @@ fn log_to_file(file_name: &str, line: &str) {
}
}

fn handle_connection<A>(connection_new: &A, request: &rouille::Request, server_name: &str, static_dir: &str, log_dir: &str, prometheus_exporter_enabled: bool, prometheus_exporter_prefix: &str) -> rouille::Response where A: DbConnection {
fn handle_connection<A>(connection_new: &A, request: &rouille::Request, server_name: &str, static_dir: &str, log_dir: &str, prometheus_exporter_enabled: bool, prometheus_exporter_prefix: &str, click_timeout_hours: u32) -> rouille::Response where A: DbConnection {
let remote_ip: String = request.header("X-Forwarded-For").unwrap_or(&request.remote_addr().ip().to_string()).to_string();
let referer: String = request.header("Referer").unwrap_or(&"-".to_string()).to_string();
let user_agent: String = request.header("User-agent").unwrap_or(&"-".to_string()).to_string();
Expand All @@ -300,15 +300,15 @@ fn handle_connection<A>(connection_new: &A, request: &rouille::Request, server_n
log_to_file(&log_file, &line);
};
rouille::log_custom(request, log_ok, log_err, || {
let result = handle_connection_internal(connection_new, request, server_name, static_dir, prometheus_exporter_enabled, prometheus_exporter_prefix);
let result = handle_connection_internal(connection_new, request, server_name, static_dir, prometheus_exporter_enabled, prometheus_exporter_prefix, click_timeout_hours);
match result {
Ok(response) => response,
Err(err) => rouille::Response::text(err.to_string()).with_status_code(500),
}
})
}

fn handle_connection_internal<A>(connection_new: &A, request: &rouille::Request, server_name: &str, static_dir: &str, prometheus_exporter_enabled: bool, prometheus_exporter_prefix: &str) -> Result<rouille::Response, Box<dyn std::error::Error>> where A: DbConnection {
fn handle_connection_internal<A>(connection_new: &A, request: &rouille::Request, server_name: &str, static_dir: &str, prometheus_exporter_enabled: bool, prometheus_exporter_prefix: &str, click_timeout_hours: u32) -> Result<rouille::Response, Box<dyn std::error::Error>> where A: DbConnection {
if request.method() != "POST" && request.method() != "GET" {
return Ok(rouille::Response::empty_404());
}
Expand Down Expand Up @@ -428,7 +428,7 @@ fn handle_connection_internal<A>(connection_new: &A, request: &rouille::Request,
"tags" => Ok(add_cors(encode_extra(connection_new.get_extra("TagCache", "TagName", Some(String::from(parameter)), param_order, param_reverse, param_hidebroken)?, format, "tag")?)),
"states" => Ok(add_cors(encode_states(connection_new.get_states(None, Some(String::from(parameter)), param_order, param_reverse, param_hidebroken)?, format)?)),
"vote" => Ok(add_cors(encode_message(connection_new.vote_for_station(&remote_ip, get_only_first_item(connection_new.get_station_by_uuid(parameter)?)), format)?)),
"url" => Ok(add_cors(encode_station_url(connection_new, get_only_first_item(connection_new.get_station_by_uuid(parameter)?), &remote_ip, format)?)),
"url" => Ok(add_cors(encode_station_url(connection_new, get_only_first_item(connection_new.get_station_by_uuid(parameter)?), &remote_ip, format, click_timeout_hours)?)),
"stations" => {
match parameter {
"topvote" => Ok(add_cors(Station::get_response(connection_new.get_stations_topvote(999999)?.drain(..).map(|x| x.into()).collect(), format)?)),
Expand Down Expand Up @@ -457,7 +457,7 @@ fn handle_connection_internal<A>(connection_new: &A, request: &rouille::Request,
let format = command;
let command = parameter;
match command {
"url" => Ok(add_cors(encode_station_url(connection_new, get_only_first_item(connection_new.get_station_by_uuid(search)?), &remote_ip, format)?)),
"url" => Ok(add_cors(encode_station_url(connection_new, get_only_first_item(connection_new.get_station_by_uuid(search)?), &remote_ip, format, click_timeout_hours)?)),
_ => Ok(rouille::Response::empty_404()),
}
}else{
Expand Down
4 changes: 3 additions & 1 deletion src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ fn dbcheck_internal(
hls: item.Hls,
check_ok: true,
url: item.Url.clone(),
timestamp: None,

metainfo_overrides_database: item.OverrideIndexMetaData,
public: item.Public,
Expand Down Expand Up @@ -198,7 +199,8 @@ fn dbcheck_internal(
hls: false,
check_ok: false,
url: "".to_string(),

timestamp: None,

metainfo_overrides_database: false,
public: None,
name: None,
Expand Down
Loading

0 comments on commit ae46ccf

Please sign in to comment.