Added config parsing in config.rs

This commit is contained in:
Random936
2024-12-22 18:47:37 -08:00
parent 23102ce070
commit adda5dd958
5 changed files with 355 additions and 17 deletions

View File

@@ -13,8 +13,8 @@ fn current_dir_as_string() -> String {
#[command(version, about, long_about = None)]
pub struct Args {
/// Port for the web server to listen on.
#[arg(short, long, default_value_t = 8000)]
pub port: u16,
#[arg(short, long)]
pub port: Option<u16>,
/// Interface to print URLs for. Default is all interfaces.
#[arg(short, long, default_value_t = String::from("all"))]

View File

@@ -0,0 +1,38 @@
use std::{io, fs};
use std::path::{Path, PathBuf};
use std::collections::HashMap;
use expanduser::expanduser;
use serde::Deserialize;
use serde_yaml;
pub fn load_config() -> Config {
Config::new("~/.up.yml")
}
#[derive(Deserialize)]
pub struct Config {
download_path: PathBuf,
upload_path: PathBuf,
pub web_port: u16,
pub shell_port: u16,
shells: HashMap<String, String>
}
impl Config {
pub fn new(path: &str) -> Self {
let exp_path = expanduser(path)
.expect(&format!("Failed to expand path, {}", path));
let file = fs::File::open(&exp_path)
.expect(&format!("Failed to open config path, {}", exp_path.display()));
let reader = io::BufReader::new(file);
serde_yaml::from_reader(reader).unwrap()
}
pub fn get_shell<S: Into<String>>(&self, key: S) -> Option<String> {
let key_str = key.into();
self.shells.get(&key_str).cloned()
}
}

View File

@@ -1,17 +1,35 @@
use axum::Router;
use tokio::net::TcpListener;
use axum::routing::get;
use tower_http::services::ServeDir;
use axum::routing::{get, post};
use axum::Router;
use std::env;
use std::net::SocketAddr;
mod argparse;
mod print_dir;
mod config;
mod shells;
#[tokio::main]
async fn main() {
let args = argparse::parse_args();
print_dir::print_interface(&args.interface, &args.port, &args.directory);
let conf = config::load_config();
let cwd = env::current_dir().unwrap();
let app = Router::new().route("/", get(|| async { "Hello world!" }));
let listener = TcpListener::bind(format!("0.0.0.0:{}", args.port))
let port = match args.port {
Some(port) => port,
None => conf.web_port
};
print_dir::print_interface(&args.interface, &port, &args.directory);
let app = Router::new()
//.route("/download/:path", get(download_handler))
//.route("/upload", post(upload_handler))
.route("/shells/:shell", get(shells::shells_handler))
.nest_service("/", ServeDir::new(cwd));
let listener = TcpListener::bind(format!("0.0.0.0:{}", port))
.await
.unwrap();