157 lines
No EOL
4.1 KiB
Markdown
157 lines
No EOL
4.1 KiB
Markdown
# 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
|
|
|
|
```bash
|
|
cargo build
|
|
```
|
|
|
|
## Running
|
|
|
|
### HTTP API with in-memory storage
|
|
|
|
```bash
|
|
cargo run --bin sharenet-api-memory
|
|
```
|
|
|
|
### HTTP API with PostgreSQL
|
|
|
|
```bash
|
|
cargo run --bin sharenet-api-postgres
|
|
```
|
|
|
|
### CLI with in-memory storage
|
|
|
|
```bash
|
|
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
|
|
|
|
```bash
|
|
cargo run --bin sharenet-cli-postgres user create --username john --email john@example.com
|
|
```
|
|
|
|
### TUI with in-memory storage
|
|
|
|
```bash
|
|
cargo run --bin sharenet-tui-memory
|
|
```
|
|
|
|
### TUI with PostgreSQL
|
|
|
|
```bash
|
|
cargo run --bin sharenet-tui-postgres
|
|
```
|
|
|
|
## Database Setup
|
|
|
|
### Using SQLx CLI
|
|
|
|
1. Install SQLx CLI:
|
|
```bash
|
|
cargo install sqlx-cli
|
|
```
|
|
|
|
2. Create the database:
|
|
```bash
|
|
sqlx database create
|
|
```
|
|
|
|
3. Run migrations:
|
|
```bash
|
|
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
|
|
|
|
MIT |