Merge pull request #3 from tronje/master

Add some simple logging via the env_logger crate
This commit is contained in:
Matt Brubeck 2020-11-10 09:42:48 -08:00 committed by GitHub
commit bd2bc310d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 99 additions and 2 deletions

86
Cargo.lock generated
View file

@ -6,12 +6,23 @@ version = "1.2.2"
dependencies = [
"async-std",
"async-tls",
"env_logger",
"log",
"mime_guess",
"once_cell",
"rustls",
"url",
]
[[package]]
name = "aho-corasick"
version = "0.7.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7404febffaa47dac81aa44dba71523c9d069b1bdc50a77db41195149e17f68e5"
dependencies = [
"memchr",
]
[[package]]
name = "async-channel"
version = "1.4.2"
@ -127,6 +138,17 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "065374052e7df7ee4047b1160cca5e1467a12351a40b3da123c870ba0b8eda2a"
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "autocfg"
version = "1.0.1"
@ -197,6 +219,19 @@ dependencies = [
"lazy_static",
]
[[package]]
name = "env_logger"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54532e3223c5af90a6a757c90b5c5521564b07e5e7a958681bcd2afad421cdcd"
dependencies = [
"atty",
"humantime",
"log",
"regex",
"termcolor",
]
[[package]]
name = "event-listener"
version = "2.5.1"
@ -267,6 +302,12 @@ dependencies = [
"libc",
]
[[package]]
name = "humantime"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c1ad908cc71012b7bea4d0c53ba96a8cba9962f048fa68d143376143d863b7a"
[[package]]
name = "idna"
version = "0.2.0"
@ -426,6 +467,24 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "regex"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38cf2c13ed4745de91a5eb834e11c00bcc3709e773173b2ce4c56c9fbde04b9c"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
"thread_local",
]
[[package]]
name = "regex-syntax"
version = "0.6.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b181ba2dcf07aaccad5448e8ead58db5b742cf85dfe035e2227f137a539a189"
[[package]]
name = "ring"
version = "0.16.15"
@ -487,6 +546,24 @@ dependencies = [
"unicode-xid",
]
[[package]]
name = "termcolor"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f"
dependencies = [
"winapi-util",
]
[[package]]
name = "thread_local"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14"
dependencies = [
"lazy_static",
]
[[package]]
name = "tinyvec"
version = "0.3.4"
@ -672,6 +749,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"

View file

@ -17,6 +17,8 @@ mime_guess = "2.0"
once_cell = "1.4"
rustls = "0.18.0"
url = "2.1"
log = "0.4"
env_logger = "0.8"
[profile.release]
lto = true

View file

@ -35,6 +35,12 @@ agate 0.0.0.0:1965 path/to/content/ cert.pem key.rsa
When a client requests the URL `gemini://example.com/foo/bar`, Agate will respond with the file at `path/to/content/foo/bar`. If there is a directory at that path, Agate will look for a file named `index.gmi` inside that directory.
Optionally, set a log level via the `AGATE_LOG` environment variable. Logging is powered by the [env_logger crate](https://crates.io/crates/env_logger):
```
AGATE_LOG=info 0.0.0.0:1965 path/to.content/ cert.pem key.rsa
```
[Gemini]: https://gemini.circumlunar.space/
[Rust]: https://www.rust-lang.org/
[home]: gemini://gem.limpet.net/agate/

View file

@ -6,13 +6,15 @@ use std::{error::Error, ffi::OsStr, fs::File, io::BufReader, marker::Unpin, sync
use url::Url;
fn main() -> Result {
env_logger::Builder::from_env("AGATE_LOG").init();
task::block_on(async {
let listener = TcpListener::bind(&ARGS.sock_addr).await?;
let mut incoming = listener.incoming();
log::info!("Listening on {}...", ARGS.sock_addr);
while let Some(Ok(stream)) = incoming.next().await {
task::spawn(async {
if let Err(e) = handle_request(stream).await {
eprintln!("Error: {:?}", e);
log::error!("{:?}", e);
}
});
}
@ -110,7 +112,7 @@ async fn parse_request<R: Read + Unpin>(stream: &mut R) -> Result<Url> {
if url.scheme() != "gemini" {
Err("unsupported URL scheme")?
}
eprintln!("Got request for {:?}", url);
log::info!("Got request for {:?}", url);
Ok(url)
}
@ -146,6 +148,7 @@ async fn send_response<W: Write + Unpin>(url: Url, stream: &mut W) -> Result {
}
async fn respond<W: Write + Unpin>(stream: &mut W, status: &str, meta: &[&str]) -> Result {
log::info!("Responding with status {} and meta {:?}", status, meta);
stream.write_all(status.as_bytes()).await?;
stream.write_all(b" ").await?;
for m in meta {