Skip to content
プロジェクト

概要

Rust server-side application framework

Web・アプリ開発

この数値の測り方

できること

  • 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 validator crate and an extensible validation system
  • Dependency Injection - Built-in DI support with declarative macros

計測したスター増加

24時間
0
7日間
+6
15日間
+6
1か月
+10

期間ごとの純増数です。期間は重複しており、日別の値ではありません。未計測の値は空欄です。スター数は注目度の指標であり、品質の保証ではありません。

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.

Features

  • 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 validator crate 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

Web Controller Example

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))
    }
}

Full Examples

Application Features

Sword provides a set of application types based on cargo features:

  • web: enables the web application type
  • socketio: enables Socket.IO controllers for web applications (implies web)
  • grpc: enables the gRPC application type
  • events: enables event handling application primitives
  • jobs: enables cron/repeated/once job scheduling

Also gives a set of sub-features for the top-level applications:

Web Features:

  • web-compression: enables response compression
  • web-cors: enables CORS handling
  • web-helmet: enables security headers through helmet
  • web-multipart: enables multipart/form-data support in web controllers
  • web-scalar-ui: enables Scalar API reference for OpenAPI specs
  • web-servedir: serves a static directory from the file system
  • web-swagger-ui: enables Swagger UI for OpenAPI specs
  • web-validator: enables input validation for web controllers

Socket.IO Features:

  • socketio-validator: enables input validation for Socket.IO controllers

Grpc Features:

  • grpc-reflection: enables gRPC reflection
  • grpc-error-details: enables detailed gRPC errors with GrpcStatus

Events Features:

  • events-memory: enables in-memory pub/sub event handling
  • events-nats: enables NATS event handling support

Cross type sub-features:

  • hot-reload: enables hot reload during development

Changelog

See CHANGELOG.md for more details.

Contributing

Contributions are welcome! Please open an issue or submit a pull request. See CONTRIBUTING.md for more details.

Requirements

  • Rust 1.95 or newer (MSRV).

関連プロジェクトを探す

読み込み中…