main.rs 5.23 KB
Newer Older
1
//#![feature(extern_prelude)]
Robert Czechowski's avatar
Robert Czechowski committed
2
3
4
5
6
7
8
9

#[macro_use]
extern crate iron;
#[macro_use]
extern crate router;
#[macro_use]
extern crate serde_derive;

10
11
12
#[macro_use]
extern crate structopt;

13
extern crate rusqlite;
Robert Czechowski's avatar
Robert Czechowski committed
14
15
16
17
18
19
20
21
22
extern crate iron_sessionstorage;
extern crate urlencoded;
extern crate time;
extern crate persistent;
extern crate rand;
extern crate mount;
extern crate staticfile;
extern crate handlebars_iron;
extern crate serde_json;
23
24
25
extern crate params;
extern crate reqwest;
extern crate serde_yaml;
26
27
28
29
30
31
32
33
34
35
36
37

use rusqlite::Connection;

mod db_apply_migrations;
mod db_conn_sqlite;
mod db_conn;
mod db_objects;

use db_conn::{MedalConnection, MedalObject};

use db_objects::*;

Robert Czechowski's avatar
Robert Czechowski committed
38
mod webfw_iron;
39
mod configreader_yaml;
Robert Czechowski's avatar
Robert Czechowski committed
40
41
42
43
44
45
46
47

use webfw_iron::start_server;

mod functions;

use std::path;
use std::fs;

48
49
50
use std::path::{Path,PathBuf};
use structopt::StructOpt;

51
52
53
54
55
56
#[derive(Serialize, Deserialize)]
pub struct Config {
    oauth_client_id: Option<String>,
    oauth_client_secret: Option<String>,
    oauth_access_token_url: Option<String>,
    oauth_user_data_url: Option<String>,
57
    database_file: Option<PathBuf>,
58
59
}

60
fn read_config_from_file(file: &Path) -> Config {
61
62
    use std::io::Read;
    
63
    if let Ok(mut file) = fs::File::open(file) {
64
65
66
67
        let mut contents = String::new();
        file.read_to_string(&mut contents).unwrap();
        serde_json::from_str(&contents).unwrap()
    } else {
68
        println!("Configuration file '{}' not found.", file.to_str().unwrap_or("<Encoding error>"));
69
70
71
72
73
        Config {
            oauth_client_id: None,
            oauth_client_secret: None,
            oauth_access_token_url: None,
            oauth_user_data_url: None,
74
            database_file: None,
75
76
        }
    }
77
}
78

79
80
81
82
83
84
85
86
87
88
89
90
91
92
#[derive(StructOpt, Debug)]
#[structopt()]
struct Opt {
    /// Config file to use (default: 'config.json')
    #[structopt(short = "c", long = "config", default_value = "config.json", parse(from_os_str))]
    configfile: PathBuf,

    /// Database file to use (default: from config file or 'medal.db')
    #[structopt(short = "d", long = "database", parse(from_os_str))]
    databasefile: Option<PathBuf>,
}



Robert Czechowski's avatar
Robert Czechowski committed
93
fn read_contest(p: &path::PathBuf) -> Option<Contest> {
94
95
    use std::fs::File;
    use std::io::Read;
Robert Czechowski's avatar
Robert Czechowski committed
96
    println!("Try to read some file …");
97

98
99
100
101
102
103
    let mut file = File::open(p).unwrap();
    let mut contents = String::new();
    file.read_to_string(&mut contents).unwrap();
    
    configreader_yaml::parse_yaml(&contents, p.file_name().to_owned()?.to_str()?, &format!("{}/", p.parent().unwrap().to_str()?)) // unwrap_or("./")?
    /*let mut contest = Contest::new("./".to_string(), "blub.json".to_string(), "Wettbewerb IX".to_string(), 45, true, None, None);
104
105
106
107
108
109
110
111
112
113
114
115
    let mut taskgroup = Taskgroup::new("Lustige Aufgabe".to_string());
    let mut task = Task::new("blub".to_string(), 1);
    taskgroup.tasks.push(task);
    let mut task = Task::new("blub2".to_string(), 4);
    taskgroup.tasks.push(task);  
    contest.taskgroups.push(taskgroup);
    let mut taskgroup = Taskgroup::new("Lustige Aufgabe3".to_string());
    let mut task = Task::new("blub3".to_string(), 2);
    taskgroup.tasks.push(task);
    let mut task = Task::new("blub4".to_string(), 3);
    taskgroup.tasks.push(task);  
    contest.taskgroups.push(taskgroup);
Robert Czechowski's avatar
Robert Czechowski committed
116

117
    Some(contest)*/
Robert Czechowski's avatar
Robert Czechowski committed
118
119
120
121
122
123
124
125
126
127
128
129
}

fn get_all_contest_info(task_dir: &str) -> Vec<Contest> {
    fn walk_me_recursively(p: &path::PathBuf, contests: &mut Vec<Contest>) {
        match fs::read_dir(p) {
            Ok(paths) => for path in paths {
                let p = path.unwrap().path();
                walk_me_recursively(&p, contests);
            },
            _ => (),
        }
        
130
        if p.file_name().unwrap().to_string_lossy().to_string().ends_with(".yaml") {
Robert Czechowski's avatar
Robert Czechowski committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
            match read_contest(p) {
                Some(contest) => contests.push(contest),
                _ => (),
            }
        };                   
    };

    
    let mut contests = Vec::new();
    match fs::read_dir(task_dir) {
        Err(why) => println!("Error opening tasks directory! {:?}", why.kind()),
        Ok(paths) => for path in paths {
            walk_me_recursively(&path.unwrap().path(), &mut contests);
        },
    };

    contests
}

fn refresh_all_contests(conn : &mut Connection) {
    let v = get_all_contest_info("tasks/");

    for mut contest_info in v {
        contest_info.save(conn);
    }
}

fn main() {
159
160
161
162
163
164
165
    let opt = Opt::from_args();
    println!("{:?}", opt);
    
    let mut config = read_config_from_file(&opt.configfile);

    if opt.databasefile.is_some() { config.database_file = opt.databasefile; }
    //if config.database_file.is_none() { config.database_file = Some(.to_path_buf()); }
166
    
167
168
169
170
171
    let mut conn = match config.database_file {
        Some(ref path) => Connection::create(path),
        None => Connection::create(&Path::new("medal.db")),
    };
        
Robert Czechowski's avatar
Robert Czechowski committed
172
173
174
    db_apply_migrations::test(&mut conn);

    refresh_all_contests(&mut conn);
175
176
177
178
179
180
181
182
183
184
185
186
187
188
        
    println!("Hello, world!");

    let contest = conn.get_contest_by_id_complete(1);

    println!("Contest {}", contest.name);
    
    for taskgroup in contest.taskgroups {
        print!("  Task {}: ", taskgroup.name);
        for task in taskgroup.tasks {
            print!("{} ({}) ", task.stars, task.location);
        }
        println!("");
    }
Robert Czechowski's avatar
Robert Czechowski committed
189

190
    start_server(conn, config);
Robert Czechowski's avatar
Robert Czechowski committed
191
192

    println!("Server started.");
193
}