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;
|
2020-03-20 00:55:11 +00:00
|
|
|
mod args;
|
|
|
|
mod config;
|
2020-03-23 22:17:53 +00:00
|
|
|
mod data;
|
2020-03-16 17:56:26 +00:00
|
|
|
mod db;
|
2020-03-16 03:36:46 +00:00
|
|
|
mod error;
|
2020-03-21 20:24:05 +00:00
|
|
|
mod jobs;
|
2020-03-23 17:38:39 +00:00
|
|
|
mod middleware;
|
2020-03-16 17:56:26 +00:00
|
|
|
mod notify;
|
2020-03-17 17:15:16 +00:00
|
|
|
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::{
|
2020-03-21 20:24:05 +00:00
|
|
|
args::Args,
|
|
|
|
config::Config,
|
2020-03-23 22:17:53 +00:00
|
|
|
data::{ActorCache, State},
|
2020-03-21 20:24:05 +00:00
|
|
|
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();
|
|
|
|
|
2020-03-20 00:55:11 +00:00
|
|
|
let config = Config::build()?;
|
2020-03-15 22:37:53 +00:00
|
|
|
|
2020-03-20 00:55:11 +00:00
|
|
|
if config.debug() {
|
2020-03-22 21:18:36 +00:00
|
|
|
std::env::set_var("RUST_LOG", "debug,tokio_postgres=info")
|
2020-03-20 00:55:11 +00:00
|
|
|
} else {
|
|
|
|
std::env::set_var("RUST_LOG", "info")
|
|
|
|
}
|
|
|
|
|
2020-03-20 15:09:42 +00:00
|
|
|
if config.pretty_log() {
|
|
|
|
pretty_env_logger::init();
|
|
|
|
} else {
|
|
|
|
env_logger::init();
|
|
|
|
}
|
2020-03-20 00:55:11 +00:00
|
|
|
|
2020-03-22 23:21:40 +00:00
|
|
|
let db = Db::build(&config).await?;
|
2020-03-15 02:05:40 +00:00
|
|
|
|
2020-03-20 00:55:11 +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",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2020-03-20 00:55:11 +00:00
|
|
|
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());
|
2020-03-23 03:52:42 +00:00
|
|
|
let job_server = create_server(db.clone());
|
2020-03-15 02:05:40 +00:00
|
|
|
|
2020-03-25 22:10:10 +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-21 20:24:05 +00:00
|
|
|
|
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();
|
|
|
|
|
2020-03-20 00:55:11 +00:00
|
|
|
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-21 20:24:05 +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())
|
2020-03-20 00:55:11 +00:00
|
|
|
.data(config.clone())
|
2020-03-21 20:24:05 +00:00
|
|
|
.data(job_server.clone())
|
2020-03-15 02:05:40 +00:00
|
|
|
.service(web::resource("/").route(web::get().to(index)))
|
2020-03-16 04:26:31 +00:00
|
|
|
.service(
|
|
|
|
web::resource("/inbox")
|
2020-03-20 00:55:11 +00:00
|
|
|
.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-16 04:26:31 +00:00
|
|
|
)
|
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
|
|
|
})
|
2020-03-20 00:55:11 +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"));
|