so://phie

verbal

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

blob: 2df75726b665cbca94dbb0c0b7185eebf55636c5 (2964 bytes)[doc][blame][raw]

1use sqlx::postgres::Postgres;

2use tide_compress::CompressMiddleware;

3use tide_sqlx::SQLxMiddleware;

4

5mod api;

6mod config;

7mod data;

8mod error;

9mod middleware;

10mod model;

11mod response;

12mod rest;

13mod service;

14mod utils;

15

16use api::Search;

17use config::Config;

18use error::ErrorKind;

19use middleware::{AuthMiddleware, CorsMiddleware};

20use response::Response;

21use service::Service;

22

23#[async_std::main]

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

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

26

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

28

29 /* bind database middleware */

30 app.with(

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

32 .await

33 .unwrap(),

34 );

35

36 /* bind cors middleware */

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

38

39 /* bind auth middleware */

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

41

42 /* bind compression middleware */

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

44

45 /* handle prefetch */

46 app.at("*")

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

48

49 /* provide index */

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

51

52 /* provide assets */

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

54

55 /* provide app */

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

57

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

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

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

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

62 Response::with(data)

63 });

64

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

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

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

68 Ok(query) => query,

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

70 };

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

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

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

74 }

75 });

76

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

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

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

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

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

82 }

83 });

84

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

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

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

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

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

90 }

91 });

92

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

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

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

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

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

98 }

99 });

100

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

102

103 Ok(())

104}