so://phie

verbal

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

blob: 9e9eface984131d5efa1d83539d912ced8e20e78 (2431 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.ron").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.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/search")

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

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

61 Ok(query) => query,

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

63 };

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

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

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

67 }

68 });

69

70 app.at("/api/favorite")

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

72 match Service::get_favorites(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/favorite")

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

80 match Service::add_favorite(req).await {

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

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

83 }

84 });

85

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

87

88 Ok(())

89}