A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
blob: b4879fcd985cd0ce5525e666b2f115e35ba6a76d (3102 bytes)[doc][blame][raw]
1
use sqlx::postgres::Postgres;2
use tide_compress::CompressMiddleware;3
use tide_rustls::TlsListener;4
use tide_sqlx::SQLxMiddleware;6
use crate::abort;7
use crate::middleware::Auth;8
use crate::middleware::Cors;9
use crate::middleware::Error;10
use crate::route;11
use crate::unwrap_option_or_abort;12
use crate::unwrap_result_or_abort;13
use crate::Config;14
use crate::Result;15
use crate::ToUrl;17
#[derive(Default)]18
pub struct Server;20
impl Server {21
pub async fn run(&self) -> Result<()> {22
let config = unwrap_result_or_abort!(Config::acquire(), "cannot acquire config!");23
let mut app = tide::new();25
/* bind middleware */26
app.with(Error::default());27
app.with(unwrap_result_or_abort!(28
SQLxMiddleware::<Postgres>::new(&unwrap_result_or_abort!(29
config.database.to_url(),30
"cannot parse database address!"31
))32
.await,33
"cannot connect to database!"34
));35
app.with(Auth::default());36
app.with(Cors::new(&config.server.options.origin));37
app.with(CompressMiddleware::new());39
/* serve content */40
app.at("/").serve_file("dist/www/index.html")?;41
app.at("/manifest.json")42
.serve_file("dist/www/manifest.json")?;43
app.at("/worker.js").serve_file("dist/www/worker.js")?;44
app.at("/asset").serve_dir("dist/www/asset/")?;45
app.at("/app").serve_dir("dist/app/")?;47
/* handle prefetch */48
app.at("*").options(route::do_prefetch);50
/* handle api requests */51
app.at("/api/account").get(route::get_account);52
app.at("/api/account").put(route::update_account);53
app.at("/api/search").get(route::do_search);54
app.at("/api/song").get(route::get_song);55
app.at("/api/favorite").get(route::get_favorites);56
app.at("/api/favorite").post(route::add_favorite);57
app.at("/api/favorite").delete(route::delete_favorite);58
app.at("/api/station/:id").get(route::get_station);59
app.at("/api/countries").get(route::get_countries);60
app.at("/api/languages").get(route::get_languages);61
app.at("/api/tags").get(route::get_tags);62
app.at("/api/devices").get(route::get_devices);64
let address =65
unwrap_result_or_abort!(config.server.to_url(), "cannot parse server address!");66
info!("starting server on http://{}", address);67
let listener = if config.server.is_tls() {68
app.listen(69
TlsListener::build()70
.addrs(&address)71
.cert(unwrap_option_or_abort!(72
config.server.cert_path,73
"no certificate for TLS found!"74
))75
.key(unwrap_option_or_abort!(76
config.server.key_path,77
"no key for TLS found!"78
)),79
)80
.await81
} else {82
app.listen(address).await83
};84
unwrap_result_or_abort!(listener, "cannot start server!");85
Ok(())86
}87
}