relay/src/main.rs

90 lines
2.7 KiB
Rust
Raw Normal View History

2020-03-15 02:05:40 +00:00
#![feature(drain_filter)]
2020-03-15 22:37:53 +00:00
use activitystreams::{actor::apub::Application, context, endpoint::EndpointProperties};
use actix_web::{client::Client, middleware::Logger, web, App, HttpServer, Responder};
2020-03-15 02:05:40 +00:00
use bb8_postgres::tokio_postgres;
mod apub;
mod db_actor;
mod inbox;
mod label;
mod state;
2020-03-15 22:37:53 +00:00
use self::{
db_actor::DbActor,
inbox::MyError,
label::ArbiterLabelFactory,
state::{State, UrlKind},
};
2020-03-15 02:05:40 +00:00
async fn index() -> impl Responder {
"hewwo, mr obama"
}
2020-03-15 22:37:53 +00:00
async fn actor_route(state: web::Data<State>) -> Result<impl Responder, MyError> {
let mut application = Application::default();
let mut endpoint = EndpointProperties::default();
endpoint.set_shared_inbox(format!("https://{}/inbox", "localhost"))?;
application
.object_props
.set_id(state.generate_url(UrlKind::Actor))?
.set_summary_xsd_string("AodeRelay bot")?
.set_name_xsd_string("AodeRelay")?
.set_url_xsd_any_uri(state.generate_url(UrlKind::Actor))?
.set_context_xsd_any_uri(context())?;
application
.ap_actor_props
.set_preferred_username("relay")?
.set_followers(state.generate_url(UrlKind::Followers))?
.set_following(state.generate_url(UrlKind::Following))?
.set_inbox(state.generate_url(UrlKind::Inbox))?
.set_endpoints(endpoint)?;
Ok(inbox::response(application))
}
2020-03-15 02:05:40 +00:00
#[actix_rt::main]
async fn main() -> Result<(), anyhow::Error> {
dotenv::dotenv().ok();
std::env::set_var("RUST_LOG", "info");
pretty_env_logger::init();
let pg_config: tokio_postgres::Config = std::env::var("DATABASE_URL")?.parse()?;
2020-03-15 22:37:53 +00:00
let hostname: String = std::env::var("HOSTNAME")?;
let use_whitelist = std::env::var("USE_WHITELIST").is_ok();
let use_https = std::env::var("USE_HTTPS").is_ok();
2020-03-15 02:05:40 +00:00
let arbiter_labeler = ArbiterLabelFactory::new();
let db_actor = DbActor::new(pg_config.clone());
arbiter_labeler.clone().set_label();
let state: State = db_actor
2020-03-15 22:37:53 +00:00
.send(db_actor::DbQuery(move |pool| {
State::hydrate(use_https, use_whitelist, hostname, pool)
}))
2020-03-15 02:05:40 +00:00
.await?
.await??;
HttpServer::new(move || {
let actor = DbActor::new(pg_config.clone());
arbiter_labeler.clone().set_label();
let client = Client::default();
App::new()
2020-03-15 22:37:53 +00:00
.wrap(Logger::default())
2020-03-15 02:05:40 +00:00
.data(actor)
.data(state.clone())
.data(client)
.service(web::resource("/").route(web::get().to(index)))
.service(web::resource("/inbox").route(web::post().to(inbox::inbox)))
2020-03-15 22:37:53 +00:00
.service(web::resource("/actor").route(web::get().to(actor_route)))
2020-03-15 02:05:40 +00:00
})
.bind("127.0.0.1:8080")?
.run()
.await?;
Ok(())
}