1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
use http_server_starter_rust::handlers::*;
use http_server_starter_rust::http_types::{get, post};
use http_server_starter_rust::router::Router;
use http_server_starter_rust::server::*;
use std::collections::HashMap;
use std::io::{self};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
fn main() -> io::Result<()> {
// Collect the command-line arguments
let args: Vec<String> = std::env::args().collect();
let mut dir = "".to_string();
// Check if the correct number of arguments are provided
if args.len() == 3 {
// Parse the arguments
if args[1] == "--directory" {
dir += &args[2];
println!("Directory: {}", dir);
} else {
eprintln!("Unknown argument: {}", args[1]);
eprintln!("Usage: {} --directory <path>", args[0]);
}
} else {
eprintln!("Usage: {} --directory <path>", args[0]);
}
let mut router: Router = Router::new();
let mut ctx = HashMap::new();
dbg!(&dir);
ctx.insert("dir".to_string(), dir);
router
.route(get("/"), handle_success)
.route(get("/echo/:var/"), handle_echo)
.route(get("/user-agent/"), handle_user_agent)
.route(get("/files/:file/"), handle_files)
.route(post("/files/:file/"), handle_post_files);
let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 4221);
let app = Server::new(socket);
app.serve(&router, Some(&ctx))
}
|