Template Worker Development
Development guide for the template-worker service including building, testing, and code structure.
Template Worker Development
This guide covers developing and contributing to the template-worker service.
Repository
The template-worker is maintained in a separate repository:
GitHub: https://github.com/Anti-Raid/template-worker
Prerequisites
- Rust (nightly recommended, version 1.88+)
- Cargo - Rust package manager
- clang and lld - For building native dependencies
- SQLX CLI - For database migrations (if needed)
Building
From Source
# Clone the repository
git clone https://github.com/Anti-Raid/template-worker.git
cd template-worker
# Build in release mode
cargo build --release
# The binary will be at: target/release/template-workerWith SQLX Offline Mode
For builds without database access:
SQLX_OFFLINE=true cargo build --releaseDevelopment Build
For faster iteration during development:
# Debug build (faster compilation)
cargo build
# Run directly
cargo runCode Structure
The source code is organized in src/:
src/
├── api/ # HTTP API handlers and types
│ └── types.rs # API request/response types
├── events/ # Event type definitions
├── main.rs # Entry point
└── ... # Other modulesKey Components
src/api/- HTTP API handlers, routes, and typessrc/events/- Event type definitions for template dispatching- VM Management - VM lifecycle, distribution, and cleanup
- Execution Engine - Template execution and event handling
- Sandboxing - Security and isolation enforcement
- Resource Management - Memory, time, and stack limits
Testing
Unit Tests
# Run all tests
cargo test
# Run specific test
cargo test test_name
# Run with output
cargo test -- --nocaptureIntegration Tests
# Run integration tests (if available)
cargo test --test integrationTest Coverage
# Install cargo-tarpaulin (if not installed)
cargo install cargo-tarpaulin
# Generate coverage report
cargo tarpaulin --out HtmlAdding New Features
HTTP API Endpoints
- Define types in
src/api/types.rs:
#[derive(Serialize, Deserialize, utoipa::ToSchema)]
pub struct MyRequest {
pub field: String,
}-
Create handler in appropriate API module
-
Register route with OpenAPI documentation
-
Add tests for the new endpoint
OpenAPI Documentation
All new API types must be annotated with #[derive(utoipa::ToSchema)] to ensure they appear in the OpenAPI specification.
Event Types
- Define event in
src/events/:
#[derive(Serialize, Deserialize, utoipa::ToSchema)]
pub struct MyEvent {
pub data: String,
}-
Update dispatcher to handle the new event
-
Document in templating API reference
VM Distribution Strategy
-
Implement strategy interface
-
Use
ThreadEntryfor common functionality -
Register in service initialization
-
Add tests for the new strategy
Code Style
Rust Formatting
# Format code
cargo fmt
# Check formatting
cargo fmt --checkClippy Linting
# Run clippy
cargo clippy
# Fix auto-fixable issues
cargo clippy --fixDependencies
Key Dependencies
- tokio - Async runtime
- utoipa - OpenAPI documentation
- khronos - Lua VM runtime
- serde - Serialization
- sqlx - Database access
Adding Dependencies
# Add a dependency
cargo add dependency-name
# Add with features
cargo add dependency-name --features feature-nameBuilding for Production
Release Build
# Optimized release build
cargo build --release
# With specific target
cargo build --release --target x86_64-unknown-linux-gnuDocker Build
# Build Docker image
docker build -f Dockerfile -t template-worker:latest .Debugging
Debug Logging
# Set log level
RUST_LOG=debug cargo run
# Specific module
RUST_LOG=template_worker::api=debug cargo runDebugger
# Build with debug symbols
cargo build
# Attach debugger (gdb/lldb)
gdb target/debug/template-workerContributing
Pull Request Process
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new features
- Ensure all tests pass
- Submit a pull request
Code Review Checklist
- Code follows Rust style guidelines
- All tests pass
- New types have
utoipa::ToSchemaderive - Documentation is updated
- No clippy warnings
Related Documentation
- Template Worker Overview- Service overview
Template Worker Service
Developer documentation for the template-worker service - the template execution engine that runs user-defined Lua/Luau scripts in a sandboxed environment.
- Architecture- Service architecture
Template Worker Architecture
Architecture details for the template-worker service including VM distribution, lifecycle, and runtime management.
- HTTP API- API documentation
Template Worker HTTP API
HTTP API documentation for the template-worker service including endpoints, types, and OpenAPI specification.
- Deployment- Deployment guides
Template Worker Deployment
Deployment guides for the template-worker service including Docker and native deployment options.
References
Last updated on