Project Structure
The files ulo new writes, how its main wires an application, and where new modules go.
ulo new writes a project from templates compiled into the binary. It fetches nothing, and the tests it writes run offline. The default project serves HTTP; --transport and --db change the transport and add a database layer (see CLI Commands).
What ulo new my-app writes
my-app/
├── Cargo.toml
├── .gitignore
├── config/
│ └── app.toml the address to serve on
└── src/
├── main.rs loads the configuration, wires the application, serves until SIGINT or SIGTERM
├── app.rs AppConfig and the root module
├── greeter.rs the service the handlers share
└── controller.rs the transport's handlersWith --transport ws the handler file is src/gateway.rs. With --db the project also has src/database.rs, a service that injects the database client. A gRPC project adds build.rs and proto/greeter.proto.
Every file with logic carries a test, and the test in src/app.rs wires the whole application. cargo test therefore checks the module graph without starting a server or a database.
The project serves GET /greetings/{name}:
cargo run
curl http://127.0.0.1:3000/greetings/Ada
# {"message":"Hello, Ada!"}The root module
src/app.rs implements Module by hand rather than with #[module], because the root module holds the loaded Config: modules that read their own configuration section take it while they register.
use serde::Deserialize;
use ulo::{Module, ModuleDef, ModuleIdentity};
use ulo_config::{Config, ConfigModule};
use ulo_health::HealthModule;
use ulo_health_http::HealthHttp;
use crate::controller::GreetingController;
use crate::greeter::Greeter;
#[derive(Clone, Debug, Deserialize)]
pub struct AppConfig {
pub server: ServerConfig,
}
#[derive(Clone, Debug, Deserialize)]
pub struct ServerConfig {
pub address: String,
}
pub struct AppModule {
config: Config,
}
impl AppModule {
pub fn new(config: Config) -> Self {
AppModule { config }
}
}
impl Module for AppModule {
fn identity(&self) -> ModuleIdentity {
ModuleIdentity::of_type::<Self>()
}
fn register(&self, m: &mut ModuleDef<'_>) {
m.import(ConfigModule::<AppConfig>::new(&self.config));
m.import(HealthModule::new());
m.import(HealthHttp::new());
m.provide::<Greeter>();
m.controller::<GreetingController>();
}
}This is crates/ulo-cli/src/templates/new/common/app.rs with the HTTP transport's imports filled in. HealthModule and HealthHttp serve /health/live, /health/ready and /health/started. A module written with #[module] and one written against ModuleDef bind the same things; example 14-without-macros shows the equivalence.
The entry point
use app::{AppConfig, AppModule};
use ulo::App;
use ulo_config::{Config, Env, File};
use ulo_http_hyper::Server;
use ulo_runtime_tokio::{Tokio, shutdown_signal};
mod app;
mod controller;
mod greeter;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();
// `load` never fails: a source that cannot be read is recorded in the value and reported by
// `wire()` with every other problem.
let config = Config::builder()
.source(File::toml("config/app.toml"))
.source(Env::process().prefix("APP_").separator("__"))
.load();
let app = App::builder(AppModule::new(config))
.runtime(Tokio::current())
.wire()?
.connect()
.await?;
let settings = app.get::<AppConfig>().await?;
let app = app.bind(Server::new(settings.server.address.clone())).listen().await?;
for bound in app.addresses() {
tracing::info!(address = %bound.addr, "listening");
}
app.serve(shutdown_signal()).await?;
Ok(())
}The configuration is layered: config/app.toml first, then environment variables named APP_<SECTION>__<KEY>. APP_SERVER__ADDRESS=0.0.0.0:8080 cargo run overrides server.address. The address is read from the connected application with app.get::<AppConfig>() and the server is bound after that, so a configuration source that cannot be read fails wire() before any socket opens. See Configuration.
The manifest
The generated Cargo.toml names every ulo-* crate at the CLI's exact version (ulo = "=0.1.0-alpha.3"), so a later release never meets templates written for an earlier one. The HTTP project depends on ulo, ulo-config, ulo-runtime-tokio, ulo-health, ulo-health-http, ulo-http, ulo-http-hyper, serde, tokio, tracing and tracing-subscriber. Its rust-version is 1.88, or higher when --db needs it (1.94 for sqlx and SeaORM, 1.89 for diesel).
These resolve from crates.io, where every published ulo-* crate is at that version (see Installation).
Growing the project
Each feature is a module: it binds its providers and controllers and exports what other modules may read. A binding another module does not export is invisible to it, and wire() reports a read of one with the key, the reader and the reader's module (example 06-modules). ulo generate writes the file for a module, provider, controller, gateway or resolver and prints the lines that register it; see CLI Commands.