agate

Simple gemini server for static files
git clone https://github.com/mbrubeck/agate.git
Log | Files | Refs | README

commit 45d2e8cf8081032b35cba9704a3665b0706b3f43
parent 73fa0e0f01e5a6cc43ea39a111a75bfab09ceb01
Author: Matt Brubeck <mbrubeck@limpet.net>
Date:   Wed, 20 May 2020 08:40:49 -0700

Use async filesystem I/O

Diffstat:
Msrc/main.rs | 12++++++------
1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/src/main.rs b/src/main.rs @@ -2,6 +2,7 @@ use { async_std::{ io::prelude::*, net::{TcpListener, TcpStream}, + path::PathBuf, stream::StreamExt, task, }, @@ -9,9 +10,8 @@ use { lazy_static::lazy_static, std::{ error::Error, - fs::{File, read}, + fs::File, io::BufReader, - path::PathBuf, sync::Arc, }, url::Url, @@ -79,7 +79,7 @@ async fn connection(stream: TcpStream) -> Result { stream.write_all(b"50 Invalid request.\r\n").await?; Err(e) } - Ok(url) => match get(&url) { + Ok(url) => match get(&url).await { Err(e) => { stream.write_all(b"40 Not found, sorry.\r\n").await?; Err(e) @@ -102,12 +102,12 @@ async fn parse_request(stream: &mut TlsStream<TcpStream>) -> Result<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); path.extend(url.path_segments().ok_or("invalid url")?); - if path.is_dir() { + if path.is_dir().await { path.push("index.gemini"); } - let response = read(&path)?; + let response = async_std::fs::read(&path).await?; Ok(response) }