mirror of
https://github.com/YGGverse/agate.git
synced 2026-04-08 12:35:28 +00:00
Use async filesystem I/O
This commit is contained in:
parent
73fa0e0f01
commit
45d2e8cf80
1 changed files with 6 additions and 6 deletions
12
src/main.rs
12
src/main.rs
|
|
@ -2,6 +2,7 @@ use {
|
||||||
async_std::{
|
async_std::{
|
||||||
io::prelude::*,
|
io::prelude::*,
|
||||||
net::{TcpListener, TcpStream},
|
net::{TcpListener, TcpStream},
|
||||||
|
path::PathBuf,
|
||||||
stream::StreamExt,
|
stream::StreamExt,
|
||||||
task,
|
task,
|
||||||
},
|
},
|
||||||
|
|
@ -9,9 +10,8 @@ use {
|
||||||
lazy_static::lazy_static,
|
lazy_static::lazy_static,
|
||||||
std::{
|
std::{
|
||||||
error::Error,
|
error::Error,
|
||||||
fs::{File, read},
|
fs::File,
|
||||||
io::BufReader,
|
io::BufReader,
|
||||||
path::PathBuf,
|
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
},
|
},
|
||||||
url::Url,
|
url::Url,
|
||||||
|
|
@ -79,7 +79,7 @@ async fn connection(stream: TcpStream) -> Result {
|
||||||
stream.write_all(b"50 Invalid request.\r\n").await?;
|
stream.write_all(b"50 Invalid request.\r\n").await?;
|
||||||
Err(e)
|
Err(e)
|
||||||
}
|
}
|
||||||
Ok(url) => match get(&url) {
|
Ok(url) => match get(&url).await {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
stream.write_all(b"40 Not found, sorry.\r\n").await?;
|
stream.write_all(b"40 Not found, sorry.\r\n").await?;
|
||||||
Err(e)
|
Err(e)
|
||||||
|
|
@ -102,12 +102,12 @@ async fn parse_request(stream: &mut TlsStream<TcpStream>) -> Result<Url> {
|
||||||
Ok(url)
|
Ok(url)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get(url: &Url) -> Result<Vec<u8>> {
|
async fn get(url: &Url) -> Result<Vec<u8>> {
|
||||||
let mut path = PathBuf::from(&ARGS.content_dir);
|
let mut path = PathBuf::from(&ARGS.content_dir);
|
||||||
path.extend(url.path_segments().ok_or("invalid url")?);
|
path.extend(url.path_segments().ok_or("invalid url")?);
|
||||||
if path.is_dir() {
|
if path.is_dir().await {
|
||||||
path.push("index.gemini");
|
path.push("index.gemini");
|
||||||
}
|
}
|
||||||
let response = read(&path)?;
|
let response = async_std::fs::read(&path).await?;
|
||||||
Ok(response)
|
Ok(response)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue