Skip to content

Commit 110d8af

Browse files
committed
A backend and server_side_storage courtesy Copilot (Claude Sonnet 4)
1 parent 8aa7852 commit 110d8af

File tree

5 files changed

+530
-215
lines changed

5 files changed

+530
-215
lines changed

backend/API_README.md

Lines changed: 91 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
1-
# Project and Module Management API
1+
# Storage API
22

3-
A Flask-based REST API for managing projects and modules with JSON content, using SQLite as the database.
3+
A Flask-based REST API for providing storage capabilities (key-value pairs and file storage) using SQLite as the database.
44

55
## Features
66

7-
- **Projects**: Create, read, update, and delete projects
8-
- **Modules**: Create, read, update, and delete modules within projects
9-
- **JSON Content**: Each module stores JSON content as a string
7+
- **Key-Value Storage**: Store and retrieve key-value pairs
8+
- **File Storage**: Store, retrieve, list, rename, and delete files and directories
109
- **SQLite Database**: Persistent storage using SQLite
10+
- **CORS Support**: Cross-origin resource sharing for frontend integration
1111

1212
## API Endpoints
1313

14-
### Projects
14+
### Key-Value Storage (Entries)
1515

16-
- `GET /projects` - Get list of all projects
17-
- `POST /projects` - Create a new project
18-
- `GET /projects/<project_id>` - Get a specific project with module list
19-
- `PUT /projects/<project_id>` - Update a project
20-
- `DELETE /projects/<project_id>` - Delete a project and all its modules
16+
- `GET /entries/<entry_key>` - Fetch entry value by key (supports `?default=value` query param)
17+
- `POST /entries/<entry_key>` - Save entry value
2118

22-
### Modules
19+
### File and Directory Operations
2320

24-
- `GET /projects/<project_id>/modules` - Get all modules for a project
25-
- `POST /projects/<project_id>/modules` - Create a new module in a project
26-
- `GET /projects/<project_id>/modules/<module_id>` - Get a specific module
27-
- `PUT /projects/<project_id>/modules/<module_id>` - Update a module
28-
- `DELETE /projects/<project_id>/modules/<module_id>` - Delete a module
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
2925

3026
## Running the Application
3127

@@ -43,51 +39,107 @@ A Flask-based REST API for managing projects and modules with JSON content, usin
4339

4440
## Example Usage
4541

46-
### Create a Project
42+
### Key-Value Operations (Entries)
43+
44+
#### Save an Entry
4745
```bash
48-
curl -X POST http://localhost:5001/projects \
46+
curl -X POST http://localhost:5001/entries/user_settings \
4947
-H "Content-Type: application/json" \
50-
-d '{"name": "Robot Controller", "description": "Main robot control project"}'
48+
-d '{"value": "{\"theme\": \"dark\", \"language\": \"en\"}"}'
49+
```
50+
51+
#### Fetch an Entry
52+
```bash
53+
curl http://localhost:5001/entries/user_settings
54+
```
55+
56+
#### Fetch an Entry with Default Value
57+
```bash
58+
curl "http://localhost:5001/entries/missing_key?default=default_value"
5159
```
5260

53-
### Create a Module
61+
### File and Directory Operations
62+
63+
#### Save a File
5464
```bash
55-
curl -X POST http://localhost:5001/projects/1/modules \
65+
curl -X POST http://localhost:5001/storage/projects/robot1/main.py \
5666
-H "Content-Type: application/json" \
57-
-d '{"name": "motor_controller", "json_content": {"type": "motor", "settings": {"speed": 100}}}'
67+
-d '{"content": "# Robot main file\nprint(\"Hello Robot!\")"}'
5868
```
5969

60-
### Get All Projects
70+
#### Fetch a File
6171
```bash
62-
curl http://localhost:5001/projects
72+
curl http://localhost:5001/storage/projects/robot1/main.py
6373
```
6474

65-
### Get Project Modules
75+
#### List Files in Directory
6676
```bash
67-
curl http://localhost:5001/projects/1/modules
77+
curl http://localhost:5001/storage/projects/
6878
```
6979

70-
### Update Module Content
80+
#### Rename a File
7181
```bash
72-
curl -X PUT http://localhost:5001/projects/1/modules/1 \
82+
curl -X POST http://localhost:5001/storage/rename \
7383
-H "Content-Type: application/json" \
74-
-d '{"json_content": {"type": "motor", "settings": {"speed": 150}}}'
84+
-d '{"old_path": "projects/robot1/main.py", "new_path": "projects/robot1/robot_main.py"}'
85+
```
86+
87+
#### Rename a Directory
88+
```bash
89+
curl -X POST http://localhost:5001/storage/rename \
90+
-H "Content-Type: application/json" \
91+
-d '{"old_path": "projects/robot1/", "new_path": "projects/my_robot/"}'
92+
```
93+
94+
#### Delete a File
95+
```bash
96+
curl -X DELETE http://localhost:5001/storage/projects/robot1/old_file.py
97+
```
98+
99+
#### Delete a Directory
100+
```bash
101+
curl -X DELETE http://localhost:5001/storage/projects/old_project/
75102
```
76103

77104
## Data Models
78105

79-
### Project
106+
### StorageEntry (Key-Value Storage)
80107
- `id`: Integer (Primary Key)
81-
- `name`: String (Unique, Required)
82-
- `description`: Text (Optional)
83-
- `modules`: Relationship to Module objects
108+
- `entry_key`: String (Unique, Required)
109+
- `entry_value`: Text (Required)
84110

85-
### Module
111+
### StorageFile (File Storage)
86112
- `id`: Integer (Primary Key)
87-
- `name`: String (Required, Unique per project)
88-
- `project_id`: Integer (Foreign Key)
89-
- `json_content`: Text (Required, stored as JSON string)
113+
- `file_path`: String (Unique, Required)
114+
- `file_content`: Text (Required)
90115

91116
## Database
92117

93-
The application creates a SQLite database file `projects.db` in the backend directory automatically when first run.
118+
The application creates a SQLite database file `storage.db` in the backend directory automatically when first run.
119+
120+
## CORS Support
121+
122+
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.
123+
124+
## Frontend Integration
125+
126+
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:
127+
128+
- Key-value storage (`saveEntry`, `fetchEntry`)
129+
- File operations (`saveFile`, `fetchFileContentText`, `list`, `rename`, `delete`)
130+
131+
Example usage in TypeScript:
132+
```typescript
133+
import { ServerSideStorage } from './storage/server_side_storage';
134+
135+
const storage = new ServerSideStorage();
136+
137+
// Save and fetch key-value data
138+
await storage.saveEntry('user_settings', JSON.stringify({theme: 'dark'}));
139+
const settings = await storage.fetchEntry('user_settings', '{}');
140+
141+
// File operations
142+
await storage.saveFile('projects/robot1/main.py', 'print("Hello Robot!")');
143+
const content = await storage.fetchFileContentText('projects/robot1/main.py');
144+
const files = await storage.list('projects/');
145+
```

0 commit comments

Comments
 (0)