summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 026f61007c8a81d297cf00b4775cfda1da003676 (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
extern crate linoquotes_gamma;

#[macro_use]
extern crate log;
extern crate env_logger;
extern crate clap;
extern crate rusqlite;

use clap::{App, Arg};

fn main() {
    env_logger::init().unwrap();

    let matches = App::new("linoquotes")
        .version("3.0.0")
        .author("Kjetil Ørbekk")
        .about(
            "Quote db for #linux.no. Run with \
               RUST_LOG=linoquotes_gamma=info to enable logging.",
        )
        .arg(
            Arg::with_name("port")
                .short("p")
                .long("port")
                .takes_value(true)
                .help("Port to serve on"),
        )
        .arg(
            Arg::with_name("db_file")
                .long("db_file")
                .takes_value(true)
                .help(
                    "Path to sqlite database (use in-memory test \
                    database if unset)",
                ),
        )
        .arg(Arg::with_name("use_test_db").long("use_test_db").help(
            "Use in-memory test database",
        ))
        .get_matches();

    let port = matches
        .value_of("port")
        .unwrap_or("8080")
        .parse::<u16>()
        .expect("port number");

    let use_test_db = matches.is_present("use_test_db");
    let db_file = matches.value_of("db_file");
    let state = linoquotes_gamma::server::State {
        connection: match db_file {
            None => {
                assert!(use_test_db, "--db_file or --use_test_db must be set");
                rusqlite::Connection::open_in_memory().unwrap()
            }
            Some(ref path) => rusqlite::Connection::open(path).unwrap(),
        },
    };

    linoquotes_gamma::data::init(&state.connection).unwrap();
    if use_test_db {
        linoquotes_gamma::data::populate_test_db(&state.connection).unwrap();
    }

    for i in 1..1000 {
        linoquotes_gamma::data::new_quote(
            &state.connection,
            "2017-07-10",
            "orbekk",
            &format!("another test {}", i),
        ).unwrap();
        let qid = state.connection.last_insert_rowid();
        info!("Last inserted quote: {}", qid);
        linoquotes_gamma::data::approve_quote(&state.connection, qid).unwrap();
    }

    linoquotes_gamma::server::serve(state, port);
}