2024-04-18 04:03:52 +02:00
|
|
|
use activitypub_federation::{
|
|
|
|
|
config::{FederationConfig, FederationMiddleware},
|
|
|
|
|
http_signatures::generate_actor_keypair,
|
|
|
|
|
};
|
2024-04-09 19:48:18 +02:00
|
|
|
use actix_web::{get, http::KeepAlive, middleware, web, App, Error, HttpResponse, HttpServer};
|
|
|
|
|
use actix_web_prom::PrometheusMetricsBuilder;
|
2024-04-18 04:03:52 +02:00
|
|
|
use chrono::{DateTime, Utc};
|
2024-04-09 19:48:18 +02:00
|
|
|
use clap::Parser;
|
|
|
|
|
use database::Database;
|
|
|
|
|
use http::{http_get_user, http_post_user_inbox, webfinger};
|
|
|
|
|
use objects::person::DbUser;
|
2024-04-18 04:03:52 +02:00
|
|
|
use sea_orm::{ActiveModelTrait, DatabaseConnection, Set};
|
2024-01-26 21:01:43 +01:00
|
|
|
use serde::{Deserialize, Serialize};
|
2024-04-09 19:48:18 +02:00
|
|
|
use std::{
|
2024-04-09 19:55:07 +02:00
|
|
|
collections::HashMap,
|
|
|
|
|
env,
|
|
|
|
|
net::ToSocketAddrs,
|
|
|
|
|
sync::{Arc, Mutex},
|
2024-04-09 19:48:18 +02:00
|
|
|
};
|
2024-04-09 19:55:07 +02:00
|
|
|
use tokio::signal;
|
2024-04-15 01:07:15 +02:00
|
|
|
use tracing::info;
|
2024-04-18 04:03:52 +02:00
|
|
|
use url::Url;
|
2024-04-09 19:48:18 +02:00
|
|
|
|
2024-04-18 03:41:52 +02:00
|
|
|
use crate::database::{Config, State};
|
|
|
|
|
|
2024-04-09 19:48:18 +02:00
|
|
|
mod activities;
|
2024-04-09 19:55:07 +02:00
|
|
|
mod database;
|
2024-04-18 04:03:52 +02:00
|
|
|
mod entities;
|
2024-04-09 19:48:18 +02:00
|
|
|
mod error;
|
|
|
|
|
mod http;
|
2024-04-09 19:55:07 +02:00
|
|
|
mod objects;
|
|
|
|
|
mod utils;
|
2024-01-26 21:01:43 +01:00
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
|
struct Response {
|
|
|
|
|
health: bool,
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-09 19:48:18 +02:00
|
|
|
#[derive(Parser, Debug)]
|
|
|
|
|
#[clap(author = "April John", version, about)]
|
|
|
|
|
/// Application configuration
|
|
|
|
|
struct Args {
|
|
|
|
|
/// whether to be verbose
|
|
|
|
|
#[arg(short = 'v')]
|
|
|
|
|
verbose: bool,
|
|
|
|
|
|
|
|
|
|
/// optional parse arg for config file
|
|
|
|
|
#[arg()]
|
|
|
|
|
config_file: Option<String>,
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-26 21:01:43 +01:00
|
|
|
#[get("/")]
|
|
|
|
|
async fn index(_: web::Data<State>) -> actix_web::Result<HttpResponse, Error> {
|
|
|
|
|
Ok(HttpResponse::Ok().json(Response { health: true }))
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-09 19:48:18 +02:00
|
|
|
const DOMAIN: &str = "example.com";
|
|
|
|
|
const LOCAL_USER_NAME: &str = "example";
|
|
|
|
|
|
2024-01-26 21:01:43 +01:00
|
|
|
#[actix_web::main]
|
2024-04-09 19:48:18 +02:00
|
|
|
async fn main() -> actix_web::Result<(), anyhow::Error> {
|
2024-01-26 21:01:43 +01:00
|
|
|
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
|
|
|
|
|
|
|
|
|
let server_url = env::var("LISTEN").unwrap_or("127.0.0.1:8080".to_string());
|
2024-04-18 03:41:52 +02:00
|
|
|
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
2024-01-26 21:01:43 +01:00
|
|
|
|
2024-04-18 04:03:52 +02:00
|
|
|
let username = env::var("LOCAL_USER_NAME").unwrap_or(LOCAL_USER_NAME.to_string());
|
|
|
|
|
let domain = env::var("FEDERATED_DOMAIN").unwrap_or(DOMAIN.to_string());
|
|
|
|
|
|
|
|
|
|
let ap_id = Url::parse(&format!("https://{}/{}", domain, &username))?;
|
|
|
|
|
let inbox = Url::parse(&format!("https://{}/{}/inbox", domain, &username))?;
|
|
|
|
|
let keypair = generate_actor_keypair()?;
|
|
|
|
|
|
|
|
|
|
let user = entities::user::ActiveModel {
|
2024-04-18 19:05:22 +02:00
|
|
|
id: Set(ap_id.clone().into()),
|
2024-04-18 04:03:52 +02:00
|
|
|
username: Set(username),
|
2024-04-18 19:01:14 +02:00
|
|
|
name: Set("Test account <3".to_string()),
|
2024-04-18 04:03:52 +02:00
|
|
|
inbox: Set(inbox.to_string()),
|
|
|
|
|
public_key: Set(keypair.public_key.clone()),
|
|
|
|
|
private_key: Set(Some(keypair.private_key.clone())),
|
2024-04-18 19:14:06 +02:00
|
|
|
last_refreshed_at: Set(Utc::now()),
|
2024-04-18 19:09:10 +02:00
|
|
|
follower_count: Set(0),
|
|
|
|
|
following_count: Set(0),
|
2024-04-18 19:05:22 +02:00
|
|
|
url: Set(ap_id.to_string()),
|
2024-04-18 04:03:52 +02:00
|
|
|
local: Set(true),
|
2024-04-18 19:14:06 +02:00
|
|
|
created_at: Set(Utc::now()),
|
2024-04-18 04:03:52 +02:00
|
|
|
..Default::default()
|
|
|
|
|
};
|
2024-01-26 21:01:43 +01:00
|
|
|
|
2024-04-18 03:41:52 +02:00
|
|
|
let db = sea_orm::Database::connect(database_url).await?;
|
|
|
|
|
|
2024-04-18 18:30:15 +02:00
|
|
|
info!("Connected to database: {:?}", db);
|
|
|
|
|
|
2024-04-18 04:03:52 +02:00
|
|
|
let user = user.insert(&db).await;
|
|
|
|
|
|
2024-04-18 18:26:41 +02:00
|
|
|
if let Err(err) = user {
|
|
|
|
|
eprintln!("Error inserting user: {:?}", err);
|
|
|
|
|
} else {
|
|
|
|
|
info!("User inserted: {:?}", user.unwrap());
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-16 19:53:24 +02:00
|
|
|
let state: State = State {
|
2024-04-18 03:41:52 +02:00
|
|
|
database_connection: db.into(),
|
2024-04-16 19:53:24 +02:00
|
|
|
};
|
2024-01-26 21:01:43 +01:00
|
|
|
|
2024-04-09 19:48:18 +02:00
|
|
|
let data = FederationConfig::builder()
|
|
|
|
|
.domain(env::var("FEDERATED_DOMAIN").expect("FEDERATED_DOMAIN must be set"))
|
2024-04-18 03:41:52 +02:00
|
|
|
.app_data(state.clone())
|
2024-04-18 04:16:33 +02:00
|
|
|
.http_signature_compat(true)
|
2024-04-09 19:55:07 +02:00
|
|
|
.build()
|
|
|
|
|
.await?;
|
2024-04-09 19:48:18 +02:00
|
|
|
|
|
|
|
|
let mut labels = HashMap::new();
|
2024-04-09 19:55:07 +02:00
|
|
|
labels.insert(
|
|
|
|
|
"domain".to_string(),
|
|
|
|
|
env::var("FEDERATED_DOMAIN")
|
|
|
|
|
.expect("FEDERATED_DOMAIN must be set")
|
|
|
|
|
.to_string(),
|
|
|
|
|
);
|
|
|
|
|
labels.insert(
|
|
|
|
|
"name".to_string(),
|
|
|
|
|
env::var("LOCAL_USER_NAME")
|
|
|
|
|
.expect("LOCAL_USER_NAME must be set")
|
|
|
|
|
.to_string(),
|
|
|
|
|
);
|
2024-04-09 19:48:18 +02:00
|
|
|
|
|
|
|
|
let prometheus = PrometheusMetricsBuilder::new("api")
|
|
|
|
|
.endpoint("/metrics")
|
|
|
|
|
.const_labels(labels)
|
|
|
|
|
.build()
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
let http_server = HttpServer::new(move || {
|
2024-01-26 21:01:43 +01:00
|
|
|
App::new()
|
|
|
|
|
.app_data(web::Data::new(state.clone()))
|
|
|
|
|
.wrap(middleware::Logger::default()) // enable logger
|
2024-04-09 19:48:18 +02:00
|
|
|
.wrap(prometheus.clone())
|
|
|
|
|
.wrap(FederationMiddleware::new(data.clone()))
|
|
|
|
|
.route("/{user}", web::get().to(http_get_user))
|
|
|
|
|
.route("/{user}/inbox", web::post().to(http_post_user_inbox))
|
|
|
|
|
.route("/.well-known/webfinger", web::get().to(webfinger))
|
2024-01-26 21:01:43 +01:00
|
|
|
.service(index)
|
|
|
|
|
})
|
|
|
|
|
.bind(&server_url)?
|
2024-04-09 19:48:18 +02:00
|
|
|
.workers(num_cpus::get())
|
|
|
|
|
.shutdown_timeout(20)
|
|
|
|
|
.keep_alive(KeepAlive::Os)
|
|
|
|
|
.run();
|
|
|
|
|
|
|
|
|
|
tokio::spawn(http_server);
|
|
|
|
|
|
|
|
|
|
match signal::ctrl_c().await {
|
2024-04-09 19:55:07 +02:00
|
|
|
Ok(()) => {}
|
2024-04-09 19:48:18 +02:00
|
|
|
Err(err) => {
|
|
|
|
|
eprintln!("Unable to listen for shutdown signal: {}", err);
|
|
|
|
|
// we also shut down in case of error
|
2024-04-09 19:55:07 +02:00
|
|
|
}
|
2024-04-09 19:48:18 +02:00
|
|
|
}
|
2024-01-26 21:01:43 +01:00
|
|
|
|
2024-04-16 19:53:24 +02:00
|
|
|
info!("Main thread shutdown..");
|
2024-04-15 01:07:15 +02:00
|
|
|
|
2024-01-26 21:01:43 +01:00
|
|
|
Ok(())
|
|
|
|
|
}
|