chore: move main cli to toplevel

This commit is contained in:
əlemi 2024-06-21 20:50:47 +02:00
parent 9004e59969
commit f94afcd8b8
Signed by: alemi
GPG key ID: A4895B84D311642C
4 changed files with 2017 additions and 227 deletions

2120
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -1,11 +1,32 @@
[workspace]
members = [
"migrations",
"server",
"model",
]
[package]
name = "backend"
name = "scct-cli"
version = "0.1.0"
edition = "2021"
[[bin]]
name = "scct"
path = "main.rs"
[dependencies]
axum = "0.7.5"
clap = { version = "4.5.7", features = ["derive"] }
tokio = { version = "1.38.0", features = ["full"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
clap = { version = "4.5.7", features = ["derive"] }
# TODO can we move tokio and sea-orm inside individual crates?
tokio = { version = "1.38.0", features = ["full"] }
sea-orm = { version = "0.12.15", features = ["runtime-tokio-rustls", "sqlx-mysql", "sqlx-sqlite", "sqlx-postgres"] }
scct-migrations = { path = "migrations", optional = true }
scct-server = { path = "server", optional = true }
tracing = "0.1.40"
[features]
default = ["server", "migrations"]
migrations = ["dep:scct-migrations"]
server = ["dep:scct-server"]

62
main.rs Normal file
View file

@ -0,0 +1,62 @@
use clap::{Parser, Subcommand};
/// Super Cool Chat Thing c:
#[derive(Parser)]
struct Args {
#[clap(subcommand)]
cmd: Command,
#[arg(long, default_value = "sqlite://./scct.db")]
/// connection string to database
db: String,
#[arg(long)]
/// run with debug log level
debug: bool,
}
#[derive(Clone, Subcommand)]
enum Command {
#[cfg(feature = "migrations")]
/// apply scct migrations
Migrate,
#[cfg(feature = "server")]
/// run scct server
Serve {
/// address+port to bind onto
#[arg(short, long, default_value = "127.0.0.1:8424")]
addr: String,
}
}
#[tokio::main]
async fn main() {
let args = Args::parse();
tracing_subscriber::fmt()
.compact()
.with_max_level(if args.debug { tracing::Level::DEBUG } else { tracing::Level::INFO })
.init();
let db = sea_orm::Database::connect(args.db)
.await
.expect("could not connect to scct database");
match args.cmd {
#[cfg(feature = "migrations")]
Command::Migrate => {
use scct_migrations::MigratorTrait;
scct_migrations::Migrator::up(&db, None)
.await
.expect("could not apply scct migrations");
},
#[cfg(feature = "server")]
Command::Serve { addr } =>
scct_server::serve(&addr)
.await
.expect("scct server terminated with exception"),
}
}

View file

@ -1,31 +0,0 @@
use axum::{response::IntoResponse, routing::get, Router};
use clap::Parser;
#[derive(Parser)]
struct Args {
/// address+port to bind onto
#[arg(short, long, default_value = "127.0.0.1:8424")]
addr: String,
}
#[tokio::main]
async fn main() {
let args = Args::parse();
tracing_subscriber::fmt::init();
let app = Router::new()
.route("/", get(root));
let listener = tokio::net::TcpListener::bind(&args.addr)
.await
.expect("failed binding to address");
axum::serve(listener, app).await.unwrap();
}
async fn root() -> impl IntoResponse {
"hello world :3"
}