2024-10-08 22:34:49 +00:00
2024-10-15 22:20:01 +00:00
# Box and Item Management API
2024-10-08 22:34:49 +00:00
2024-10-15 22:20:01 +00:00
This project is a backend API built using Go, designed for managing boxes and the items stored in those boxes. The application is containerized using Docker and uses SQLite3 for data storage. It also supports JWT-based authentication and logs various activities such as logins and box/item operations.
2024-10-08 22:34:49 +00:00
## Features
2024-10-15 22:20:01 +00:00
- Manage boxes and items stored within them.
- JWT-based authentication.
- Automatic database creation if it doesn't exist.
- Configurable using a `config.yaml` file.
- Logs logins, box/item creation, and deletion.
- Image upload and storage support for items.
- Containerized using Docker.
## Tech Stack
- **Language:** Go
- **Database:** SQLite3
- **Authentication:** JWT
- **Containerization:** Docker
## API Endpoints
### Authentication
- **POST** `/login`
Authenticates a user and returns a JWT.
### Boxes
- **GET** `/boxes`
Retrieves all boxes.
- **POST** `/boxes`
Creates a new box.
- **DELETE** `/boxes/{id}`
Deletes a box by its ID.
- **GET** `/boxes/{id}/items`
Retrieves all items in a box by its ID.
### Items
- **GET** `/items`
Retrieves all items, optionally searchable by description.
- **POST** `/items`
Adds a new item to a box.
- **GET** `/items/{id}`
Retrieves an item by its ID.
- **PUT** `/items/{id}`
Updates an existing item.
- **DELETE** `/items/{id}`
Deletes an item by its ID.
- **GET** `/items/{id}/image`
Retrieves the image of an item.
- **POST** `/items/{id}/upload`
Uploads an image for an item.
## Configuration
The application uses a `config.yaml` file to manage configuration settings, such as:
```yaml
database_path: "./data/database.db"
jwt_secret: "your_jwt_secret"
log_file: "./logs/app.log"
image_storage_directory: "./images"
```
2024-10-15 22:21:52 +00:00
The server also expects the CONFIG environment variable to be set. e.g. `export CONFIG=config/config.yaml` .
2024-10-15 22:20:01 +00:00
## Setup and Running
### Prerequisites
- Docker
- Go (if running locally)
- SQLite3
### Running with Docker
1. Build the Docker image:
2024-10-08 22:34:49 +00:00
```bash
2024-10-15 22:20:01 +00:00
docker build -t box-management-api .
2024-10-08 22:34:49 +00:00
```
2024-10-15 22:20:01 +00:00
2. Run the Docker container:
2024-10-08 22:34:49 +00:00
```bash
2024-10-15 22:20:01 +00:00
docker run -p 8080:8080 box-management-api
2024-10-08 22:34:49 +00:00
```
2024-10-15 22:20:01 +00:00
### Running Locally
2024-10-08 22:34:49 +00:00
2024-10-15 22:20:01 +00:00
1. Clone the repository:
```bash
git clone https://github.com/your-repo/box-management-api.git
cd box-management-api
```
2024-10-08 22:34:49 +00:00
2024-10-15 22:20:01 +00:00
2. Build the Go application:
```bash
go build -o main .
```
2024-10-08 22:34:49 +00:00
2024-10-15 22:20:01 +00:00
3. Run the application:
```bash
./main
2024-10-08 22:34:49 +00:00
```
2024-10-15 22:20:01 +00:00
## Database Schema
2024-10-08 22:34:49 +00:00
2024-10-15 22:20:01 +00:00
The application creates the following SQLite3 tables:
2024-10-08 22:34:49 +00:00
2024-10-15 22:20:01 +00:00
- `boxes` : Contains `id` (int) and `name` (text).
- `items` : Contains `id` (int), `name` (text), `description` (text), `box_id` (int), and `image_path` (text).
- `users` : Contains `id` (int), `username` (text), and `password` (hashed).
2024-10-08 22:34:49 +00:00
2024-10-15 22:20:01 +00:00
## Authentication
2024-10-08 22:34:49 +00:00
2024-10-15 22:20:01 +00:00
The API uses JWT-based authentication. A default user is created on startup:
2024-10-08 22:34:49 +00:00
2024-10-15 22:20:01 +00:00
- **Username**: `boxuser`
- **Password**: `boxuser`
2024-10-08 22:34:49 +00:00
2024-10-15 22:20:01 +00:00
## Logs
The following events are logged to the file specified in the configuration:
- User logins
- Box creation/deletion
- Item creation/deletion
2024-10-08 22:34:49 +00:00
2024-10-15 22:20:01 +00:00
## License
This project is licensed under the MIT License.