so://phie

verbal

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

blob: 3a7965baba8240d1d078df3ff107f8cf4c45856d (3357 bytes)[doc][blame][raw]

1use sqlx::postgres::Postgres;

2use tide_compress::CompressMiddleware;

3use tide_sqlx::SQLxMiddleware;

4

5use crate::abort;

6use crate::api::Search;

7use crate::data;

8use crate::middleware::Auth;

9use crate::middleware::Cors;

10use crate::model;

11use crate::Config;

12use crate::Response;

13use crate::Result;

14use crate::Service;

15

16#[derive(Default)]

17pub struct Server {}

18

19impl Server {

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

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

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

23

24 /* bind middleware */

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

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

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

28 app.with(

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

30 .await

31 .unwrap(),

32 );

33

34 /* handle prefetch */

35 app.at("*")

36 .options(|_: tide::Request<()>| async move { Ok(tide::Response::new(200)) });

37

38 /* serve content */

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

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

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

42

43 app.at("/api/account").get(get_account);

44

45 app.at("/api/search")

46 .get(|req: tide::Request<()>| async move {

47 let search = match req.query::<Search>() {

48 Ok(query) => query,

49 Err(err) => return Response::throw(&err.to_string()),

50 };

51 match Service::search(search, req).await {

52 Ok(data) => Response::with(data),

53 Err(err) => Response::throw(&err.to_string()),

54 }

55 });

56

57 app.at("/api/song")

58 .get(|req: tide::Request<()>| async move {

59 let song = match req.query::<data::Song>() {

60 Ok(song) => song,

61 Err(err) => return Response::throw(&err.to_string()),

62 };

63 match Service::get_song(song).await {

64 Ok(data) => Response::with(data),

65 Err(err) => Response::throw(&err.to_string()),

66 }

67 });

68

69 app.at("/api/like")

70 .get(|req: tide::Request<()>| async move {

71 match Service::get_likes(req).await {

72 Ok(data) => Response::with(data),

73 Err(err) => Response::throw(&err.to_string()),

74 }

75 });

76

77 app.at("/api/like")

78 .post(|req: tide::Request<()>| async move {

79 match Service::add_like(req).await {

80 Ok(data) => Response::with(data),

81 Err(err) => Response::throw(&err.to_string()),

82 }

83 });

84

85 app.at("/api/like")

86 .delete(|req: tide::Request<()>| async move {

87 match Service::remove_like(req).await {

88 Ok(data) => Response::with(data),

89 Err(err) => Response::throw(&err.to_string()),

90 }

91 });

92

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

94 Ok(())

95 }

96}

97

98async fn get_account(req: tide::Request<()>) -> tide::Result {

99 let account = req.ext::<model::Account>().unwrap();

100 let data: data::Account = (*account).clone().into();

101 Response::with(data)

102}