Skip to content

Commit d05ba6b

Browse files
author
hackpulsar
committed
Merge branch 'dev', remote-tracking branch 'origin'
2 parents 96087eb + 5408d07 commit d05ba6b

5 files changed

Lines changed: 232 additions & 0 deletions

File tree

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Storage Crab API
2+
3+
A Rust-based backend API for file storage with sharing capabilities.
4+
5+
## Overview
6+
This backend provides a secure, HTTPS-enabled API for managing user accounts and storing files. Built with Rust using the Actix-web framework, it supports user registration, login with JWT-based authentication, file uploads/downloads, and temporary file sharing via share codes.
7+
8+
## Features
9+
10+
- **User Authentication** - Registration and login with JWT tokens
11+
- **Token Refresh** - Secure token refresh with blacklisting via Redis
12+
- **File Storage** - Upload, download, list, and delete files
13+
- **File Sharing** - Generate temporary share codes (5-minute expiry) via Redis
14+
- **HTTPS** - TLS/SSL encrypted communication
15+
16+
## Architecture
17+
18+
```
19+
src/
20+
├── main.rs # Server setup, TLS config, app state
21+
├── routes/ # HTTP endpoints
22+
│ ├── auth.rs # Login, register, token refresh
23+
│ ├── files.rs # File operations
24+
│ └── user.rs # User profile endpoints
25+
├── services/ # Business logic
26+
├── models/ # Data structures
27+
└── utils/ # Error handling, helpers
28+
```
29+
30+
## Documentation
31+
32+
See the [docs](docs/) directory for detailed documentation:
33+
- [API Reference](docs/api.md)
34+
- [Configuration](docs/config.md)
35+
- [Deployment](docs/deployment.md)

docs/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Documentation
2+
3+
- [API Reference](api.md) - Endpoints, request/response formats
4+
- [Configuration](config.md) - Environment variables
5+
- [Deployment](deployment.md) - Docker, local setup

docs/api.md

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# API Reference
2+
3+
## Authentication
4+
5+
| Method | Endpoint | Description | Auth Required |
6+
|--------|----------|-------------|---------------|
7+
| `POST` | `/api/users/` | Register new user | No |
8+
| `POST` | `/api/token/get/` | Login | No |
9+
| `POST` | `/api/token/refresh/` | Refresh tokens | No |
10+
| `POST` | `/api/users/greet/` | Test authentication | Yes |
11+
12+
## User
13+
14+
| Method | Endpoint | Description | Auth Required |
15+
|--------|----------|-------------|---------------|
16+
| `GET` | `/api/users/me/` | Get current user info | Yes |
17+
18+
## Files
19+
20+
| Method | Endpoint | Description | Auth Required |
21+
|--------|----------|-------------|---------------|
22+
| `GET` | `/api/files/` | List user's files | Yes |
23+
| `POST` | `/api/files/upload/` | Upload a file | Yes |
24+
| `GET` | `/api/files/download/{file_id}/` | Download a file | Yes |
25+
| `POST` | `/api/files/delete/{file_id}/` | Delete a file | Yes |
26+
| `POST` | `/api/files/share/{file_id}/` | Generate share code | Yes |
27+
| `GET` | `/api/files/download/shared/{share_code}/` | Download shared file | Yes |
28+
29+
## Request/Response Formats
30+
31+
### Register User
32+
```json
33+
// POST /api/users/
34+
// Request
35+
{
36+
"email": "user@example.com",
37+
"username": "johndoe",
38+
"password_hash": "hashed_password"
39+
}
40+
41+
// Response
42+
{
43+
"id": 1,
44+
"email": "user@example.com",
45+
"username": "johndoe"
46+
}
47+
```
48+
49+
### Login
50+
```json
51+
// POST /api/token/get/
52+
// Request
53+
{
54+
"email": "user@example.com",
55+
"password_hash": "hashed_password"
56+
}
57+
58+
// Response
59+
{
60+
"access_token": "eyJ...",
61+
"refresh_token": "eyJ..."
62+
}
63+
```
64+
65+
### Upload File
66+
```
67+
// POST /api/files/upload/
68+
// Content-Type: multipart/form-data
69+
// Form fields:
70+
// - file: <binary>
71+
// - json: {"filename": "document.pdf"}
72+
```
73+
74+
### Refresh Token
75+
```json
76+
// POST /api/token/refresh/
77+
// Request
78+
{
79+
"refresh_token": "eyJ..."
80+
}
81+
82+
// Response
83+
{
84+
"access_token": "eyJ...",
85+
"refresh_token": "eyJ..."
86+
}
87+
```
88+
89+
### Greet
90+
```json
91+
// POST /api/users/greet/
92+
// Response (plain text)
93+
Welcome back, johndoe
94+
```
95+
96+
### Get Current User
97+
```json
98+
// GET /api/users/me/
99+
// Response
100+
{
101+
"id": 1,
102+
"email": "user@example.com",
103+
"username": "johndoe"
104+
}
105+
```
106+
107+
### List Files
108+
```json
109+
// GET /api/files/
110+
// Response
111+
[
112+
{
113+
"id": 1,
114+
"filename": "document.pdf",
115+
"path": "/app/files_storage/abc123/document.pdf",
116+
"size": 1024000,
117+
"uploaded_at": "2024-01-15T10:30:00",
118+
"user_id": 1
119+
}
120+
]
121+
```
122+
123+
### Download File
124+
```
125+
// GET /api/files/download/{file_id}/
126+
// Response: binary file stream
127+
// Headers:
128+
// - Content-Disposition: attachment; filename="document.pdf"
129+
// - Content-Length: 1024000
130+
```
131+
132+
### Delete File
133+
```
134+
// POST /api/files/delete/{file_id}/
135+
// Response: 204 No Content
136+
```
137+
138+
### Download Shared File
139+
```
140+
// GET /api/files/download/shared/{share_code}/
141+
// Response: binary file stream (no auth required)
142+
// Headers:
143+
// - Content-Disposition: attachment; filename="document.pdf"
144+
// - Content-Length: 1024000
145+
```
146+
147+
## JWT Tokens
148+
149+
- **Access token**: 10 minute expiry
150+
- **Refresh token**: 30 minute expiry
151+
- **Header format**: `Authorization: Bearer <token>`
152+
- **Share codes**: 5 minute expiry, stored in Redis

docs/config.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Configuration
2+
3+
## Environment Variables
4+
5+
| Variable | Description | Example |
6+
|----------|-------------|---------|
7+
| `DATABASE_URL` | PostgreSQL connection string | `postgresql://admin:admin@db:5432/crab_storage` |
8+
| `REDIS_URL` | Redis connection string | `redis://redis:6379` |
9+
| `DATABASE_USER` | PostgreSQL username | `admin` |
10+
| `DATABASE_PASSWORD` | PostgreSQL password | `admin` |
11+
| `DATABASE_NAME` | PostgreSQL database name | `crab_storage` |
12+
| `FILES_STORAGE_PATH` | Path for stored files | `/app/files_storage` |
13+
| `TMP_FILES_STORAGE` | Path for temporary files | `/app/files_storage/tmp` |
14+
| `RUST_LOG` | Logging level | `debug` |
15+
16+
## Prerequisites
17+
18+
- Rust (latest stable)
19+
- Docker & Docker Compose
20+
- PostgreSQL 16
21+
- Redis 7
22+
23+
## TLS Certificates
24+
25+
Place `cert.pem` and `key.pem` in the project root for HTTPS support.

docs/deployment.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Deployment
2+
3+
## Local Development
4+
5+
```bash
6+
docker compose up --build -d
7+
```
8+
9+
The server runs on port 8080 with HTTPS enabled.
10+
11+
## Testing
12+
13+
```bash
14+
cargo test
15+
```

0 commit comments

Comments
 (0)