|
@ -0,0 +1,78 @@ |
|
|
|
|
|
/*
|
|
|
|
|
|
* For God so loved the world, that He gave His only begotten Son, that all who believe in Him should not perish but have everlasting life
|
|
|
|
|
|
*/
|
|
|
|
|
|
use std::env;
|
|
|
|
|
|
use clap::Parser;
|
|
|
|
|
|
use rocket::http::Status;
|
|
|
|
|
|
use rocket::Request;
|
|
|
|
|
|
use rocket::request::{FromRequest, Outcome};
|
|
|
|
|
|
use rocket_db_pools::{Database};
|
|
|
|
|
|
use crate::cli_chirho::cli_chirho::CliChirho;
|
|
|
|
|
|
use crate::models_chirho::DbChirho;
|
|
|
|
|
|
use crate::models_chirho::host_model_chirho::{create_host_chirho, delete_host_chirho, list_hosts_chirho, read_host_chirho, update_host_chirho};
|
|
|
|
|
|
use crate::models_chirho::user_model_chirho::{create_user_chirho, delete_user_chirho, list_users_chirho, read_user_chirho, update_user_chirho};
|
|
|
|
|
|
use crate::models_chirho::user_ssh_authorized_key_chirho::{create_ssh_key_chirho, delete_ssh_key_chirho, list_ssh_keys_chirho, read_ssh_key_chirho, update_ssh_key_chirho};
|
|
|
|
|
|
|
|
|
|
|
|
pub mod models_chirho;
|
|
|
|
|
|
pub mod cli_chirho;
|
|
|
|
|
|
|
|
|
|
|
|
#[macro_use] extern crate rocket;
|
|
|
|
|
|
|
|
|
|
|
|
// API Key request guard
|
|
|
|
|
|
pub struct ApiKeyChirho(String);
|
|
|
|
|
|
|
|
|
|
|
|
#[rocket::async_trait]
|
|
|
|
|
|
impl<'r> FromRequest<'r> for ApiKeyChirho {
|
|
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
|
|
|
|
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> {
|
|
|
|
|
|
fn is_valid(key: &str) -> bool {
|
|
|
|
|
|
let api_key = env::var("API_KEY_CHIRHO").expect("API_KEY_CHIRHO must be set");
|
|
|
|
|
|
key == api_key
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
match request.headers().get_one("X-API-Key-Chirho") {
|
|
|
|
|
|
None => Outcome::Error((Status::Unauthorized, ())),
|
|
|
|
|
|
Some(key) if is_valid(key) => Outcome::Success(ApiKeyChirho(key.to_string())),
|
|
|
|
|
|
Some(_) => Outcome::Error((Status::Unauthorized, ())),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[get("/")]
|
|
|
|
|
|
fn index() -> &'static str {
|
|
|
|
|
|
"Hallelujah, world!"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[launch]
|
|
|
|
|
|
fn rocket_chirho() -> _ {
|
|
|
|
|
|
dotenvy::from_filename(".env_chirho").expect("Failed to load .env file");
|
|
|
|
|
|
|
|
|
|
|
|
let cli_chirho = CliChirho::parse();
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(url_chirho) = cli_chirho.cli_url_chirho {
|
|
|
|
|
|
// Run in CLI mode
|
|
|
|
|
|
if let Err(e) = cli_chirho::cli_chirho::run_cli_chirho(&url_chirho) {
|
|
|
|
|
|
eprintln!("Error in CLI mode: {}", e);
|
|
|
|
|
|
std::process::exit(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
std::process::exit(0);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// Run in server mode
|
|
|
|
|
|
create_rocket_chirho()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn create_rocket_chirho() -> rocket::Rocket<rocket::Build> {
|
|
|
|
|
|
rocket::build()
|
|
|
|
|
|
.attach(DbChirho::init())
|
|
|
|
|
|
.mount("/", routes![
|
|
|
|
|
|
index,
|
|
|
|
|
|
// HostChirho routes
|
|
|
|
|
|
list_hosts_chirho, create_host_chirho, read_host_chirho, update_host_chirho, delete_host_chirho,
|
|
|
|
|
|
// UserChirho routes
|
|
|
|
|
|
list_users_chirho, create_user_chirho, read_user_chirho, update_user_chirho, delete_user_chirho,
|
|
|
|
|
|
// UserSshAuthorizedKeyChirho routes
|
|
|
|
|
|
list_ssh_keys_chirho, create_ssh_key_chirho, read_ssh_key_chirho, update_ssh_key_chirho, delete_ssh_key_chirho,
|
|
|
|
|
|
])
|
|
|
|
|
|
}
|