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]
|
||||
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"
|
||||
|
|
74
src/main.rs
74
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<serde_json::Value>) -> Result<Json<serde_json::Value>, 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<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