Server-side application framework for Rust, built on top of the Tokio ecosystem. Designed to write code with less boilerplate, more simplicity, and cleaner patterns. It leverages the Tokio ecosystem to give you performance with great DX.
Sword is under active development; expect breaking changes.
- Multiple application types - Web, Socket.IO, gRPC, events, and jobs, all able to run together
- Macro-based approach - Clean, intuitive, and declarative macros for defining the application structure
- Built-in validation - Support for the
validatorcrate and an extensible validation system - Dependency Injection - Built-in DI support with declarative macros
- TOML configuration loader - Built-in support for loading configuration from TOML files
use sword::prelude::*;
use sword::web::*;
#[controller(kind = Controller::Web, path = "/users")]
pub struct UsersController {
hasher: Arc<Hasher>,
users: Arc<UserRepository>,
}
impl UsersController {
#[get("/")]
async fn get_users(&self, req: Request) -> WebResult {
let data = self.users.find_all().await?;
Ok(JsonResponse::Ok().data(data).request_id(req.id()))
}
#[post("/")]
async fn create_user(&self, req: Request) -> WebResult {
let body = req.body_validator::<CreateUserDto>()?;
let user = User::new(body.username, self.hasher.hash(&body.password)?);
if self.users.find_by_username(&user.username).await?.is_some() {
tracing::error!(
"Attempt to create user with existing username: {}",
user.username
);
Err(AppError::UserConflictError("username", &user.username))?;
}
self.users.save(&user).await?;
Ok(JsonResponse::Created().message("User created").data(user))
}
}- Controllers API
- Socket.IO Controllers Chat
- Interceptors (Web + Socket.IO controllers)
- gRPC Application with error handling
web: enables the web application typesocketio: enables Socket.IO controllers for web applications (impliesweb)grpc: enables the gRPC application typeevents-in-memory: enables in-memory event publishing and handlingjobs: enables cron/repeated/once job schedulingweb-multipart: enablesmultipart/form-datasupport in web controllersweb-swagger-ui: enables Swagger UI for OpenAPI specsgrpc-reflection: enables gRPC reflectiongrpc-error-details: enables detailed gRPC errors withGrpcStatusvalidation-validator: enables input validation with thevalidatorcratehot-reload: enables hot reload during development
See CHANGELOG.md for more details.
Contributions are welcome! Please open an issue or submit a pull request. See CONTRIBUTING.md for more details.
