You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

137 lines
3.4 KiB

#[macro_use]
extern crate rocket;
/* use rocket::response::Responder;
use rocket::http::{ContentType, Header}; */
use rusqlite::Connection;
mod api;
mod db;
#[derive(Debug)]
struct AuthToken {
id: i32,
account_id: i32,
valid_from: String,
valid_until: String
}
/* #[derive(Debug, Deserialize)]
struct Account {
id: i32,
firstname: String,
lastname: String
} */
/* #[derive(Debug)]
struct EMail {
address: String,
description: String
} */
fn get_auth_token(token: &str) -> AuthToken {
let mut result: AuthToken = AuthToken {
id: 0,
account_id: 0,
valid_from: String::from(""),
valid_until: String::from("")
};
let conn = Connection::open("freeid.db");
match conn {
Ok(connection) => {
let stmt = connection.prepare("SELECT id, accountID, validFrom, validUntil FROM authtokens WHERE id = :id");
match stmt {
Ok(mut statement) => {
let auth_tokens = statement.query_map(&[(":id", token)], |row| {
Ok(AuthToken {
id: row.get(0)?,
account_id: row.get(1)?,
valid_from: row.get(2)?,
valid_until: row.get(3)?
})
});
match auth_tokens {
Ok(auth_tokens_iter) => {
for auth_token_res in auth_tokens_iter {
match auth_token_res {
Ok(auth_token) => {
result = auth_token;
}
Err(err) => {
println!("Error: {}", err);
}
}
}
}
Err(err) => {
println!("Error: {}", err);
}
}
}
Err(err) => {
println!("Error: {}", err);
}
}
}
Err(err) => {
println!("Error: {}", err);
}
}
result
}
fn remove_brackets(value: &str) -> &str {
if value.starts_with('[') && value.ends_with(']') {
let mut chars = value.chars();
chars.next();
chars.next_back();
chars.as_str()
}
else {
value
}
}
#[get("/")]
fn index() -> &'static str {
"Hello, world!"
}
#[get("/id/<token>/options/<options>", rank = 1)]
fn id_options(token: &str, options: &str) -> String {
println!("Route: Id -> {}", token);
let auth_token = get_auth_token(token);
let options = remove_brackets(options);
format!("Auth Token {} for Account {} with options [{}]", auth_token.id, auth_token.account_id, options)
}
#[get("/id/<token>", rank = 2)]
fn id(token: &str) -> String {
println!("Route: Id -> {}", token);
let auth_token = get_auth_token(token);
format!("Auth Token {} for Account {}", auth_token.id, auth_token.account_id)
}
#[get("/health")]
fn health() -> String {
String::from("200 OK")
}
#[launch]
fn rocket() -> _ {
match db::init_db() {
Ok(_) => {
println!("Database initiated");
},
Err(err) => {
println!("Error {}", err);
}
}
rocket::build().
mount("/", routes![index, id, id_options, health]).
mount("/api", api::user_routes())
}