so://phie

verbal

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

blob: 3c4970512aeb2d8d869f94c678e521a2e458ccb9 (3451 bytes)[doc][blame][raw]

1use data::Song;

2use sqlx::postgres::Postgres;

3use tide_compress::CompressMiddleware;

4use tide_sqlx::SQLxMiddleware;

5

6mod api;

7mod config;

8mod data;

9mod error;

10mod middleware;

11mod model;

12mod response;

13mod rest;

14mod service;

15mod utils;

16

17use api::Search;

18use config::Config;

19use error::ErrorKind;

20use middleware::{AuthMiddleware, CorsMiddleware};

21use response::Response;

22use service::Service;

23

24#[tokio::main]

25async fn main() -> tide::Result<()> {

26 let config = Config::from_file("./config.toml").expect("Unable to parse config!");

27

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

29

30 /* bind database middleware */

31 app.with(

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

33 .await

34 .unwrap(),

35 );

36

37 /* bind cors middleware */

38 app.with(CorsMiddleware::new(&config.server.options.origin));

39

40 /* bind auth middleware */

41 app.with(AuthMiddleware::new());

42

43 /* bind compression middleware */

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

45

46 /* handle prefetch */

47 app.at("*")

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

49

50 /* provide index */

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

52

53 /* provide assets */

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

55

56 /* provide app */

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

58

59 app.at("/api/account")

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

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

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

63 Response::with(data)

64 });

65

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

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

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

69 Ok(query) => query,

70 Err(err) => return Response::throw(ErrorKind::Arguments, &err.to_string()),

71 };

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

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

74 Err(err) => Response::throw(ErrorKind::Fetch, &err.to_string()),

75 }

76 });

77

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

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

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

81 Ok(song) => song,

82 Err(err) => return Response::throw(ErrorKind::Arguments, &err.to_string()),

83 };

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

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

86 Err(err) => Response::throw(ErrorKind::Fetch, &err.to_string()),

87 }

88 });

89

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

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

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

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

94 Err(err) => Response::throw(ErrorKind::Fetch, &err.to_string()),

95 }

96 });

97

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

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

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

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

102 Err(err) => Response::throw(ErrorKind::Fetch, &err.to_string()),

103 }

104 });

105

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

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

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

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

110 Err(err) => Response::throw(ErrorKind::Fetch, &err.to_string()),

111 }

112 });

113

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

115

116 Ok(())

117}