REST Controllers
Relevant source files
The following files were used as context for generating this wiki page:
REST Controllers in the SeaORM Pro project serve as the interface between the HTTP transport layer and the underlying business logic and database entities. They are responsible for handling incoming requests, performing authentication checks, interacting with the SeaORM database layer, and formatting responses.
The project utilizes both the loco-rs framework for its backend services and axum for specific examples. These controllers facilitate administrative tasks, user authentication, and provide endpoints for the dynamic admin panel configuration.
Sources: src/controllers/mod.rs, examples/axum_example/api/src/lib.rs:43-85
Controller Architecture and Routing
The controller system is modularized into specialized modules under the controllers directory. Each module defines its own routes and handlers, which are then integrated into the main application router.
Controller Modules
| Module | Responsibility |
|---|---|
admin | Handles admin panel configuration and dashboard data visualization. |
auth | Manages authentication and JWT-based security. |
graphql | Provides endpoints for the GraphQL playground and request handling. |
user | Manages user-specific data and profiles. |
Sources: src/controllers/mod.rs:1-4, src/controllers/admin.rs:163-171
Data Flow for Request Handling
The following diagram illustrates how a request flows through the controller layer to the database and back.
The system ensures that sensitive endpoints (like the Dashboard) are protected via JWT authentication before proceeding to data extraction.
Sources: src/controllers/admin.rs:44-51, examples/axum_example/api/src/lib.rs:218-232
Admin Controller
The Admin Controller is primarily responsible for serving the dynamic configuration required by the SeaORM Pro frontend and providing the data points for dashboard visualizations.
Configuration Endpoint
The /admin/config endpoint uses a ConfigParser to load TOML-based configurations. In production environments, it serves a cached version via lazy_static, while in development, it reloads from disk on every request to facilitate rapid prototyping.
Sources: src/controllers/admin.rs:17-31, sea-orm-pro/src/parser.rs:16-24
Dashboard Data Aggregation
The dashboard handler processes requests for specific graph data (e.g., "new_customer_by_month", "sales_value_by_day"). It performs complex SeaORM queries involving joins, aggregations (SUM, COUNT), and date casting across different database backends (MySQL, Postgres, SQLite).
Sources: src/controllers/admin.rs:52-120
GraphQL Controller
The GraphQL controller manages the integration of seaography to provide a dynamic GraphQL API over the existing database schema.
- GraphQL Playground: Serves the interactive web interface for testing queries, including a mechanism to automatically inject the authentication token from
localStorage. - GraphQL Handler: Executes incoming GraphQL requests against the generated schema. It injects a
UserContextinto the request data for row-level security or auditing purposes.
Sources: src/controllers/graphql.rs:10-25, src/controllers/graphql.rs:43-51
Shared Response Utilities
To maintain consistency across endpoints, the project defines a standard Res structure for simple success or failure responses.
| Field | Type | Description |
|---|---|---|
success | bool | Indicates if the operation was successful. |
message | String | A descriptive message for the client. |
Sources: src/controllers/mod.rs:7-11
Summary
The REST Controllers in SeaORM Pro provide a robust bridge between the client-side admin panel and the server-side SeaORM entities. By separating concerns into dedicated modules for administration, GraphQL, and user management, the architecture remains scalable. Key features include environment-aware configuration loading, multi-dialect SQL support for analytics, and seamless GraphQL integration.
Sources: src/controllers/admin.rs, src/controllers/graphql.rs