A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
51
($target:expr, $message:expr) => {{52
match $target {53
Ok(target) => target,54
Err(_) => return $crate::Response::throw($message),54
Err(_) => {55
error!("{}", $message);56
return $crate::Response::throw($message);57
}58
}59
}};60
}
64
($target:expr, $message:expr) => {{65
match $target {66
Some(target) => target,64
None => return $crate::Response::throw($message),67
None => {68
error!("{}", $message);69
return $crate::Response::throw($message);70
}71
}72
}};73
}
1
use crate::error::Error;2
use crate::unwrap_result_or_throw;3
use serde::Serialize;5
#[derive(Debug, Serialize)]
12
impl Response<(), ()> {13
pub fn respond(body: Result<tide::Body, tide::Error>) -> tide::Result {13
Ok(tide::Response::builder(200).body(body.unwrap()).build())14
Ok(tide::Response::builder(200)15
.body(unwrap_result_or_throw!(16
body,17
"cannot acquire response body!"18
))19
.build())20
}21
}
5
use tide_sqlx::SQLxRequestExt;7
use crate::model;8
use crate::unwrap_option_or_throw;9
use crate::unwrap_result_or_throw;10
use crate::Response;11
use crate::ToSql;
15
}17
pub async fn get_account(req: tide::Request<()>) -> tide::Result {16
let account = req.ext::<model::Account>().unwrap();18
let account =19
unwrap_option_or_throw!(req.ext::<model::Account>(), "no account in request found!");20
Response::with(account)21
}23
pub async fn do_search(req: tide::Request<()>) -> tide::Result {24
let query = req.query::<model::Query>()?;22
let account = req.ext::<model::Account>().unwrap();25
let account =26
unwrap_option_or_throw!(req.ext::<model::Account>(), "no account in request found!");27
let sql = format!(28
r#"29
SELECT
42
"#,43
account_id = account.id,44
offset = query.page.unwrap_or_default() * 10,41
conditions = query.to_sql().unwrap(),45
conditions = unwrap_result_or_throw!(query.to_sql(), "cannot parse sql statement!"),46
);47
let mut conn = req.sqlx_conn::<Postgres>().await;48
let result = sqlx::query_as::<_, model::Station>(&sql)
67
let mut buf = [0; 10000];68
let mut title = None;69
loop {66
let len = response.read(&mut buf).await.unwrap();70
let len =71
unwrap_result_or_throw!(response.read(&mut buf).await, "cannot read stream data!");72
bytes.extend_from_slice(&buf);73
if total + len > interval {74
let metadata = String::from_utf8_lossy(&bytes);70
if let Some(cap) = Regex::new("StreamTitle='([^;]*)';")71
.unwrap()72
.captures_iter(&metadata)73
.next()75
if let Some(cap) =76
unwrap_result_or_throw!(Regex::new("StreamTitle='([^;]*)';"), "cannot parse regex!")77
.captures_iter(&metadata)78
.next()79
{80
title = Some(cap[1].to_string());81
}
91
}93
pub async fn get_favorites(req: tide::Request<()>) -> tide::Result {89
let account = req.ext::<model::Account>().unwrap();94
let account =95
unwrap_option_or_throw!(req.ext::<model::Account>(), "no account in request found!");96
let sql = format!(97
r#"98
SELECT
115
pub async fn add_favorite(mut req: tide::Request<()>) -> tide::Result {116
let station_id = req.body_json::<i32>().await?;111
let account = req.ext::<model::Account>().unwrap();117
let account =118
unwrap_option_or_throw!(req.ext::<model::Account>(), "no account in request found!");119
let sql = format!(120
r#"121
INSERT INTO
135
pub async fn delete_favorite(mut req: tide::Request<()>) -> tide::Result {136
let station_id = req.body_json::<i32>().await?;130
let account = req.ext::<model::Account>().unwrap();137
let account =138
unwrap_option_or_throw!(req.ext::<model::Account>(), "no account in request found!");139
let sql = format!(140
r#"141
DELETE FROM
55
let address =56
unwrap_result_or_abort!(config.server.to_url(), "cannot parse server address!");57
info!("starting server on {}", address);58
let listener = match config.server.is_tls() {59
true => {60
app.listen(61
TlsListener::build()62
.addrs(&address)63
.cert(unwrap_option_or_abort!(64
config.server.cert_path,65
"no certificate for TLS found!"66
))67
.key(unwrap_option_or_abort!(68
config.server.key_path,69
"no key for TLS found!"70
)),71
)72
.await73
}74
false => app.listen(address).await,58
let listener = if config.server.is_tls() {59
app.listen(60
TlsListener::build()61
.addrs(&address)62
.cert(unwrap_option_or_abort!(63
config.server.cert_path,64
"no certificate for TLS found!"65
))66
.key(unwrap_option_or_abort!(67
config.server.key_path,68
"no key for TLS found!"69
)),70
)71
.await72
} else {73
app.listen(address).await74
};75
unwrap_result_or_abort!(listener, "cannot start server!");76
Ok(())