so://phie

verbal

A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal

blob: 4bea309748402e6c4e225a9223c9b3c0d7b3a791 (2586 bytes)[doc][blame][raw]

1use sqlx::postgres::Postgres;

2use tide_compress::CompressMiddleware;

3use tide_rustls::TlsListener;

4use tide_sqlx::SQLxMiddleware;

5

6use crate::abort;

7use crate::middleware::Auth;

8use crate::middleware::Cors;

9use crate::middleware::Error;

10use crate::route;

11use crate::unwrap_option_or_abort;

12use crate::unwrap_result_or_abort;

13use crate::Config;

14use crate::Result;

15use crate::ToUrl;

16

17#[derive(Default)]

18pub struct Server;

19

20impl 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();

24

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());

38

39 /* serve content */

40 app.at("/").serve_file("www/index.html")?;

41 app.at("/asset").serve_dir("www/asset/")?;

42 app.at("/app").serve_dir("app/")?;

43

44 /* handle prefetch */

45 app.at("*").options(route::do_prefetch);

46

47 /* handle api requests */

48 app.at("/api/account").get(route::get_account);

49 app.at("/api/search").get(route::do_search);

50 app.at("/api/song").get(route::get_song);

51 app.at("/api/favorite").get(route::get_favorites);

52 app.at("/api/favorite").post(route::add_favorite);

53 app.at("/api/favorite").delete(route::delete_favorite);

54

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 = 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 .await

72 } else {

73 app.listen(address).await

74 };

75 unwrap_result_or_abort!(listener, "cannot start server!");

76 Ok(())

77 }

78}