6.6 KiB
name, description
| name | description |
|---|---|
| tokio-scaffold | Scaffold new Tokio projects with proper structure and best practices |
Tokio Scaffold Command
This command scaffolds new Tokio-based Rust projects with modern structure, proper dependencies, error handling patterns, tracing infrastructure, and testing setup.
Arguments
$1- Project name (required)$2- Project type:http-server,grpc-server,tcp-server,cli, orlibrary(optional, defaults tolibrary)
Usage
/rust-tokio-expert:tokio-scaffold my-service http-server
/rust-tokio-expert:tokio-scaffold my-cli cli
/rust-tokio-expert:tokio-scaffold my-lib library
Workflow
-
Validate Arguments
- Check that project name is provided
- Validate project type if provided
- Ensure target directory doesn't already exist
-
Invoke Agent
- Use Task tool with
subagent_type="rust-tokio-expert:tokio-pro" - Pass project name and type to the agent
- Use Task tool with
-
Agent Instructions
The agent should create a complete project structure based on the type:
For HTTP Server Projects
Create:
-
Cargo.tomlwith dependencies:- tokio with full features
- axum for HTTP framework
- tower and tower-http for middleware
- serde and serde_json for serialization
- tracing and tracing-subscriber for logging
- anyhow and thiserror for error handling
- sqlx (optional) for database
- config for configuration management
-
src/main.rswith:- Runtime setup with tracing
- Router configuration
- Graceful shutdown handling
- Health check endpoints
-
src/handlers/mod.rswith example HTTP handlers -
src/error.rswith custom error types -
src/config.rswith configuration loading -
src/telemetry.rswith tracing setup -
tests/integration_test.rswith API integration tests -
.env.examplewith configuration template -
README.mdwith usage instructions
For gRPC Server Projects
Create:
-
Cargo.tomlwith:- tokio with full features
- tonic and tonic-build
- prost for protobuf
- tower for middleware
- tracing infrastructure
- error handling crates
-
proto/service.protowith example service definition -
build.rsfor proto compilation -
src/main.rswith gRPC server setup -
src/service.rswith service implementation -
src/error.rswith error types -
tests/integration_test.rs
For TCP Server Projects
Create:
-
Cargo.tomlwith:- tokio with io-util, net features
- tokio-util with codec
- bytes for buffer management
- tracing infrastructure
-
src/main.rswith TCP server setup -
src/protocol.rswith protocol definition -
src/handler.rswith connection handler -
tests/integration_test.rs
For CLI Projects
Create:
-
Cargo.tomlwith:- tokio with full features
- clap for argument parsing
- anyhow for error handling
- tracing-subscriber for logging
-
src/main.rswith CLI setup -
src/commands/mod.rswith command structure -
src/config.rswith configuration -
tests/cli_test.rs
For Library Projects
Create:
-
Cargo.tomlwith:- tokio as optional dependency
- async-trait
- thiserror for errors
-
src/lib.rswith library structure -
tests/lib_test.rswith comprehensive tests -
examples/basic.rswith usage example -
README.mdwith API documentation
-
-
Common Files for All Types
.gitignorewith Rust-specific ignoresCargo.tomlwith proper metadatarustfmt.tomlwith formatting rulesclippy.tomlwith linting configuration (if needed)
-
Initialize Testing
For all project types:
- Add
#[tokio::test]examples - Include timeout tests
- Add mock/test utilities
- Set up test helpers
- Add
-
Documentation
Generate
README.mdwith:- Project description
- Requirements
- Installation instructions
- Usage examples
- Development setup
- Testing instructions
- Contributing guidelines
-
Verification
After scaffolding:
- Run
cargo checkto verify compilation - Run
cargo testto verify tests - Report any issues found
- Run
Example Cargo.toml Template (HTTP Server)
[package]
name = "{{project_name}}"
version = "0.1.0"
edition = "2021"
[dependencies]
tokio = { version = "1", features = ["full"] }
axum = "0.7"
tower = "0.4"
tower-http = { version = "0.5", features = ["trace", "compression-gzip"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter", "json"] }
anyhow = "1"
thiserror = "1"
config = "0.14"
[dev-dependencies]
tokio-test = "0.4"
Example Main Template (HTTP Server)
use axum::{Router, routing::get};
use std::net::SocketAddr;
use tower_http::trace::TraceLayer;
mod handlers;
mod error;
mod config;
mod telemetry;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Initialize telemetry
telemetry::init()?;
// Load configuration
let config = config::load()?;
// Create router
let app = Router::new()
.route("/health", get(handlers::health_check))
.route("/api/v1/users", get(handlers::list_users))
.layer(TraceLayer::new_for_http());
// Start server
let addr = SocketAddr::from(([0, 0, 0, 0], config.port));
tracing::info!("Starting server on {}", addr);
let listener = tokio::net::TcpListener::bind(addr).await?;
axum::serve(listener, app)
.with_graceful_shutdown(shutdown_signal())
.await?;
Ok(())
}
async fn shutdown_signal() {
tokio::signal::ctrl_c()
.await
.expect("failed to install CTRL+C signal handler");
}
Best Practices
The scaffolded project should follow these best practices:
- Error Handling: Use
thiserrorfor domain errors,anyhowfor application errors - Configuration: Use environment variables with sensible defaults
- Logging: Use
tracingwith structured logging - Testing: Include both unit and integration tests
- Documentation: Generate comprehensive README with examples
- Security: Include basic security headers and validation
- Performance: Configure runtime appropriately for workload type
- Observability: Include metrics and health check endpoints
Notes
- Always use the latest stable versions of dependencies
- Include comments explaining key architectural decisions
- Provide both simple and advanced usage examples
- Generate projects that compile and pass tests out of the box
- Follow Rust API guidelines and naming conventions