diff --git a/Cargo.toml b/Cargo.toml index a52bed75..6336b331 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,11 @@ edition = "2021" [dependencies] axum = "0.7.3" chrono = { version = "0.4.31", features = ["serde"] } +clap = { version = "4.5.3", features = ["derive"] } +sea-orm = { version = "0.12.14", features = ["macros", "sqlx-sqlite", "runtime-tokio-rustls"] } +sea-orm-migration = "0.12.15" serde = { version = "1.0.193", features = ["derive"] } serde_json = "1.0.108" tokio = { version = "1.35.1", features = ["full"] } +tracing = "0.1.40" +tracing-subscriber = "0.3.18" diff --git a/src/main.rs b/src/main.rs index 9ff0730a..e84c9eea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,46 +1,58 @@ pub mod model; +pub mod migrations; pub mod activitystream; pub mod activitypub; pub mod server; -pub mod storage; -use activitystream::{types::{ActivityType, ObjectType}, Object, Type}; -use axum::{extract::{Path, State}, http::StatusCode, response::IntoResponse, routing::{get, post}, Json, Router}; +use clap::{Parser, Subcommand}; +use sea_orm::Database; +use sea_orm_migration::MigratorTrait; + +#[derive(Parser)] +/// all names were taken +struct CliArgs { + #[clap(subcommand)] + /// command to run + command: CliCommand, + + #[arg(short, long, default_value = "sqlite://./anwt.db")] + /// database connection uri + database: String, + + #[arg(long, default_value_t=false)] + /// run with debug level tracing + debug: bool, +} + +#[derive(Clone, Subcommand)] +enum CliCommand { + /// run fediverse server + Serve , + + /// apply database migrations + Migrate, +} #[tokio::main] async fn main() { - // build our application with a single route - let app = Router::new() - .with_state(()) - .route("/inbox", post(inbox)) - .route("/outbox", get(|| async { todo!() })) - .route("/users/:id", get(user)) - .route("/objects/:id", get(object)); - // run our app with hyper, listening globally on port 3000 - let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); + let args = CliArgs::parse(); - axum::serve(listener, app) - .await - .unwrap(); -} + tracing_subscriber::fmt() + .compact() + .with_max_level(if args.debug { tracing::Level::DEBUG } else { tracing::Level::INFO }) + .init(); -async fn inbox(State(ctx) : State<()>, Json(object): Json) -> Result, StatusCode> { - match object.object_type() { - None => { Err(StatusCode::BAD_REQUEST) }, - Some(Type::Activity) => { Err(StatusCode::UNPROCESSABLE_ENTITY) }, - Some(Type::ActivityType(ActivityType::Follow)) => { todo!() }, - Some(Type::ActivityType(ActivityType::Create)) => { todo!() }, - Some(Type::ActivityType(ActivityType::Like)) => { todo!() }, - Some(Type::ActivityType(x)) => { Err(StatusCode::NOT_IMPLEMENTED) }, - Some(x) => { Err(StatusCode::UNPROCESSABLE_ENTITY) } + let db = Database::connect(&args.database) + .await.expect("error connecting to db"); + + match args.command { + CliCommand::Serve => server::serve(db) + .await, + + CliCommand::Migrate => migrations::Migrator::up(&db, None) + .await.expect("error applying migrations"), } } -async fn user(State(ctx) : State<()>, Path(id): Path) -> Result, StatusCode> { - todo!() -} -async fn object(State(ctx) : State<()>, Path(id): Path) -> Result, StatusCode> { - todo!() -} diff --git a/src/migrations/m20240316_000001_create_table.rs b/src/migrations/m20240316_000001_create_table.rs new file mode 100644 index 00000000..ba286fd9 --- /dev/null +++ b/src/migrations/m20240316_000001_create_table.rs @@ -0,0 +1,42 @@ +use sea_orm_migration::prelude::*; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager. + create_table( + Table::create() + .table(Users::Table) + .if_not_exists() + .col( + ColumnDef::new(Users::Id) + .string() + .not_null() + .primary_key() + ) + .col(ColumnDef::new(Users::Name).string().null()) + .to_owned() + ) + .await?; + + Ok(()) + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager. + drop_table(Table::drop().table(Users::Table).to_owned()) + .await?; + + Ok(()) + } +} + +#[derive(DeriveIden)] +enum Users { + Table, + Id, + Name, +} diff --git a/src/migrations/mod.rs b/src/migrations/mod.rs new file mode 100644 index 00000000..01820ada --- /dev/null +++ b/src/migrations/mod.rs @@ -0,0 +1,14 @@ +use sea_orm_migration::prelude::*; + +mod m20240316_000001_create_table; + +pub struct Migrator; + +#[async_trait::async_trait] +impl MigratorTrait for Migrator { + fn migrations() -> Vec> { + vec![ + Box::new(m20240316_000001_create_table::Migration) + ] + } +}