feat: config basics; fmt; small issues

Closes #11
This commit is contained in:
nullishamy 2025-04-23 19:13:03 +01:00
parent 918dcb60f9
commit 005c13e1d4
Signed by: amy
SSH key fingerprint: SHA256:WmV0uk6WgAQvDJlM8Ld4mFPHZo02CLXXP5VkwQ5xtyk
9 changed files with 43 additions and 14 deletions

1
Cargo.lock generated
View file

@ -335,6 +335,7 @@ dependencies = [
"rocket", "rocket",
"server", "server",
"sqlx", "sqlx",
"toml",
] ]
[[package]] [[package]]

2
config.toml Normal file
View file

@ -0,0 +1,2 @@
[server]
host = "https://ferri.amy.mov"

View file

@ -9,3 +9,4 @@ server = { path = "../ferri-server" }
rocket = { workspace = true } rocket = { workspace = true }
sqlx = { workspace = true } sqlx = { workspace = true }
clap = { version = "4", features = ["derive"] } clap = { version = "4", features = ["derive"] }
toml = "0.8.20"

View file

@ -5,17 +5,30 @@ use sqlx::sqlite::SqlitePool;
use std::env; use std::env;
use clap::Parser; use clap::Parser;
use main::config;
use std::fs;
use std::path::{Path, PathBuf};
#[derive(Parser)] #[derive(Parser)]
#[command(version, about, long_about = None)] #[command(version, about, long_about = None)]
struct Cli { struct Cli {
#[arg(short, long)] #[arg(short, long)]
init: bool, init: bool,
#[arg(short, long)]
config: PathBuf,
}
pub fn read_config(path: impl AsRef<Path>) -> config::Config {
let content = fs::read_to_string(path).unwrap();
toml::from_str(&content).unwrap()
} }
#[rocket::main] #[rocket::main]
async fn main() { async fn main() {
let cli = Cli::parse(); let cli = Cli::parse();
let config = read_config(cli.config);
if cli.init { if cli.init {
// Seed DB // Seed DB
let pool = SqlitePool::connect(&env::var("DATABASE_URL").unwrap()) let pool = SqlitePool::connect(&env::var("DATABASE_URL").unwrap())
@ -28,9 +41,9 @@ async fn main() {
INSERT INTO actor (id, inbox, outbox) INSERT INTO actor (id, inbox, outbox)
VALUES (?1, ?2, ?3) VALUES (?1, ?2, ?3)
"#, "#,
"https://ferri.amy.mov/users/c81db53f-d836-4283-a835-26606c9d14ff", "https://ferri.amy.mov/users/9b9d497b-2731-435f-a929-e609ca69dac9",
"https://ferri.amy.mov/users/c81db53f-d836-4283-a835-26606c9d14ff/inbox", "https://ferri.amy.mov/users/9b9d497b-2731-435f-a929-e609ca69dac9/inbox",
"https://ferri.amy.mov/users/c81db53f-d836-4283-a835-26606c9d14ff/outbox" "https://ferri.amy.mov/users/9b9d497b-2731-435f-a929-e609ca69dac9/outbox"
) )
.execute(&mut *conn) .execute(&mut *conn)
.await .await
@ -43,13 +56,13 @@ async fn main() {
"#, "#,
"9b9d497b-2731-435f-a929-e609ca69dac9", "9b9d497b-2731-435f-a929-e609ca69dac9",
"amy", "amy",
"https://ferri.amy.mov/users/c81db53f-d836-4283-a835-26606c9d14ff", "https://ferri.amy.mov/users/9b9d497b-2731-435f-a929-e609ca69dac9",
"amy" "amy"
) )
.execute(&mut *conn) .execute(&mut *conn)
.await .await
.unwrap(); .unwrap();
} else { } else {
let _ = launch().launch().await; let _ = launch(config).launch().await;
} }
} }

View file

@ -0,0 +1,11 @@
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
pub struct ServerConfig {
pub host: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Config {
pub server: ServerConfig,
}

View file

@ -1 +1,2 @@
pub mod ap; pub mod ap;
pub mod config;

View file

@ -76,10 +76,7 @@ pub async fn new_follow(
}; };
let req = ap::OutgoingActivity { let req = ap::OutgoingActivity {
signed_by: format!( signed_by: format!("{}#main-key", follower.uri()),
"https://ferri.amy.mov/users/{}#main-key",
follower.username()
),
req: activity, req: activity,
to: followed.actor().clone(), to: followed.actor().clone(),
}; };

View file

@ -36,7 +36,7 @@ pub async fn finger_account(mut db: Connection<Db>, account: &str) -> status::Ac
r#" r#"
INSERT INTO user (id, username, actor_id, display_name) INSERT INTO user (id, username, actor_id, display_name)
VALUES (?1, ?2, ?3, ?4) VALUES (?1, ?2, ?3, ?4)
ON CONFLICT(id) DO NOTHING ON CONFLICT(actor_id) DO NOTHING
"#, "#,
uuid, uuid,
username, username,
@ -47,7 +47,7 @@ pub async fn finger_account(mut db: Connection<Db>, account: &str) -> status::Ac
.await .await
.unwrap(); .unwrap();
status::Accepted(format!("https://ferri.amy.mov/users/{}", username)) status::Accepted(format!("https://ferri.amy.mov/users/{}", uuid))
} }
pub async fn resolve_user(acct: &str, host: &str) -> types::Person { pub async fn resolve_user(acct: &str, host: &str) -> types::Person {

View file

@ -3,6 +3,7 @@ use endpoints::{
custom, inbox, oauth, user, well_known, custom, inbox, oauth, user, well_known,
}; };
use main::ap::http; use main::ap::http;
use main::config::Config;
use rocket::{ use rocket::{
Build, Request, Rocket, build, get, Build, Request, Rocket, build, get,
http::ContentType, http::ContentType,
@ -20,7 +21,8 @@ mod types;
pub struct Db(sqlx::SqlitePool); pub struct Db(sqlx::SqlitePool);
#[get("/")] #[get("/")]
async fn user_profile() -> (ContentType, &'static str) { async fn user_profile(cfg: &rocket::State<Config>) -> (ContentType, &'static str) {
dbg!(cfg);
(ContentType::HTML, "<p>hello</p>") (ContentType::HTML, "<p>hello</p>")
} }
@ -58,9 +60,10 @@ impl<'a> FromRequest<'a> for AuthenticatedUser {
} }
} }
pub fn launch() -> Rocket<Build> { pub fn launch(cfg: Config) -> Rocket<Build> {
let http_client = http::HttpClient::new(); let http_client = http::HttpClient::new();
build() build()
.manage(cfg)
.manage(http_client) .manage(http_client)
.attach(Db::init()) .attach(Db::init())
.attach(cors::CORS) .attach(cors::CORS)