mirror of
https://github.com/getnora-io/nora.git
synced 2026-04-12 13:50:31 +00:00
fix: remove all unwrap() calls for safer error handling
This commit is contained in:
@@ -2,7 +2,7 @@ use crate::AppState;
|
|||||||
use axum::{
|
use axum::{
|
||||||
body::Bytes,
|
body::Bytes,
|
||||||
extract::{Path, State},
|
extract::{Path, State},
|
||||||
http::{header, StatusCode},
|
http::{header, HeaderName, StatusCode},
|
||||||
response::{IntoResponse, Response},
|
response::{IntoResponse, Response},
|
||||||
routing::{get, head, put},
|
routing::{get, head, put},
|
||||||
Json, Router,
|
Json, Router,
|
||||||
@@ -67,7 +67,7 @@ async fn start_upload(Path(name): Path<String>) -> Response {
|
|||||||
StatusCode::ACCEPTED,
|
StatusCode::ACCEPTED,
|
||||||
[
|
[
|
||||||
(header::LOCATION, location.clone()),
|
(header::LOCATION, location.clone()),
|
||||||
("Docker-Upload-UUID".parse().unwrap(), uuid),
|
(HeaderName::from_static("docker-upload-uuid"), uuid),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
.into_response()
|
.into_response()
|
||||||
@@ -127,7 +127,7 @@ async fn put_manifest(
|
|||||||
StatusCode::CREATED,
|
StatusCode::CREATED,
|
||||||
[
|
[
|
||||||
(header::LOCATION, location),
|
(header::LOCATION, location),
|
||||||
("Docker-Content-Digest".parse().unwrap(), digest),
|
(HeaderName::from_static("docker-content-digest"), digest),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
.into_response()
|
.into_response()
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ struct S3Error {
|
|||||||
fn xml_response<T: Serialize>(data: T) -> Response {
|
fn xml_response<T: Serialize>(data: T) -> Response {
|
||||||
let xml = format!(
|
let xml = format!(
|
||||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n{}",
|
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n{}",
|
||||||
to_xml(&data).unwrap()
|
to_xml(&data).unwrap_or_default()
|
||||||
);
|
);
|
||||||
(
|
(
|
||||||
StatusCode::OK,
|
StatusCode::OK,
|
||||||
@@ -90,7 +90,7 @@ fn error_response(status: StatusCode, code: &str, message: &str) -> Response {
|
|||||||
};
|
};
|
||||||
let xml = format!(
|
let xml = format!(
|
||||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n{}",
|
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n{}",
|
||||||
to_xml(&error).unwrap()
|
to_xml(&error).unwrap_or_default()
|
||||||
);
|
);
|
||||||
(
|
(
|
||||||
status,
|
status,
|
||||||
@@ -105,12 +105,12 @@ async fn main() {
|
|||||||
tracing_subscriber::fmt()
|
tracing_subscriber::fmt()
|
||||||
.with_env_filter(
|
.with_env_filter(
|
||||||
tracing_subscriber::EnvFilter::from_default_env()
|
tracing_subscriber::EnvFilter::from_default_env()
|
||||||
.add_directive("nora_storage=info".parse().unwrap()),
|
.add_directive("nora_storage=info".parse().expect("valid directive")),
|
||||||
)
|
)
|
||||||
.init();
|
.init();
|
||||||
|
|
||||||
let config = Config::load();
|
let config = Config::load();
|
||||||
fs::create_dir_all(&config.storage.data_dir).unwrap();
|
fs::create_dir_all(&config.storage.data_dir).expect("Failed to create data directory");
|
||||||
|
|
||||||
let state = Arc::new(AppState {
|
let state = Arc::new(AppState {
|
||||||
config: config.clone(),
|
config: config.clone(),
|
||||||
@@ -128,10 +128,14 @@ async fn main() {
|
|||||||
.with_state(state);
|
.with_state(state);
|
||||||
|
|
||||||
let addr = format!("{}:{}", config.server.host, config.server.port);
|
let addr = format!("{}:{}", config.server.host, config.server.port);
|
||||||
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
|
let listener = tokio::net::TcpListener::bind(&addr)
|
||||||
|
.await
|
||||||
|
.expect("Failed to bind to address");
|
||||||
|
|
||||||
info!("nora-storage (S3 compatible) running on http://{}", addr);
|
info!("nora-storage (S3 compatible) running on http://{}", addr);
|
||||||
axum::serve(listener, app).await.unwrap();
|
axum::serve(listener, app)
|
||||||
|
.await
|
||||||
|
.expect("Server error");
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn list_buckets(State(state): State<Arc<AppState>>) -> Response {
|
async fn list_buckets(State(state): State<Arc<AppState>>) -> Response {
|
||||||
@@ -201,15 +205,17 @@ fn collect_files(dir: &std::path::Path, prefix: &str) -> Vec<ObjectInfo> {
|
|||||||
if path.is_dir() {
|
if path.is_dir() {
|
||||||
objects.extend(collect_files(&path, &key));
|
objects.extend(collect_files(&path, &key));
|
||||||
} else if let Ok(metadata) = entry.metadata() {
|
} else if let Ok(metadata) = entry.metadata() {
|
||||||
let modified: chrono::DateTime<Utc> = metadata.modified().unwrap().into();
|
if let Ok(modified) = metadata.modified() {
|
||||||
|
let datetime: chrono::DateTime<Utc> = modified.into();
|
||||||
objects.push(ObjectInfo {
|
objects.push(ObjectInfo {
|
||||||
key,
|
key,
|
||||||
size: metadata.len(),
|
size: metadata.len(),
|
||||||
last_modified: modified.format("%Y-%m-%dT%H:%M:%SZ").to_string(),
|
last_modified: datetime.format("%Y-%m-%dT%H:%M:%SZ").to_string(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
objects
|
objects
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user