91 lines
2.4 KiB
Rust
91 lines
2.4 KiB
Rust
mod api;
|
|
mod config;
|
|
mod model;
|
|
mod state;
|
|
mod storage_manager;
|
|
|
|
use crate::config::PicOxConfig;
|
|
use crate::model::api_key::{ApiKey, ApiPermissions};
|
|
use base64::Engine;
|
|
use j_db::database::Database;
|
|
use log::info;
|
|
use std::path::PathBuf;
|
|
use structopt::StructOpt;
|
|
|
|
#[derive(StructOpt, Debug, Clone)]
|
|
#[structopt(about = "PicOx Commands")]
|
|
enum SubCommands {
|
|
/// Start the PicOx server
|
|
Start,
|
|
/// Create a Picox API Key
|
|
CreateKey {
|
|
/// Key description
|
|
description: String,
|
|
/// API Key permissions (WRITE, DELETE)
|
|
permissions: ApiPermissions,
|
|
},
|
|
/// Dump the database state to json
|
|
Dump {
|
|
/// Output path
|
|
out_path: PathBuf,
|
|
},
|
|
/// Import the database state from json
|
|
Import {
|
|
/// Path to json containing DB state
|
|
db_file: PathBuf,
|
|
},
|
|
}
|
|
|
|
#[derive(Debug, Clone, StructOpt)]
|
|
struct Args {
|
|
/// Path to the config file
|
|
#[structopt(short, long, env = "PICOX_CONFIG")]
|
|
pub config: PathBuf,
|
|
#[structopt(subcommand)]
|
|
pub command: SubCommands,
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() {
|
|
let args = Args::from_args();
|
|
|
|
// initialize tracing
|
|
tracing_subscriber::fmt::init();
|
|
|
|
let config = PicOxConfig::new(args.config);
|
|
|
|
let db = Database::new(&config.db_path).unwrap();
|
|
|
|
match args.command {
|
|
SubCommands::Start => {
|
|
api::run_picox(db, config).await;
|
|
}
|
|
SubCommands::CreateKey {
|
|
description,
|
|
permissions,
|
|
} => {
|
|
let (token, api_key) = ApiKey::create_new_key(&db, description, permissions).unwrap();
|
|
|
|
info!("New Key info: {:?}", api_key);
|
|
info!("Token: {}", base64::prelude::BASE64_STANDARD.encode(token));
|
|
db.db.flush().unwrap();
|
|
}
|
|
SubCommands::Dump { out_path } => {
|
|
info!("Dumping database state to {:?}", out_path);
|
|
|
|
tokio::fs::write(out_path, db.dump_db().unwrap().pretty(4))
|
|
.await
|
|
.unwrap();
|
|
}
|
|
SubCommands::Import { db_file } => {
|
|
info!("Importing database state from {:?}", db_file);
|
|
let db_state = tokio::fs::read(db_file).await.unwrap();
|
|
let db_state = String::from_utf8(db_state).unwrap();
|
|
let db_state = json::parse(&db_state).unwrap();
|
|
|
|
db.import_db(db_state).unwrap();
|
|
db.db.flush().unwrap();
|
|
}
|
|
}
|
|
}
|