so://phie

verbal

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

blob: e35aa185a53be2a454679e34051a8b56b59e6b13 (1539 bytes)[doc][blame][raw]

1use sqlx::postgres::Postgres;

2use tide_compress::CompressMiddleware;

3use tide_sqlx::SQLxMiddleware;

4

5use crate::abort;

6use crate::middleware::Auth;

7use crate::middleware::Cors;

8use crate::middleware::Error;

9use crate::route;

10use crate::Config;

11use crate::Result;

12

13#[derive(Default)]

14pub struct Server;

15

16impl Server {

17 pub async fn run(&self) -> Result<()> {

18 let config = Config::acquire().unwrap_or_else(|err| abort(&err));

19 let mut app = tide::new();

20

21 /* bind middleware */

22 app.with(Error::default());

23 app.with(

24 SQLxMiddleware::<Postgres>::new(&config.database.to_string())

25 .await

26 .unwrap(),

27 );

28 app.with(Auth::default());

29 app.with(Cors::new(&config.server.options.origin));

30 app.with(CompressMiddleware::new());

31

32 /* serve content */

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

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

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

36

37 /* handle prefetch */

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

39

40 /* handle api requests */

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

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

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

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

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

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

47

48 app.listen(&config.server.to_string()).await?;

49 Ok(())

50 }

51}