Infra refactor

This commit is contained in:
2025-12-25 21:57:20 +00:00
parent 78d9314602
commit bb15181817
14 changed files with 381 additions and 84 deletions

View File

@@ -15,6 +15,7 @@ A modern, self-hosted note-taking application built with performance, security,
- **Responsive**: Mobile-friendly UI built with Tailwind CSS.
- **Architecture**:
- **Backend**: Hexagonal Architecture (Domain, Infra, API layers) in Rust.
- **Infrastructure**: Configurable database backends (SQLite, Postgres).
- **Frontend**: Modern React with TypeScript and Vite.
- **Deployment**: Full Docker support with `compose.yml`.
@@ -23,7 +24,7 @@ A modern, self-hosted note-taking application built with performance, security,
### Backend
- **Language**: Rust
- **Framework**: Axum
- **Database**: SQLite (SQLx)
- **Database**: SQLite (Default) or Postgres (Supported via feature flag)
- **Dependency Injection**: Manual wiring for clear boundaries
### Frontend
@@ -59,6 +60,16 @@ The frontend is automatically configured to talk to the backend.
cargo run -p notes-api
```
By default, this uses the **SQLite** backend.
**Running with Postgres:**
To use PostgreSQL, build with the `postgres` feature:
```bash
cargo run -p notes-api --no-default-features --features notes-infra/postgres
```
*Note: Ensure your `DATABASE_URL` is set to a valid Postgres connection string.*
#### Frontend
1. Navigate to `k-notes-frontend`.
@@ -74,7 +85,32 @@ bun install
bun dev
```
## 🏗️ Project Structure
## Database Architecture
The backend follows a Hexagonal Architecture (Ports and Adapters). The `notes-domain` crate defines the repository capabilities (Ports), and `notes-infra` implements them (Adapters).
### Supported Databases
- **SQLite**: Fully implemented (default). Ideal for single-instance, self-hosted deployments.
- **Postgres**: Structure is in place (via feature flag), ready for implementation.
### Extending Database Support
To add a new database (e.g., MySQL), follow these steps:
1. **Dependencies**: Add the driver to `notes-infra/Cargo.toml` (e.g., `sqlx` with `mysql` feature) and create a feature flag.
2. **Configuration**: Update `DatabaseConfig` in `notes-infra/src/db.rs` to handle the new connection URL scheme and connection logic in `create_pool`.
3. **Repository Implementation**:
- Implement `NoteRepository`, `TagRepository`, and `UserRepository` traits for the new database in `notes-infra`.
4. **Factory Integration**:
- Update `notes-infra/src/factory.rs` to include a builder for the new repositories.
- Update `build_database_pool` and repository `build_*` functions to support the new database type match arm.
5. **Migrations**:
- Add migration files in `migrations/<db_type>`.
- Update `run_migrations` in `db.rs` to execute them.
This design ensures the `notes-api` layer remains completely agnostic to the underlying database technology.
## Project Structure
```
├── notes-api # API Interface (Axum, HTTP routes)