forked from alemi/upub
feat: added migrations
TODO gate them under feature flag eventually ™️
This commit is contained in:
parent
7876b95de5
commit
170756588d
4 changed files with 104 additions and 31 deletions
|
@ -8,6 +8,11 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
axum = "0.7.3"
|
axum = "0.7.3"
|
||||||
chrono = { version = "0.4.31", features = ["serde"] }
|
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 = { version = "1.0.193", features = ["derive"] }
|
||||||
serde_json = "1.0.108"
|
serde_json = "1.0.108"
|
||||||
tokio = { version = "1.35.1", features = ["full"] }
|
tokio = { version = "1.35.1", features = ["full"] }
|
||||||
|
tracing = "0.1.40"
|
||||||
|
tracing-subscriber = "0.3.18"
|
||||||
|
|
74
src/main.rs
74
src/main.rs
|
@ -1,46 +1,58 @@
|
||||||
pub mod model;
|
pub mod model;
|
||||||
|
pub mod migrations;
|
||||||
pub mod activitystream;
|
pub mod activitystream;
|
||||||
pub mod activitypub;
|
pub mod activitypub;
|
||||||
pub mod server;
|
pub mod server;
|
||||||
pub mod storage;
|
|
||||||
|
|
||||||
use activitystream::{types::{ActivityType, ObjectType}, Object, Type};
|
use clap::{Parser, Subcommand};
|
||||||
use axum::{extract::{Path, State}, http::StatusCode, response::IntoResponse, routing::{get, post}, Json, Router};
|
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]
|
#[tokio::main]
|
||||||
async fn 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 args = CliArgs::parse();
|
||||||
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
|
|
||||||
|
|
||||||
axum::serve(listener, app)
|
tracing_subscriber::fmt()
|
||||||
.await
|
.compact()
|
||||||
.unwrap();
|
.with_max_level(if args.debug { tracing::Level::DEBUG } else { tracing::Level::INFO })
|
||||||
}
|
.init();
|
||||||
|
|
||||||
async fn inbox(State(ctx) : State<()>, Json(object): Json<serde_json::Value>) -> Result<Json<serde_json::Value>, StatusCode> {
|
let db = Database::connect(&args.database)
|
||||||
match object.object_type() {
|
.await.expect("error connecting to db");
|
||||||
None => { Err(StatusCode::BAD_REQUEST) },
|
|
||||||
Some(Type::Activity) => { Err(StatusCode::UNPROCESSABLE_ENTITY) },
|
match args.command {
|
||||||
Some(Type::ActivityType(ActivityType::Follow)) => { todo!() },
|
CliCommand::Serve => server::serve(db)
|
||||||
Some(Type::ActivityType(ActivityType::Create)) => { todo!() },
|
.await,
|
||||||
Some(Type::ActivityType(ActivityType::Like)) => { todo!() },
|
|
||||||
Some(Type::ActivityType(x)) => { Err(StatusCode::NOT_IMPLEMENTED) },
|
CliCommand::Migrate => migrations::Migrator::up(&db, None)
|
||||||
Some(x) => { Err(StatusCode::UNPROCESSABLE_ENTITY) }
|
.await.expect("error applying migrations"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn user(State(ctx) : State<()>, Path(id): Path<String>) -> Result<Json<serde_json::Value>, StatusCode> {
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn object(State(ctx) : State<()>, Path(id): Path<String>) -> Result<Json<serde_json::Value>, StatusCode> {
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
|
|
42
src/migrations/m20240316_000001_create_table.rs
Normal file
42
src/migrations/m20240316_000001_create_table.rs
Normal file
|
@ -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,
|
||||||
|
}
|
14
src/migrations/mod.rs
Normal file
14
src/migrations/mod.rs
Normal file
|
@ -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<Box<dyn MigrationTrait>> {
|
||||||
|
vec![
|
||||||
|
Box::new(m20240316_000001_create_table::Migration)
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue