.. | ||
config | ||
crates | ||
migrations | ||
Cargo.toml | ||
README.md |
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
- Install SQLx CLI:
cargo install sqlx-cli
- Create the database:
sqlx database create
- Run migrations:
sqlx migrate run
API Endpoints
Users
POST /users
- Create a new userGET /users/:id
- Get a user by IDGET /users
- List all usersPUT /users/:id
- Update a userDELETE /users/:id
- Delete a user
Products
POST /products
- Create a new productGET /products/:id
- Get a product by IDGET /products
- List all productsPUT /products/:id
- Update a productDELETE /products/:id
- Delete a product
CLI Commands
Users
user create --username <username> --email <email>
- Create a new useruser get --id <id>
- Get a user by IDuser list
- List all usersuser update --id <id> [--username <username>] [--email <email>]
- Update a useruser delete --id <id>
- Delete a user
Products
product create --name <name> --description <description>
- Create a new productproduct get --id <id>
- Get a product by IDproduct list
- List all productsproduct update --id <id> [--name <name>] [--description <description>]
- Update a productproduct delete --id <id>
- Delete a product
Configuration
Configuration files are located in the config/
directory:
api-memory.env
- API server with in-memory storageapi-postgres.env
- API server with PostgreSQL storagecli-memory.env
- CLI with in-memory storagecli-postgres.env
- CLI with PostgreSQL storagetui-memory.env
- TUI with in-memory storagetui-postgres.env
- TUI with PostgreSQL storage
License
MIT