Fix #3112 serve docs on localhost:{random port}#3697
Fix #3112 serve docs on localhost:{random port}#3697brettearle wants to merge 13 commits intorust-lang:mainfrom
Conversation
|
|
||
| [features] | ||
| curl-backend = ["download/curl-backend"] | ||
|
|
There was a problem hiding this comment.
Please avoid making unrelated changes.
There was a problem hiding this comment.
Sorry. Will avoid
| thiserror.workspace = true | ||
| threadpool = "1" | ||
| tokio = { workspace = true, optional = true } | ||
| tiny_http = "0.11" |
There was a problem hiding this comment.
Given that we're already depending on hyper for the reqwest download backend, I think we should be using hyper (0.14) for the server, too.
There was a problem hiding this comment.
Too easy, will do
| ) | ||
| .arg(Arg::new("topic").help(TOPIC_ARG_HELP)) | ||
| .subcommand( | ||
| Command::new("servedoc") |
There was a problem hiding this comment.
I think this should probably be doc --serve rather than a new command.
There was a problem hiding this comment.
Alright, so this would be instead of a specific doc flag and just do base docs or in addition to specific flag "doc --nomicon --serve"?
There was a problem hiding this comment.
It should be a new --serve flag on the existing doc command, that works orthogonally to other flags. Maybe it should take an optional port number or SocketAddr (like 192.168.0.1:3112) as argument?
| let doc_path_index = toolchain.doc_path(doc_url)?; | ||
| let doc_path_base = toolchain.doc_path("")?; | ||
| let doc_path_str = doc_path_base.to_string_lossy().into_owned(); | ||
| loop { | ||
| let server = Server::http("127.0.0.1:0").unwrap(); | ||
| println!("Serving documentation at {}", server.server_addr()); | ||
| for request in server.incoming_requests() { | ||
| //TODO get request path and serve filebased on that | ||
| let request_path = request.url().strip_prefix('/'); | ||
| match request_path { | ||
| Some(mut request_path) => { | ||
| println!("doc url: {:?}", &doc_url); | ||
| println!("Req file: {:?}", &request_path); | ||
| let base_string = doc_path_str.clone(); | ||
| if request_path == "" { | ||
| request_path = doc_url; | ||
| } | ||
| //strip search params | ||
| if let Some(index) = request_path.find('?') { | ||
| request_path = &request_path[..index]; | ||
| } | ||
| //ignore favicon requests | ||
| if request_path == "favicon.ico" { | ||
| continue; | ||
| } | ||
| let path = String::from(base_string + &request_path); | ||
| println!("Serving file: {:?}", &path); | ||
| let file = std::fs::File::open(path).unwrap(); | ||
| request.respond(Response::from_file(file)).unwrap(); | ||
| } | ||
| None => { | ||
| let file = std::fs::File::open(&doc_path_index).unwrap(); | ||
| request.respond(Response::from_file(file)).unwrap(); | ||
| } | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
This should be outlined into a separate function.
There was a problem hiding this comment.
Too easy, will do
|
(Also please make sure to run |
A starting implementation looking for feedback and whether I am on the right path