lumni/subcommands/
request_handler.rs1use std::fs::File;
2use std::io::{self, Write};
3use std::sync::{Arc, Mutex};
4
5use crate::{BinaryCallbackWrapper, EnvironmentConfig, ObjectStoreHandler};
6
7pub async fn handle_request(
8 matches: &clap::ArgMatches,
9 config: &mut EnvironmentConfig,
10) {
11 let method = matches.get_one::<String>("method").unwrap();
12 let uri = matches.get_one::<String>("uri").unwrap();
13
14 let output_file = None;
18
19 match method.as_str() {
20 "GET" => {
21 handle_get_request(uri, config, output_file).await;
22 }
23 "PUT" => {
24 println!("PUT request not yet implemented");
25 }
26 "DELETE" => {
27 println!("DELETE request not yet implemented");
28 }
29 "HEAD" => {
30 println!("HEAD request not yet implemented");
31 }
32 "LIST" => {
33 println!("LIST request not yet implemented");
34 }
35 _ => {
36 eprintln!("Invalid HTTP method: {}", method);
37 }
38 }
39}
40
41async fn handle_get_request(
42 uri: &str,
43 config: &EnvironmentConfig,
44 output_path: Option<&str>,
45) {
46 let handler = ObjectStoreHandler::new(None);
47
48 let callback = if let Some(output_path) = output_path {
49 let file = Arc::new(Mutex::new(File::create(output_path).unwrap()));
51 Some(BinaryCallbackWrapper::create_async(move |data: Vec<u8>| {
52 let mut file = file.lock().unwrap();
53 if let Err(e) = file.write_all(&data) {
54 eprintln!("Error writing to file: {:?}", e);
55 }
56 async {}
57 }))
58 } else {
59 Some(BinaryCallbackWrapper::create_async(move |data: Vec<u8>| {
61 let mut stdout = io::stdout();
62 if let Err(e) = stdout.write_all(&data) {
63 eprintln!("Error writing to stdout: {:?}", e);
64 }
65 async {}
66 }))
67 };
68
69 if let Err(err) = handler.get_object(uri, config, callback).await {
70 eprintln!("Error: {:?}", err);
71 }
72}