1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//! Unified manage runner — collapses `src/main.rs` + `src/bin/manage.rs`
//! boilerplate into one builder so apps stop hand-writing the
//! dispatcher.
//!
//! ```ignore
//! mod apps;
//! mod settings;
//!
//! #[rustango::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! rustango::manage::Cli::new()
//! .api(apps::api())
//! .seed(apps::seed)
//! .run().await
//! }
//! ```
//!
//! `Cli::run()` reads `std::env::args()` and dispatches:
//!
//! * (no args) or `runserver` — open the pool from `DATABASE_URL`,
//! apply pending migrations, mount the user's API router, serve.
//! * everything else — forward to [`crate::migrate::manage::run`]
//! (or [`crate::tenancy::manage::run`] when [`Cli::tenancy`] is on).
//!
//! The dispatcher owns the `cargo run` vs `cargo run -- migrate` split
//! so users have one binary instead of two.
use std::future::Future;
use std::path::PathBuf;
use std::pin::Pin;
use axum::Router;
use crate::sql::sqlx::PgPool;
/// Boxed seed-hook future. Keeps the public method signature simple
/// while accepting any `async fn(&PgPool) -> Result<…>` closure.
type SeedFut<'a> =
Pin<Box<dyn Future<Output = Result<(), Box<dyn std::error::Error>>> + Send + 'a>>;
type SeedFn = Box<dyn for<'a> FnOnce(&'a PgPool) -> SeedFut<'a> + Send>;
/// One-builder dispatcher. Hand it your API router (and optionally a
/// seed hook), call [`Cli::run`], and you're done.
#[must_use = "Cli does nothing until .run() is awaited"]
pub struct Cli {
api: Router,
seed: Option<SeedFn>,
bind: String,
migrations_dir: PathBuf,
tenancy: bool,
/// Bootstrap initializer used by the `init-tenancy` verb when
/// [`Cli::tenancy`] is on. Defaults to
/// [`crate::tenancy::init_tenancy`]; replaced by [`Cli::user_model`]
/// to swap in a custom [`crate::tenancy::TenantUserModel`].
#[cfg(feature = "tenancy")]
init_tenancy_fn: crate::tenancy::manage::InitTenancyFn,
}
impl Cli {
/// Default builder — empty router, no seed, binds `0.0.0.0:8080`,
/// migrations live in `./migrations`, single-tenant.
#[must_use]
pub fn new() -> Self {
Self {
api: Router::new(),
seed: None,
bind: std::env::var("RUSTANGO_BIND").unwrap_or_else(|_| "0.0.0.0:8080".into()),
migrations_dir: PathBuf::from("./migrations"),
tenancy: false,
#[cfg(feature = "tenancy")]
init_tenancy_fn: crate::tenancy::init_tenancy,
}
}
/// Mount the user's stateless API router. Pool is injected via
/// `axum::Extension<PgPool>` at serve time so handlers can pull
/// it without managing state themselves.
#[must_use]
pub fn api(mut self, router: Router) -> Self {
self.api = router;
self
}
/// Run a one-shot async hook on first boot — typical use is
/// inserting a demo tenant or a seed superuser. The hook receives
/// the registry pool (or single-tenant pool when [`Cli::tenancy`]
/// is off).
#[must_use]
pub fn seed<F, Fut>(mut self, hook: F) -> Self
where
F: for<'a> FnOnce(&'a PgPool) -> Fut + Send + 'static,
Fut: Future<Output = Result<(), Box<dyn std::error::Error>>> + Send + 'static,
{
self.seed = Some(Box::new(move |pool| Box::pin(hook(pool))));
self
}
/// Override the bind address. Defaults to `RUSTANGO_BIND` env or
/// `0.0.0.0:8080`.
#[must_use]
pub fn bind(mut self, addr: impl Into<String>) -> Self {
self.bind = addr.into();
self
}
/// Override the migrations directory. Defaults to `./migrations`.
#[must_use]
pub fn migrations_dir(mut self, dir: impl Into<PathBuf>) -> Self {
self.migrations_dir = dir.into();
self
}
/// Switch dispatch to the multi-tenant code path —
/// [`crate::tenancy::manage::run`] handles `create-tenant`,
/// `migrate-tenants`, `create-operator`, `create-user` plus every
/// single-tenant verb. `runserver` defers to
/// [`crate::server::Builder`].
#[must_use]
pub fn tenancy(mut self) -> Self {
self.tenancy = true;
self
}
/// Swap the tenant user model used by the `init-tenancy` verb.
/// Implement [`crate::tenancy::TenantUserModel`] on a model that
/// declares extra columns on `rustango_users` (display name,
/// timezone, …) and pass it here — the materialized bootstrap
/// migration will then `CREATE TABLE` with those extras included.
///
/// Only meaningful in tenancy mode and only on the very first
/// `init-tenancy`: subsequent invocations are idempotent and
/// won't rewrite the migration JSON.
///
/// ```ignore
/// rustango::manage::Cli::new()
/// .api(apps::api())
/// .tenancy()
/// .user_model::<myapp::AppUser>()
/// .run().await
/// ```
#[cfg(feature = "tenancy")]
#[must_use]
pub fn user_model<U: crate::tenancy::TenantUserModel>(mut self) -> Self {
self.init_tenancy_fn = crate::tenancy::init_tenancy_with::<U>;
self
}
/// Read argv, dispatch.
///
/// # Errors
/// Surfaces whatever the underlying dispatcher / server returns.
pub async fn run(self) -> Result<(), Box<dyn std::error::Error>> {
let args: Vec<String> = std::env::args().skip(1).collect();
let verb = args.first().map_or("", String::as_str);
match verb {
"" | "runserver" => self.runserver().await,
_ => self.dispatch(args).await,
}
}
async fn dispatch(self, args: Vec<String>) -> Result<(), Box<dyn std::error::Error>> {
// Verbs that print info and never touch the DB. We let these
// run even when DATABASE_URL is unset so users can scaffold or
// read help without configuring Postgres first.
let no_db_verb = matches!(
args.first().map(String::as_str),
Some("help")
| Some("--help")
| Some("-h")
| Some("startapp")
| Some("makemigrations")
| Some("docs")
| Some("version")
| Some("--version")
| Some("make:viewset")
| Some("make:serializer")
| Some("make:form")
| Some("make:job")
| Some("make:notification")
| Some("make:middleware")
| Some("make:test")
);
let url = std::env::var("DATABASE_URL").unwrap_or_else(|_| "postgres://offline".into());
if !no_db_verb && std::env::var("DATABASE_URL").is_err() {
return Err("missing env var `DATABASE_URL`. Set it in your shell, or copy `.env.example` to `.env`.".into());
}
#[cfg(feature = "tenancy")]
if self.tenancy {
let pool = if no_db_verb {
PgPool::connect_lazy(&url)?
} else {
PgPool::connect(&url).await?
};
let pools = crate::tenancy::TenantPools::new(pool);
crate::tenancy::manage::run_with_init(
&pools,
&url,
&self.migrations_dir,
args,
self.init_tenancy_fn,
)
.await?;
return Ok(());
}
#[cfg(not(feature = "tenancy"))]
if self.tenancy {
return Err("Cli::tenancy() requires the `tenancy` feature".into());
}
let pool = if no_db_verb {
PgPool::connect_lazy(&url)?
} else {
PgPool::connect(&url).await?
};
crate::migrate::manage::run(&pool, &self.migrations_dir, args).await?;
Ok(())
}
async fn runserver(self) -> Result<(), Box<dyn std::error::Error>> {
#[cfg(feature = "tenancy")]
if self.tenancy {
return self.runserver_tenancy().await;
}
#[cfg(not(feature = "tenancy"))]
if self.tenancy {
return Err("Cli::tenancy() requires the `tenancy` feature".into());
}
let url = std::env::var("DATABASE_URL").map_err(|_| {
"missing env var `DATABASE_URL`. Set it in your shell, or copy `.env.example` to `.env`."
})?;
let pool = PgPool::connect(&url).await?;
let _ = crate::migrate::migrate(&pool, &self.migrations_dir).await?;
if let Some(seed) = self.seed {
seed(&pool).await?;
}
let app = self.api.layer(axum::Extension(pool));
let listener = tokio::net::TcpListener::bind(&self.bind).await?;
eprintln!("server listening on http://{}", listener.local_addr()?);
axum::serve(listener, app).await?;
Ok(())
}
#[cfg(feature = "tenancy")]
async fn runserver_tenancy(self) -> Result<(), Box<dyn std::error::Error>> {
let mut builder = crate::server::Builder::from_env().await?.api(self.api);
if let Some(seed) = self.seed {
// Tenancy Builder's seed_with takes (Arc<TenantPools>, PgPool,
// String); we forward the registry pool and discard the rest.
builder = builder
.seed_with(move |_pools, registry, _url| async move { seed(®istry).await })
.await?;
}
builder.serve(&self.bind).await
}
}
impl Default for Cli {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn defaults_are_sensible() {
let cli = Cli::new().bind("0.0.0.0:8080"); // pin past any inherited RUSTANGO_BIND
assert_eq!(cli.bind, "0.0.0.0:8080");
assert_eq!(cli.migrations_dir, std::path::PathBuf::from("./migrations"));
assert!(!cli.tenancy);
assert!(cli.seed.is_none());
}
#[test]
fn builder_methods_chain() {
let cli = Cli::new()
.bind("127.0.0.1:7777")
.migrations_dir("custom/migrations")
.tenancy();
assert_eq!(cli.bind, "127.0.0.1:7777");
assert_eq!(
cli.migrations_dir,
std::path::PathBuf::from("custom/migrations")
);
assert!(cli.tenancy);
}
#[test]
fn seed_hook_stored() {
let cli = Cli::new().seed(|_pool| async { Ok(()) });
assert!(cli.seed.is_some());
}
#[test]
fn default_impl_matches_new() {
let a = Cli::default();
let b = Cli::new();
assert_eq!(a.bind, b.bind);
assert_eq!(a.migrations_dir, b.migrations_dir);
assert_eq!(a.tenancy, b.tenancy);
}
}