actix-webfinger/README.md

110 lines
3.1 KiB
Markdown
Raw 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.
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]
2019-05-12 17:24:45 +00:00
actix = "0.8"
actix-web = "1.0.0-beta.3"
actix-webfinger = "0.2.0-beta.2"
2019-01-28 20:16:51 +00:00
```
Then use it in your application
#### Client Example
```rust
2019-05-12 17:24:45 +00:00
use actix::System;
use actix_web::client::Connector;
2019-01-28 20:16:51 +00:00
use actix_webfinger::Webfinger;
2019-05-12 17:24:45 +00:00
use futures::{future::lazy, Future};
2019-01-28 20:16:51 +00:00
use openssl::ssl::{SslConnector, SslMethod};
2019-05-12 17:24:45 +00:00
use std::error::Error;
2019-01-28 20:16:51 +00:00
2019-05-12 17:24:45 +00:00
fn main() -> Result<(), Box<dyn Error>> {
let sys = System::new("sir-boops");
2019-01-28 20:16:51 +00:00
2019-05-12 17:24:45 +00:00
let ssl_conn = SslConnector::builder(SslMethod::tls())?.build();
let conn = Connector::new().ssl(ssl_conn).finish();
2019-01-28 20:16:51 +00:00
2019-05-12 17:24:45 +00:00
let fut = lazy(move || {
Webfinger::fetch(conn, "asonix@asonix.dog", "localhost:8000", false)
.map(move |w: Webfinger| {
println!("asonix's webfinger:\n{:#?}", w);
2019-01-28 20:16:51 +00:00
2019-05-12 17:24:45 +00:00
System::current().stop();
})
.map_err(|e| eprintln!("Error: {}", e))
});
2019-01-28 20:16:51 +00:00
actix::spawn(fut);
2019-05-12 17:24:45 +00:00
sys.run()?;
Ok(())
2019-01-28 20:16:51 +00:00
}
```
#### Server Example
```rust
2019-05-12 17:24:45 +00:00
use actix_web::{web::Data, App, HttpServer};
2019-01-28 20:16:51 +00:00
use actix_webfinger::{Resolver, Webfinger};
use futures::{future::IntoFuture, Future};
2019-05-12 17:24:45 +00:00
use std::error::Error;
2019-01-28 20:16:51 +00:00
#[derive(Clone, Debug)]
pub struct MyState {
domain: String,
}
pub struct MyResolver;
2019-05-12 17:24:45 +00:00
impl Resolver<Data<MyState>> for MyResolver {
2019-01-28 20:16:51 +00:00
type Error = actix_web::error::JsonPayloadError;
fn find(
account: &str,
domain: &str,
2019-05-12 17:24:45 +00:00
state: &Data<MyState>,
2019-01-28 20:16:51 +00:00
) -> Box<dyn Future<Item = Option<Webfinger>, Error = Self::Error>> {
let w = if domain == state.domain {
Some(Webfinger::new(&format!("{}@{}", account, domain)))
} else {
None
};
Box::new(Ok(w).into_future())
}
}
2019-05-12 17:24:45 +00:00
fn main() -> Result<(), Box<dyn Error>> {
HttpServer::new(|| {
App::new()
.data(MyState {
domain: "asonix.dog".to_owned(),
})
.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")?
.run()?;
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
Copyright © 2019 Riley Trautman
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/).