summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: bfd53b8b3911abc5ea8ca80511460a555ca39ff1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use]
extern crate rocket;
use rspotify::spotify::client::Spotify;
use rspotify::spotify::oauth2::{SpotifyClientCredentials, SpotifyOAuth};
use rspotify::spotify::util::get_token;

mod serve;

//#[tokio::main]
fn main() {
    // Set client_id and client_secret in .env file or
    // export CLIENT_ID="your client_id"
    // export CLIENT_SECRET="secret"
    // export REDIRECT_URI=your-direct-uri

    // Or set client_id, client_secret,redirect_uri explictly
    // let oauth = SpotifyOAuth::default()
    //     .client_id("this-is-my-client-id")
    //     .client_secret("this-is-my-client-secret")
    //     .redirect_uri("http://localhost:8888/callback")
    //     .build();
    //
    // looking up article 42 should yield the article we inserted with a vote count of 1
    let mut rt = tokio::runtime::Builder::new()
        .enable_all()
        .threaded_scheduler()
        .thread_name("voter")
        .build()
        .unwrap();

    let mut db = rt
        .block_on(noria::ControllerHandle::from_zk("127.0.0.1:2181"))
        .unwrap();
    println!("test");
    rt.block_on(db.install_recipe(
        "
    CREATE TABLE Article (aid int, title varchar(255), url text, PRIMARY KEY(aid));
    CREATE TABLE Vote (aid int, uid int);
",
    ))
    .unwrap();
    println!("staring_init");
    let init = init();

    rocket::ignite()
        .mount("/", routes![serve::token, serve::get_tracks])
        .launch();
    rt.block_on(init);
}

async fn init() {
    let mut db = noria::ControllerHandle::from_zk("127.0.0.1:2181")
        .await
        .unwrap();

    //db.ready();
    //println!("test {:?}", db.url());
    db.install_recipe(
        "
    CREATE TABLE Article (aid int, title varchar(255), url text, PRIMARY KEY(aid));
    CREATE TABLE Vote (aid int, uid int);
",
    )
    .await
    .unwrap();
    println!("test");
    // we can then get handles that let us insert into the new tables
    let mut article = db.table("Article").await.unwrap();
    let mut vote = db.table("Vote").await.unwrap();

    // let's make a new article
    let aid = 42;
    let title = "I love Soup";
    let url = "https://pdos.csail.mit.edu";
    article
        .insert(vec![aid.into(), title.into(), url.into()])
        .await
        .unwrap();

    // and then vote for it
    vote.insert(vec![aid.into(), 1.into()]).await.unwrap();
    println!("test");

    // we can also declare views that we want want to query
    db.extend_recipe(
        "
    VoteCount: \
      SELECT Vote.aid, COUNT(uid) AS votes \
      FROM Vote GROUP BY Vote.aid;
    QUERY ArticleWithVoteCount: \
      SELECT Article.aid, title, url, VoteCount.votes AS votes \
      FROM Article LEFT JOIN VoteCount ON (Article.aid = VoteCount.aid) \
      WHERE Article.aid = ?;",
    )
    .await
    .unwrap();

    // and then get handles that let us execute those queries to fetch their results
    let mut awvc = db.view("ArticleWithVoteCount").await.unwrap();

    assert_eq!(
        awvc.lookup(&[aid.into()], true).await.unwrap(),
        vec![vec![
            noria::DataType::from(aid),
            title.into(),
            url.into(),
            1.into()
        ]]
    );
    println!("init done");
}