mirror of
https://github.com/sigoden/dufs.git
synced 2026-04-09 00:59:02 +03:00
feat: add logger
This commit is contained in:
@@ -40,6 +40,10 @@ fn app() -> clap::Command<'static> {
|
||||
.help("Authenticate with user and pass")
|
||||
.value_name("user:pass");
|
||||
|
||||
let arg_no_log = Arg::new("no-log")
|
||||
.long("--no-log")
|
||||
.help("Don't log any request/response information.");
|
||||
|
||||
clap::command!()
|
||||
.about(ABOUT)
|
||||
.arg(arg_address)
|
||||
@@ -47,6 +51,7 @@ fn app() -> clap::Command<'static> {
|
||||
.arg(arg_path)
|
||||
.arg(arg_readonly)
|
||||
.arg(arg_auth)
|
||||
.arg(arg_no_log)
|
||||
}
|
||||
|
||||
pub fn matches() -> ArgMatches {
|
||||
@@ -60,6 +65,7 @@ pub struct Args {
|
||||
pub path: PathBuf,
|
||||
pub readonly: bool,
|
||||
pub auth: Option<String>,
|
||||
pub log: bool,
|
||||
}
|
||||
|
||||
impl Args {
|
||||
@@ -74,6 +80,7 @@ impl Args {
|
||||
let path = Args::parse_path(path)?;
|
||||
let readonly = matches.is_present("readonly");
|
||||
let auth = matches.value_of("auth").map(|v| v.to_owned());
|
||||
let log = !matches.is_present("no-log");
|
||||
|
||||
Ok(Args {
|
||||
address,
|
||||
@@ -81,6 +88,7 @@ impl Args {
|
||||
path,
|
||||
readonly,
|
||||
auth,
|
||||
log,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -59,8 +59,8 @@
|
||||
const ajax = new XMLHttpRequest();
|
||||
ajax.upload.addEventListener("progress", e => this.progress(e), false);
|
||||
ajax.addEventListener("load", e => this.complete(e), false);
|
||||
ajax.addEventListener("error", e => this.error(e), false);
|
||||
ajax.addEventListener("abort", e => this.abort(e), false);
|
||||
ajax.addEventListener("error", e => this.fail(e), false);
|
||||
ajax.addEventListener("abort", e => this.fail(e), false);
|
||||
ajax.open("PUT", path);
|
||||
ajax.send(file);
|
||||
}
|
||||
@@ -74,12 +74,8 @@
|
||||
this.$elem.innerHTML = `${this.file.name}`;
|
||||
}
|
||||
|
||||
error(event) {
|
||||
this.$elem.innerHTML = `${this.file.name} (x)`;
|
||||
}
|
||||
|
||||
abort(event) {
|
||||
this.$elem.innerHTML = `${this.file.name} (x)`;
|
||||
fail(event) {
|
||||
this.$elem.innerHTML = `<strike>${this.file.name}</strike>`;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,18 +122,15 @@
|
||||
async function deletePath(index) {
|
||||
const file = paths[index];
|
||||
if (!file) return;
|
||||
try {
|
||||
const res = await fetch(encodeURI(file.path), {
|
||||
method: "DELETE",
|
||||
});
|
||||
if (res.status !== 200) {
|
||||
const text = await res.text();
|
||||
throw new Error(text);
|
||||
|
||||
const ajax = new XMLHttpRequest();
|
||||
ajax.open("DELETE", encodeURI(file.path));
|
||||
ajax.addEventListener("readystatechange", function() {
|
||||
if(ajax.readyState === 4 && ajax.status === 200) {
|
||||
document.getElementById(`addPath${index}`).remove();
|
||||
}
|
||||
document.getElementById(`addPath${index}`).remove();
|
||||
} catch (err) {
|
||||
alert(`Failed to delete ${file.name}, ${err.message}`);
|
||||
}
|
||||
});
|
||||
ajax.send();
|
||||
}
|
||||
|
||||
function addUploadControl() {
|
||||
|
||||
25
src/main.rs
25
src/main.rs
@@ -4,21 +4,36 @@ macro_rules! bail {
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
|
||||
mod args;
|
||||
mod server;
|
||||
|
||||
pub type BoxResult<T> = Result<T, Box<dyn std::error::Error>>;
|
||||
|
||||
use log::LevelFilter;
|
||||
|
||||
use crate::args::{matches, Args};
|
||||
use crate::server::serve;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
Args::parse(matches())
|
||||
.map(serve)
|
||||
.unwrap_or_else(handle_err)
|
||||
.await
|
||||
.unwrap_or_else(handle_err);
|
||||
run().await.unwrap_or_else(handle_err)
|
||||
}
|
||||
|
||||
async fn run() -> BoxResult<()> {
|
||||
let args = Args::parse(matches())?;
|
||||
|
||||
let level = if args.log {
|
||||
LevelFilter::Info
|
||||
} else {
|
||||
LevelFilter::Error
|
||||
};
|
||||
simple_logger::SimpleLogger::default()
|
||||
.with_level(level)
|
||||
.init()?;
|
||||
serve(args).await
|
||||
}
|
||||
|
||||
fn handle_err<T>(err: Box<dyn std::error::Error>) -> T {
|
||||
|
||||
@@ -37,7 +37,7 @@ pub async fn serve(args: Args) -> BoxResult<()> {
|
||||
async {
|
||||
Ok::<_, Infallible>(service_fn(move |req| {
|
||||
let inner = inner.clone();
|
||||
inner.handle(req)
|
||||
inner.call(req)
|
||||
}))
|
||||
}
|
||||
});
|
||||
@@ -59,7 +59,18 @@ impl InnerService {
|
||||
Self { args }
|
||||
}
|
||||
|
||||
pub async fn handle(self: Arc<Self>, req: Request) -> Result<Response, hyper::Error> {
|
||||
pub async fn call(self: Arc<Self>, req: Request) -> Result<Response, hyper::Error> {
|
||||
let method = req.method().clone();
|
||||
let uri = req.uri().clone();
|
||||
let res = self
|
||||
.handle(req)
|
||||
.await
|
||||
.unwrap_or_else(|_| status_code!(StatusCode::INTERNAL_SERVER_ERROR));
|
||||
info!(r#""{} {}" - {}"#, method, uri, res.status());
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
pub async fn handle(self: Arc<Self>, req: Request) -> BoxResult<Response> {
|
||||
if !self.auth_guard(&req).unwrap_or_default() {
|
||||
let mut res = status_code!(StatusCode::UNAUTHORIZED);
|
||||
res.headers_mut()
|
||||
@@ -67,7 +78,7 @@ impl InnerService {
|
||||
return Ok(res);
|
||||
}
|
||||
|
||||
let res = if req.method() == Method::GET {
|
||||
if req.method() == Method::GET {
|
||||
self.handle_static(req).await
|
||||
} else if req.method() == Method::PUT {
|
||||
if self.args.readonly {
|
||||
@@ -78,8 +89,7 @@ impl InnerService {
|
||||
self.handle_delete(req).await
|
||||
} else {
|
||||
return Ok(status_code!(StatusCode::NOT_FOUND));
|
||||
};
|
||||
Ok(res.unwrap_or_else(|_| status_code!(StatusCode::INTERNAL_SERVER_ERROR)))
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_static(&self, req: Request) -> BoxResult<Response> {
|
||||
|
||||
Reference in New Issue
Block a user