actix-webfinger/README.md

113 lines
3.3 KiB
Markdown
Raw Permalink Normal View History

2019-01-27 20:45:44 +00:00
# Actix Webfinger
2019-01-28 20:16:51 +00:00
A library to aid in resolving and providing webfinger objects with the Actix Web web framework.
2019-07-30 22:37:43 +00:00
- [Read the documentation on docs.rs](https://docs.rs/actix-webfinger)
- [Find the crate on crates.io](https://crates.io/crates/actix-webfinger)
2020-03-16 01:03:35 +00:00
- [Hit me up on Mastodon](https://asonix.dog/@asonix)
2019-07-30 22:37:43 +00:00
2019-01-28 20:16:51 +00:00
The main functionality this crate provides is through the `Webfinger::fetch` method for Actix
Web-based clients, and the `Resolver<S>` trait for Actix Web-based servers.
### Usage
First, add Actix Webfinger as a dependency
```toml
[dependencies]
2022-03-08 17:56:05 +00:00
actix-rt = "2.6.0"
actix-web = "4.0.0"
actix-webfinger = "0.4.0"
2019-01-28 20:16:51 +00:00
```
Then use it in your application
#### Client Example
```rust
use actix_webfinger::Webfinger;
2022-03-08 17:56:05 +00:00
use awc::Client;
2019-05-12 17:24:45 +00:00
use std::error::Error;
2019-01-28 20:16:51 +00:00
2020-03-16 01:03:35 +00:00
#[actix_rt::main]
async fn main() -> Result<(), Box<dyn Error>> {
let client = Client::default();
2022-03-08 17:56:05 +00:00
let wf = Webfinger::fetch(
&client,
Some("acct:"),
"asonix@localhost:8000",
"localhost:8000",
false,
)
.await?;
2019-01-28 20:16:51 +00:00
2020-03-16 01:03:35 +00:00
println!("asonix's webfinger:\n{:#?}", wf);
2019-05-12 17:24:45 +00:00
Ok(())
2019-01-28 20:16:51 +00:00
}
```
#### Server Example
```rust
2022-03-08 17:56:05 +00:00
use actix_web::{middleware::Logger, web::Data, App, HttpServer};
2019-01-28 20:16:51 +00:00
use actix_webfinger::{Resolver, Webfinger};
2020-03-16 01:03:35 +00:00
use std::{error::Error, future::Future, pin::Pin};
2019-01-28 20:16:51 +00:00
#[derive(Clone, Debug)]
pub struct MyState {
domain: String,
}
pub struct MyResolver;
2022-03-08 17:56:05 +00:00
type LocalBoxFuture<'a, Output> = Pin<Box<dyn Future<Output = Output> + 'a>>;
2020-04-21 19:08:59 +00:00
impl Resolver for MyResolver {
type State = Data<MyState>;
2019-01-28 20:16:51 +00:00
type Error = actix_web::error::JsonPayloadError;
fn find(
2022-03-08 17:56:05 +00:00
scheme: Option<&str>,
2019-01-28 20:16:51 +00:00
account: &str,
domain: &str,
2021-02-04 01:17:01 +00:00
state: Data<MyState>,
2022-03-08 17:56:05 +00:00
) -> LocalBoxFuture<'static, Result<Option<Webfinger>, Self::Error>> {
let w = if scheme == Some("acct:") && domain == state.domain {
2019-01-28 20:16:51 +00:00
Some(Webfinger::new(&format!("{}@{}", account, domain)))
} else {
None
};
2020-03-16 01:03:35 +00:00
Box::pin(async move { Ok(w) })
2019-01-28 20:16:51 +00:00
}
}
2020-03-16 01:03:35 +00:00
#[actix_rt::main]
async fn main() -> Result<(), Box<dyn Error>> {
2022-03-08 17:56:05 +00:00
std::env::set_var("RUST_LOG", "info");
pretty_env_logger::init();
2019-05-12 17:24:45 +00:00
HttpServer::new(|| {
App::new()
2022-03-08 17:56:05 +00:00
.app_data(Data::new(MyState {
domain: "localhost:8000".to_owned(),
}))
.wrap(Logger::default())
.service(actix_webfinger::resource::<MyResolver>())
2019-01-28 20:16:51 +00:00
})
2019-05-12 17:24:45 +00:00
.bind("127.0.0.1:8000")?
2020-03-16 01:03:35 +00:00
.run()
.await?;
2019-05-12 17:24:45 +00:00
Ok(())
2019-01-28 20:16:51 +00:00
}
```
### Contributing
Feel free to open issues for anything you find an issue with. Please note that any contributed code will be licensed under the GPLv3.
### License
2020-03-16 01:03:35 +00:00
Copyright © 2020 Riley Trautman
2019-01-28 20:16:51 +00:00
Actix Webfinger is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Actix Webfinger is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. This file is part of Tokio ZMQ.
You should have received a copy of the GNU General Public License along with Actix Webfinger. If not, see [http://www.gnu.org/licenses/](http://www.gnu.org/licenses/).