summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: f0cac8f5d2eb064abb51add6826ca3b5615a9854 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#![recursion_limit = "1024"]

#![feature(plugin)]
#![plugin(rocket_codegen)]

extern crate clap;
extern crate chrono;
extern crate dimensioned;
#[macro_use]
extern crate error_chain;
#[macro_use]
extern crate log;
extern crate regex;
extern crate rocket;
extern crate rocket_contrib;
extern crate serde;
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate serde_json;
extern crate simplelog;

use std::collections::HashMap;
use std::process::exit;
use std::fs::File;

use clap::{App, Arg};
use chrono::prelude::*;
use rocket::State;
use rocket_contrib::Json;
use simplelog::{SimpleLogger, LogLevelFilter, Config as LogConfig};

mod api;
mod config;
mod error;
use api::*;
use config::{Config, LogItem};
use error::*;

#[get("/")]
fn index() -> &'static str {
    "Hello there!"
}

#[post("/search", format = "application/json", data = "<data>")]
fn search(data : Json<Search>, config: State<Config>) -> Json<SearchResponse> {
    debug!("handling search request: {:?}", data.0);
    Json(
        SearchResponse(
            (*config.all_aliases()).clone()
        )
    )
}

#[post("/query", format = "application/json", data = "<data>")]
fn query(data: Json<Query>, config: State<Config>) -> Result<Json<QueryResponse>> {
    debug!("handling query: {:?}", data.0);
    let targets = data.0.targets;
    debug!("targets: {:?}", targets);
    let response : Vec<TargetData> = Vec::new();
    let mut target_hash : HashMap<&String, (&LogItem, Vec<String>)> = HashMap::new();
    for li in config.items() {
        for t in targets.clone() {
            if li.aliases().contains(&t.target) {
                if target_hash.contains_key(&li.alias()) {
                    if let Some(&mut (litem, ref mut cnames)) = target_hash.get_mut(&li.alias()) {
                        cnames.push(t.target.split('.').nth(1).ok_or(Error::from("no capture name found"))?.into());
                    }
                }
                else {
                    target_hash.insert(
                        li.alias(),
                        (&li, vec![
                            t.target
                                .split('.')
                                .nth(1)
                                .ok_or(Error::from("no capture name found"))?
                                .into()
                            ]
                        )
                    );
                }
            }
        }
    }

    Err(Error::from("not implemented"))
}

fn main() {
    let matches = App::new("aklog-server")
                        .version("0.1.0")
                        .author("Mario Krehl <mario-krehl@gmx.de>")
                        .about("Presents antikoerper-logfiles to grafana")
                        .arg(Arg::with_name("config")
                             .short("c")
                             .long("config")
                             .value_name("FILE")
                             .help("configuration file to use")
                             .takes_value(true)
                             .required(true))
                        .arg(Arg::with_name("verbosity")
                             .short("v")
                             .long("verbose")
                             .help("sets the level of verbosity")
                             .multiple(true))
                        .get_matches();

    match matches.occurrences_of("verbosity") {
        0 => SimpleLogger::init(LogLevelFilter::Warn, LogConfig::default()).unwrap(),
        1 => SimpleLogger::init(LogLevelFilter::Info, LogConfig::default()).unwrap(),
        2 => SimpleLogger::init(LogLevelFilter::Debug, LogConfig::default()).unwrap(),
        3 | _  => SimpleLogger::init(LogLevelFilter::Trace, LogConfig::default()).unwrap(),
    };
    debug!("Initialized logger");

    let config_file = matches.value_of("config").unwrap();
    let config = match Config::load(String::from(config_file)) {
        Ok(c) => c,
        Err(e) => {
            error!("{}", e);
            exit(1);
        },
    };

    rocket::ignite()
        .manage(config)
        .mount("/", routes![index, search, query])
        .launch();
}