so://phie

verbal

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

blob: a6b38bb1b362f75db9cbae9c9befe8db181ece39 (3383 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("dist/www/index.html")?;

41 app.at("/*").serve_file("dist/www/index.html")?;

42 app.at("/manifest.json")

43 .serve_file("dist/www/manifest.json")?;

44 app.at("/worker.js").serve_file("dist/www/worker.js")?;

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

46 app.at("/media").serve_dir("media/")?;

47 app.at("/app").serve_dir("dist/app/")?;

48

49 /* handle prefetch */

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

51

52 /* handle api requests */

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

54 app.at("/api/account").put(route::update_account);

55 app.at("/api/account").post(route::add_account);

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

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

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

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

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

61 app.at("/api/station/:id").get(route::get_station);

62 app.at("/api/category/:kind").get(route::get_category);

63 app.at("/api/group/:id").get(route::get_group);

64 app.at("/api/countries").get(route::get_countries);

65 app.at("/api/languages").get(route::get_languages);

66 app.at("/api/tags").get(route::get_tags);

67 app.at("/api/devices").get(route::get_devices);

68

69 let address =

70 unwrap_result_or_abort!(config.server.to_url(), "cannot parse server address!");

71 info!("starting server on http://{}", address);

72 let listener = if config.server.is_tls() {

73 app.listen(

74 TlsListener::build()

75 .addrs(&address)

76 .cert(unwrap_option_or_abort!(

77 config.server.cert_path,

78 "no certificate for TLS found!"

79 ))

80 .key(unwrap_option_or_abort!(

81 config.server.key_path,

82 "no key for TLS found!"

83 )),

84 )

85 .await

86 } else {

87 app.listen(address).await

88 };

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

90 Ok(())

91 }

92}