main: Add arguments and the respective parsing

Signed-off-by: Gunwant Jain <mail@wantguns.dev>
This commit is contained in:
Gunwant Jain
2022-01-18 15:47:54 +05:30
parent f35bad75e5
commit 6961ed59b4
3 changed files with 128 additions and 0 deletions

View File

@@ -1,14 +1,50 @@
#[macro_use]
extern crate rocket;
use std::{fs, net::IpAddr, path::PathBuf};
use clap::Parser;
use rocket::shield::{NoSniff, Shield};
use rocket_dyn_templates::Template;
mod models;
mod routes;
/// A minimal, opinionated pastebin
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about=None)]
pub struct Args {
/// Path to the uploads folder
#[clap(short, long, default_value = "./upload")]
upload: std::path::PathBuf,
/// Port on which the webserver runs
#[clap(short, long, default_value_t = 6162)]
port: u16,
/// Address on which the webserver runs
#[clap(short, long, default_value = "0.0.0.0")]
address: IpAddr,
/// Binary uploads file size limit (in MiB)
#[clap(short, long, default_value_t = 100)]
binary_upload_limit: i32,
}
pub fn get_parsed_args() -> Args {
Args::parse()
}
pub fn get_upload_dir() -> PathBuf {
get_parsed_args().upload
}
#[launch]
fn rocket() -> _ {
let shield = Shield::default().disable::<NoSniff>();
let args = get_parsed_args();
// create the upload directory, if not already created
fs::create_dir_all(args.upload).expect("Could not create the upload directory");
rocket::build()
.mount(