Skip to content

Commit 3057bc0

Browse files
authored
Backend (#218)
* First pass of AI generated backend * A backend and server_side_storage courtesy Copilot (Claude Sonnet 4) * Addressing review comments * Convert test to python unit test * Adding test * Fix so it correctly handles getting the root
1 parent c6cbe4f commit 3057bc0

File tree

6 files changed

+640
-0
lines changed

6 files changed

+640
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ venv/
2424
.env.test.local
2525
.env.production.local
2626
.vscode
27+
projects.db
2728

2829
npm-debug.log*
2930
yarn-debug.log*

backend/README.md

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# Storage API
2+
3+
A Flask-based REST API for providing storage capabilities (key-value pairs and file storage) using SQLite as the database.
4+
5+
## Features
6+
7+
- **Key-Value Storage**: Store and retrieve key-value pairs
8+
- **File Storage**: Store, retrieve, list, rename, and delete files and directories
9+
- **SQLite Database**: Persistent storage using SQLite
10+
- **CORS Support**: Cross-origin resource sharing for frontend integration
11+
12+
## API Endpoints
13+
14+
### Key-Value Storage (Entries)
15+
16+
- `GET /entries/<entry_key>` - Fetch entry value by key (supports `?default=value` query param)
17+
- `POST /entries/<entry_key>` - Save entry value
18+
19+
### File and Directory Operations
20+
21+
- `GET /storage/<path>` - List directory contents (if path ends with `/`) or fetch file content (if path doesn't end with `/`)
22+
- `POST /storage/<path>` - Save file content (path must not end with `/`)
23+
- `DELETE /storage/<path>` - Delete file or directory
24+
- `POST /storage/rename` - Rename file or directory
25+
26+
## Setup
27+
1. cd <your repo>/backend
28+
2. python3.12 -m venv ./venv
29+
3. source ./venv/bin/activate
30+
4. python3.12 -m pip install -r requirements.txt
31+
5. deactivate
32+
33+
## Running the Application
34+
1. source ./venv/bin/activate
35+
2. python main.py
36+
3. The API will be available at `http://localhost:5001`
37+
38+
## Example Usage
39+
40+
### Key-Value Operations (Entries)
41+
42+
#### Save an Entry
43+
```bash
44+
curl -X POST http://localhost:5001/entries/user_settings \
45+
-H "Content-Type: application/json" \
46+
-d '{"value": "{\"theme\": \"dark\", \"language\": \"en\"}"}'
47+
```
48+
49+
#### Fetch an Entry
50+
```bash
51+
curl http://localhost:5001/entries/user_settings
52+
```
53+
54+
#### Fetch an Entry with Default Value
55+
```bash
56+
curl "http://localhost:5001/entries/missing_key?default=default_value"
57+
```
58+
59+
### File and Directory Operations
60+
61+
#### Save a File
62+
```bash
63+
curl -X POST http://localhost:5001/storage/projects/robot1/main.py \
64+
-H "Content-Type: application/json" \
65+
-d '{"content": "# Robot main file\nprint(\"Hello Robot!\")"}'
66+
```
67+
68+
#### Fetch a File
69+
```bash
70+
curl http://localhost:5001/storage/projects/robot1/main.py
71+
```
72+
73+
#### List Files in Directory
74+
```bash
75+
curl http://localhost:5001/storage/projects/
76+
```
77+
78+
#### Rename a File
79+
```bash
80+
curl -X POST http://localhost:5001/storage/rename \
81+
-H "Content-Type: application/json" \
82+
-d '{"old_path": "projects/robot1/main.py", "new_path": "projects/robot1/robot_main.py"}'
83+
```
84+
85+
#### Rename a Directory
86+
```bash
87+
curl -X POST http://localhost:5001/storage/rename \
88+
-H "Content-Type: application/json" \
89+
-d '{"old_path": "projects/robot1/", "new_path": "projects/my_robot/"}'
90+
```
91+
92+
#### Delete a File
93+
```bash
94+
curl -X DELETE http://localhost:5001/storage/projects/robot1/old_file.py
95+
```
96+
97+
#### Delete a Directory
98+
```bash
99+
curl -X DELETE http://localhost:5001/storage/projects/old_project/
100+
```
101+
102+
## Data Models
103+
104+
### StorageEntry (Key-Value Storage)
105+
- `id`: Integer (Primary Key)
106+
- `entry_key`: String (Unique, Required)
107+
- `entry_value`: Text (Required)
108+
109+
### StorageFile (File Storage)
110+
- `id`: Integer (Primary Key)
111+
- `file_path`: String (Unique, Required)
112+
- `file_content`: Text (Required)
113+
114+
## Database
115+
116+
The application creates a SQLite database file `storage.db` in the backend directory automatically when first run.
117+
118+
## CORS Support
119+
120+
The API includes CORS (Cross-Origin Resource Sharing) headers to allow frontend applications running on different ports to access the API. This is essential for development when the frontend runs on a different port than the backend.
121+
122+
## Frontend Integration
123+
124+
The server-side storage implementation in `src/storage/server_side_storage.ts` provides a TypeScript interface that connects to this Flask backend. It implements the `Storage` interface with methods for:
125+
126+
- Key-value storage (`saveEntry`, `fetchEntry`)
127+
- File operations (`saveFile`, `fetchFileContentText`, `list`, `rename`, `delete`)
128+
129+
Example usage in TypeScript:
130+
```typescript
131+
import { ServerSideStorage } from './storage/server_side_storage';
132+
133+
const storage = new ServerSideStorage();
134+
135+
// Save and fetch key-value data
136+
await storage.saveEntry('user_settings', JSON.stringify({theme: 'dark'}));
137+
const settings = await storage.fetchEntry('user_settings', '{}');
138+
139+
// File operations
140+
await storage.saveFile('projects/robot1/main.py', 'print("Hello Robot!")');
141+
const content = await storage.fetchFileContentText('projects/robot1/main.py');
142+
const files = await storage.list('projects/');
143+
```

0 commit comments

Comments
 (0)