Axum Integration Example
Relevant source files
The following files were used as context for generating this wiki page:
The Axum Integration Example demonstrates a full-stack implementation using the Axum web framework integrated with SeaORM and SeaORM Pro. This example serves as a blueprint for building applications that require a RESTful API, a GraphQL interface via Seaography, and a professional admin panel backend.
Sources: examples/axum_example/README.md, examples/axum_example/api/src/lib.rs:32-75
The project is structured as a workspace comprising several crates: api for the web server and routing, service for database logic, entity for SeaORM models, and migration for database schema management. It leverages Axum for HTTP routing, Tera for HTML templating, and async-graphql for its GraphQL capabilities.
Sources: examples/axum_example/Cargo.toml:10-11, examples/axum_example/api/Cargo.toml:10-25
System Architecture
The integration follows a multi-tiered architecture where the API layer handles requests and state management, while the service layer encapsulates the business and database logic.
The diagram illustrates the flow of requests from the client through Axum's routing and state management to the underlying SeaORM service layers.
Sources: examples/axum_example/api/src/lib.rs:32-84, examples/axum_example/api/Cargo.toml:10-15
API Endpoints and Routing
The Axum router is configured to handle various types of requests, including server-side rendered HTML, JSON APIs for the admin panel, and GraphQL queries.
REST and UI Routes
| Method | Path | Description |
|---|---|---|
| GET | / | Lists posts using Tera templates. |
| POST | / | Creates a new post. |
| GET | /{id} | Edits a specific post. |
| POST | /{id} | Updates a specific post. |
| GET | /new | Displays the form for a new post. |
| POST | /delete/{id} | Deletes a post. |
Sources: examples/axum_example/api/src/lib.rs:48-53
Admin and System Routes
| Method | Path | Description |
|---|---|---|
| GET | /api/admin/config | Returns SeaORM Pro dashboard configuration. |
| POST | /api/auth/login | Handles demo user authentication. |
| GET | /api/user/current | Retrieves authenticated user info. |
| GET/POST | /api/graphql | GraphQL playground and query handler. |
| GET | /admin/* | Serves the static admin panel frontend assets. |
Sources: examples/axum_example/api/src/lib.rs:54-73
GraphQL Integration
The project uses seaography to automatically generate a GraphQL schema from SeaORM entities. The api crate defines a schema builder that incorporates database connections and limit settings.
Sequence of a GraphQL request handling, including authentication check and database execution.
Sources: examples/axum_example/api/src/lib.rs:218-232, examples/axum_example/api/src/graphql/query_root.rs:13-28
Schema Configuration
The GraphQL schema is constructed with specific limits to prevent resource exhaustion:
- Depth Limit: Controlled via the
depthparameter in the schema function. - Complexity Limit: Controlled via the
complexityparameter. - Context: The database connection is injected into the GraphQL context for use by
seaography.
Sources: examples/axum_example/api/src/graphql/query_root.rs:17-28
Authentication and Security
For demonstration purposes, the example implements a basic token-based authentication system. It uses a static DEMO_USER_TOKEN and checks the AUTHORIZATION header for valid requests to sensitive endpoints like /api/user/current and the GraphQL handler.
fn check_user_auth(headers: &HeaderMap) -> Result<(), (StatusCode, &'static str)> {
let auth_header = headers.get("AUTHORIZATION");
let Some(auth_header) = auth_header else {
panic!("unauthorized!");
};
if !auth_header.to_str().unwrap().ends_with(DEMO_USER_TOKEN) {
panic!("unauthorized!");
}
Ok(())
}
Sources: examples/axum_example/api/src/lib.rs:191-203
Configuration and Setup
The application's behavior is governed by environment variables and crate features.
Environment Variables
| Variable | Description |
|---|---|
DATABASE_URL | Connection string (e.g., sqlite://db.sqlite). |
HOST | The server host address (e.g., 127.0.0.1). |
PORT | The server port (e.g., 8000). |
Sources: examples/axum_example/api/src/lib.rs:36-40
Database Drivers
Database backends are toggled via Cargo features in service/Cargo.toml. Supported options include:
sqlx-sqlite(Default)sqlx-postgressqlx-mysql
Sources: examples/axum_example/service/Cargo.toml:16-21
Conclusion
The Axum Integration Example provides a robust starting point for developers using SeaORM Pro. By combining Axum's efficient routing with SeaORM's database abstractions and Seaography's automatic GraphQL generation, it demonstrates a complete workflow for modern Rust web development.
Sources: examples/axum_example/README.md, examples/axum_example/api/src/lib.rs:234-242