relay/src/main.rs

135 lines
4 KiB
Rust
Raw Normal View History

2020-03-23 00:07:36 +00:00
use actix::Arbiter;
2020-03-23 17:38:39 +00:00
use actix_web::{middleware::Logger, web, App, HttpServer};
2020-03-15 02:05:40 +00:00
mod apub;
mod args;
mod config;
2020-03-23 22:17:53 +00:00
mod data;
mod db;
2020-03-16 03:36:46 +00:00
mod error;
mod jobs;
2020-03-23 17:38:39 +00:00
mod middleware;
mod notify;
mod requests;
2020-03-23 17:38:39 +00:00
mod routes;
2020-03-15 02:05:40 +00:00
2020-03-20 18:40:18 +00:00
use self::{
args::Args,
config::Config,
2020-03-23 22:17:53 +00:00
data::{ActorCache, State},
db::Db,
jobs::{create_server, create_workers},
2020-03-23 17:38:39 +00:00
middleware::RelayResolver,
routes::{actor, inbox, index, nodeinfo, nodeinfo_meta, statics},
2020-03-20 18:40:18 +00:00
};
2020-03-20 04:06:16 +00:00
2020-03-15 02:05:40 +00:00
#[actix_rt::main]
async fn main() -> Result<(), anyhow::Error> {
dotenv::dotenv().ok();
let config = Config::build()?;
2020-03-15 22:37:53 +00:00
if config.debug() {
2020-03-22 21:18:36 +00:00
std::env::set_var("RUST_LOG", "debug,tokio_postgres=info")
} else {
std::env::set_var("RUST_LOG", "info")
}
if config.pretty_log() {
pretty_env_logger::init();
} else {
env_logger::init();
}
2020-03-22 23:21:40 +00:00
let db = Db::build(&config).await?;
2020-03-15 02:05:40 +00:00
let args = Args::new();
2020-03-23 00:07:36 +00:00
if args.jobs_only() && args.no_jobs() {
return Err(anyhow::Error::msg(
"Either the server or the jobs must be run",
));
}
if !args.blocks().is_empty() || !args.whitelists().is_empty() {
if args.undo() {
db.remove_blocks(args.blocks()).await?;
db.remove_whitelists(args.whitelists()).await?;
} else {
db.add_blocks(args.blocks()).await?;
db.add_whitelists(args.whitelists()).await?;
}
return Ok(());
}
let state = State::hydrate(config.clone(), &db).await?;
2020-03-23 22:17:53 +00:00
let actors = ActorCache::new(db.clone());
let job_server = create_server(db.clone());
2020-03-15 02:05:40 +00:00
notify::Notifier::new(config.database_url().parse()?)
.register(notify::NewBlocks(state.clone()))
.register(notify::NewWhitelists(state.clone()))
.register(notify::NewListeners(state.clone(), job_server.clone()))
.register(notify::NewActors(actors.clone()))
.register(notify::NewNodes(state.node_cache()))
.register(notify::RmBlocks(state.clone()))
.register(notify::RmWhitelists(state.clone()))
.register(notify::RmListeners(state.clone()))
.register(notify::RmActors(actors.clone()))
.register(notify::RmNodes(state.node_cache()))
.start();
2020-03-23 00:07:36 +00:00
if args.jobs_only() {
for _ in 0..num_cpus::get() {
let state = state.clone();
2020-03-23 22:17:53 +00:00
let actors = actors.clone();
2020-03-23 00:07:36 +00:00
let job_server = job_server.clone();
Arbiter::new().exec_fn(move || {
2020-03-23 22:17:53 +00:00
create_workers(state, actors, job_server);
2020-03-23 00:07:36 +00:00
});
}
actix_rt::signal::ctrl_c().await?;
return Ok(());
}
let no_jobs = args.no_jobs();
let bind_address = config.bind_address();
2020-03-15 02:05:40 +00:00
HttpServer::new(move || {
2020-03-23 00:07:36 +00:00
if !no_jobs {
2020-03-23 22:17:53 +00:00
create_workers(state.clone(), actors.clone(), job_server.clone());
2020-03-23 00:07:36 +00:00
}
2020-03-15 02:05:40 +00:00
App::new()
2020-03-15 22:37:53 +00:00
.wrap(Logger::default())
2020-03-19 22:19:05 +00:00
.data(db.clone())
2020-03-15 02:05:40 +00:00
.data(state.clone())
2020-03-18 04:35:20 +00:00
.data(state.requests())
2020-03-23 22:17:53 +00:00
.data(actors.clone())
.data(config.clone())
.data(job_server.clone())
2020-03-15 02:05:40 +00:00
.service(web::resource("/").route(web::get().to(index)))
.service(
web::resource("/inbox")
.wrap(config.digest_middleware())
2020-03-23 22:17:53 +00:00
.wrap(config.signature_middleware(state.requests(), actors.clone()))
2020-03-23 17:38:39 +00:00
.route(web::post().to(inbox)),
)
2020-03-23 17:38:39 +00:00
.service(web::resource("/actor").route(web::get().to(actor)))
.service(web::resource("/nodeinfo/2.0.json").route(web::get().to(nodeinfo)))
2020-03-19 19:05:16 +00:00
.service(
web::scope("/.well-known")
.service(actix_webfinger::scoped::<_, RelayResolver>())
2020-03-23 17:38:39 +00:00
.service(web::resource("/nodeinfo").route(web::get().to(nodeinfo_meta))),
2020-03-19 19:05:16 +00:00
)
2020-03-23 17:38:39 +00:00
.service(web::resource("/static/{filename}").route(web::get().to(statics)))
2020-03-15 02:05:40 +00:00
})
.bind(bind_address)?
2020-03-15 02:05:40 +00:00
.run()
.await?;
Ok(())
}
2020-03-20 18:40:18 +00:00
include!(concat!(env!("OUT_DIR"), "/templates.rs"));