impl auto-gen-key
parent
a356d981a8
commit
456ab04f78
|
@ -21,6 +21,7 @@ http = "0.2.1"
|
|||
lazy_static = "1.4.0"
|
||||
percent-encoding = "2.1.0"
|
||||
pulldown-cmark = "0.8.0"
|
||||
rand = "0.8.3"
|
||||
regex = "1.4.1"
|
||||
reqwest = { version = "0.11.1", features = [] }
|
||||
rmp-serde = "0.14.4"
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
#![warn (clippy::pedantic)]
|
||||
|
||||
use std::{
|
||||
fs::File,
|
||||
io::Write,
|
||||
path::PathBuf,
|
||||
os::unix::fs::PermissionsExt,
|
||||
};
|
||||
|
||||
use structopt::StructOpt;
|
||||
use tokio::runtime;
|
||||
|
||||
use ptth_server::{
|
||||
load_toml,
|
||||
|
@ -14,6 +16,9 @@ use ptth_server::{
|
|||
|
||||
#[derive (Debug, StructOpt)]
|
||||
struct Opt {
|
||||
#[structopt (long)]
|
||||
auto_gen_key: bool,
|
||||
|
||||
#[structopt (long)]
|
||||
file_server_root: Option <PathBuf>,
|
||||
|
||||
|
@ -41,14 +46,41 @@ pub struct ConfigFile {
|
|||
pub file_server_root: Option <PathBuf>,
|
||||
}
|
||||
|
||||
fn main () -> Result <(), anyhow::Error> {
|
||||
#[tokio::main]
|
||||
async fn main () -> Result <(), anyhow::Error> {
|
||||
tracing_subscriber::fmt::init ();
|
||||
|
||||
let opt = Opt::from_args ();
|
||||
let asset_root = opt.asset_root;
|
||||
|
||||
let path = opt.config_path.clone ().unwrap_or_else (|| PathBuf::from ("./config/ptth_server.toml"));
|
||||
let config_file: ConfigFile = load_toml::load (&path)?;
|
||||
|
||||
let config_file: ConfigFile = match load_toml::load (&path) {
|
||||
Err (ptth_server::errors::LoadTomlError::Io (_)) => if opt.auto_gen_key {
|
||||
use rand::RngCore;
|
||||
let mut buffer = vec! [0u8; 64];
|
||||
rand::thread_rng ().fill_bytes (&mut buffer);
|
||||
|
||||
let api_key = base64::encode (&buffer);
|
||||
|
||||
{
|
||||
let mut f = File::create (&path)?;
|
||||
let metadata = f.metadata ()?;
|
||||
let mut permissions = metadata.permissions ();
|
||||
permissions.set_mode (0o600);
|
||||
f.set_permissions (permissions)?;
|
||||
|
||||
f.write (format! ("api_key = \"{}\"\n", api_key).as_bytes ())?;
|
||||
}
|
||||
|
||||
load_toml::load (&path)?
|
||||
}
|
||||
else {
|
||||
panic! ("API key not provided in config file and auto-gen-key not provided");
|
||||
},
|
||||
Ok (x) => x,
|
||||
Err (e) => return Err (e.into ()),
|
||||
};
|
||||
|
||||
let config_file = ptth_server::ConfigFile {
|
||||
name: opt.name.or (config_file.name).expect ("`name` must be provided in command line or config file"),
|
||||
|
@ -63,16 +95,12 @@ fn main () -> Result <(), anyhow::Error> {
|
|||
return Ok (());
|
||||
}
|
||||
|
||||
let rt = runtime::Runtime::new ()?;
|
||||
|
||||
rt.block_on (async move {
|
||||
run_server (
|
||||
config_file,
|
||||
ptth_core::graceful_shutdown::init (),
|
||||
Some (path),
|
||||
asset_root
|
||||
).await
|
||||
})?;
|
||||
run_server (
|
||||
config_file,
|
||||
ptth_core::graceful_shutdown::init (),
|
||||
Some (path),
|
||||
asset_root
|
||||
).await?;
|
||||
|
||||
Ok (())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue