sharenet/backend
2025-06-23 21:19:09 -04:00
..
config Initial commit: Project setup with frontend and backend 2025-06-16 00:40:45 -04:00
crates Add tui unit tests and make sure all unit tests pass 2025-06-23 21:19:09 -04:00
migrations Add postgres crate unit tests 2025-06-22 19:25:16 -04:00
Cargo.toml Add unit tests for domain and application layers, and update licenses to correct license 2025-06-22 16:19:38 -04:00
README.md Update readmes to reference correct license 2025-06-16 01:13:51 -04:00

Sharenet

A Rust backend for performing CRUD operations on various struct types (e.g., products, users) that uses a clean architecture with configurable storage and interface layers.

Features

  • Clean architecture with separate domain, application, and infrastructure layers
  • Multiple interface options:
    • HTTP REST API
    • Command Line Interface (CLI)
    • Text User Interface (TUI)
  • Configurable storage backends:
    • In-memory storage
    • PostgreSQL database
  • Generic implementations for easy extension
  • Static dispatch with generic traits
  • Axum-based HTTP API
  • Clap-based CLI
  • Ratatui-based TUI

Project Structure

.
├── crates/
│   ├── api/           # HTTP API implementation (Axum)
│   ├── application/   # Application services and use cases
│   ├── cli/           # CLI implementation (Clap)
│   ├── domain/        # Domain entities and traits
│   ├── memory/        # In-memory storage implementation
│   ├── postgres/      # PostgreSQL storage implementation
│   ├── tui/           # TUI implementation (Ratatui)
│   ├── sharenet-api-memory/    # API binary with in-memory storage
│   ├── sharenet-api-postgres/  # API binary with PostgreSQL storage
│   ├── sharenet-cli-memory/    # CLI binary with in-memory storage
│   ├── sharenet-cli-postgres/  # CLI binary with PostgreSQL storage
│   ├── sharenet-tui-memory/    # TUI binary with in-memory storage
│   └── sharenet-tui-postgres/  # TUI binary with PostgreSQL storage
├── migrations/        # Database migrations
└── config/           # Configuration files for different environments

Building

cargo build

Running

HTTP API with in-memory storage

cargo run --bin sharenet-api-memory

HTTP API with PostgreSQL

cargo run --bin sharenet-api-postgres

CLI with in-memory storage

cargo run --bin sharenet-cli-memory user create --username john --email john@example.com
cargo run --bin sharenet-cli-memory user list
cargo run --bin sharenet-cli-memory product create --name "Product 1" --description "Description"
cargo run --bin sharenet-cli-memory product list

CLI with PostgreSQL

cargo run --bin sharenet-cli-postgres user create --username john --email john@example.com

TUI with in-memory storage

cargo run --bin sharenet-tui-memory

TUI with PostgreSQL

cargo run --bin sharenet-tui-postgres

Database Setup

Using SQLx CLI

  1. Install SQLx CLI:
cargo install sqlx-cli
  1. Create the database:
sqlx database create
  1. Run migrations:
sqlx migrate run

API Endpoints

Users

  • POST /users - Create a new user
  • GET /users/:id - Get a user by ID
  • GET /users - List all users
  • PUT /users/:id - Update a user
  • DELETE /users/:id - Delete a user

Products

  • POST /products - Create a new product
  • GET /products/:id - Get a product by ID
  • GET /products - List all products
  • PUT /products/:id - Update a product
  • DELETE /products/:id - Delete a product

CLI Commands

Users

  • user create --username <username> --email <email> - Create a new user
  • user get --id <id> - Get a user by ID
  • user list - List all users
  • user update --id <id> [--username <username>] [--email <email>] - Update a user
  • user delete --id <id> - Delete a user

Products

  • product create --name <name> --description <description> - Create a new product
  • product get --id <id> - Get a product by ID
  • product list - List all products
  • product update --id <id> [--name <name>] [--description <description>] - Update a product
  • product delete --id <id> - Delete a product

Configuration

Configuration files are located in the config/ directory:

  • api-memory.env - API server with in-memory storage
  • api-postgres.env - API server with PostgreSQL storage
  • cli-memory.env - CLI with in-memory storage
  • cli-postgres.env - CLI with PostgreSQL storage
  • tui-memory.env - TUI with in-memory storage
  • tui-postgres.env - TUI with PostgreSQL storage

License

This project is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License - see the LICENSE.md file for details.