-
Notifications
You must be signed in to change notification settings - Fork 9
Backend #218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Backend #218
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4f68a23
First pass of AI generated backend
alan412 8aa7852
Merge branch 'main' into backend
alan412 110d8af
A backend and server_side_storage courtesy Copilot (Claude Sonnet 4)
alan412 cb17501
Addressing review comments
alan412 c119e8e
Convert test to python unit test
alan412 17e8750
Adding test
alan412 face87a
Fix so it correctly handles getting the root
alan412 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ venv/ | |
| .env.test.local | ||
| .env.production.local | ||
| .vscode | ||
| projects.db | ||
|
|
||
| npm-debug.log* | ||
| yarn-debug.log* | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| # Storage API | ||
|
|
||
| A Flask-based REST API for providing storage capabilities (key-value pairs and file storage) using SQLite as the database. | ||
|
|
||
| ## Features | ||
|
|
||
| - **Key-Value Storage**: Store and retrieve key-value pairs | ||
| - **File Storage**: Store, retrieve, list, rename, and delete files and directories | ||
| - **SQLite Database**: Persistent storage using SQLite | ||
| - **CORS Support**: Cross-origin resource sharing for frontend integration | ||
|
|
||
| ## API Endpoints | ||
|
|
||
| ### Key-Value Storage (Entries) | ||
|
|
||
| - `GET /entries/<entry_key>` - Fetch entry value by key (supports `?default=value` query param) | ||
| - `POST /entries/<entry_key>` - Save entry value | ||
|
|
||
| ### File and Directory Operations | ||
|
|
||
| - `GET /storage/<path>` - List directory contents (if path ends with `/`) or fetch file content (if path doesn't end with `/`) | ||
| - `POST /storage/<path>` - Save file content (path must not end with `/`) | ||
| - `DELETE /storage/<path>` - Delete file or directory | ||
| - `POST /storage/rename` - Rename file or directory | ||
|
|
||
| ## Setup | ||
| 1. cd <your repo>/backend | ||
| 2. python3.12 -m venv ./venv | ||
| 3. source ./venv/bin/activate | ||
| 4. python3.12 -m pip install -r requirements.txt | ||
| 5. deactivate | ||
|
|
||
| ## Running the Application | ||
| 1. source ./venv/bin/activate | ||
| 2. python main.py | ||
| 3. The API will be available at `http://localhost:5001` | ||
|
|
||
| ## Example Usage | ||
|
|
||
| ### Key-Value Operations (Entries) | ||
|
|
||
| #### Save an Entry | ||
| ```bash | ||
| curl -X POST http://localhost:5001/entries/user_settings \ | ||
| -H "Content-Type: application/json" \ | ||
| -d '{"value": "{\"theme\": \"dark\", \"language\": \"en\"}"}' | ||
| ``` | ||
|
|
||
| #### Fetch an Entry | ||
| ```bash | ||
| curl http://localhost:5001/entries/user_settings | ||
| ``` | ||
|
|
||
| #### Fetch an Entry with Default Value | ||
| ```bash | ||
| curl "http://localhost:5001/entries/missing_key?default=default_value" | ||
| ``` | ||
|
|
||
| ### File and Directory Operations | ||
|
|
||
| #### Save a File | ||
| ```bash | ||
| curl -X POST http://localhost:5001/storage/projects/robot1/main.py \ | ||
| -H "Content-Type: application/json" \ | ||
| -d '{"content": "# Robot main file\nprint(\"Hello Robot!\")"}' | ||
| ``` | ||
|
|
||
| #### Fetch a File | ||
| ```bash | ||
| curl http://localhost:5001/storage/projects/robot1/main.py | ||
| ``` | ||
|
|
||
| #### List Files in Directory | ||
| ```bash | ||
| curl http://localhost:5001/storage/projects/ | ||
| ``` | ||
|
|
||
| #### Rename a File | ||
| ```bash | ||
| curl -X POST http://localhost:5001/storage/rename \ | ||
| -H "Content-Type: application/json" \ | ||
| -d '{"old_path": "projects/robot1/main.py", "new_path": "projects/robot1/robot_main.py"}' | ||
| ``` | ||
|
|
||
| #### Rename a Directory | ||
| ```bash | ||
| curl -X POST http://localhost:5001/storage/rename \ | ||
| -H "Content-Type: application/json" \ | ||
| -d '{"old_path": "projects/robot1/", "new_path": "projects/my_robot/"}' | ||
| ``` | ||
|
|
||
| #### Delete a File | ||
| ```bash | ||
| curl -X DELETE http://localhost:5001/storage/projects/robot1/old_file.py | ||
| ``` | ||
|
|
||
| #### Delete a Directory | ||
| ```bash | ||
| curl -X DELETE http://localhost:5001/storage/projects/old_project/ | ||
| ``` | ||
|
|
||
| ## Data Models | ||
|
|
||
| ### StorageEntry (Key-Value Storage) | ||
| - `id`: Integer (Primary Key) | ||
| - `entry_key`: String (Unique, Required) | ||
| - `entry_value`: Text (Required) | ||
|
|
||
| ### StorageFile (File Storage) | ||
| - `id`: Integer (Primary Key) | ||
| - `file_path`: String (Unique, Required) | ||
| - `file_content`: Text (Required) | ||
|
|
||
| ## Database | ||
|
|
||
| The application creates a SQLite database file `storage.db` in the backend directory automatically when first run. | ||
|
|
||
| ## CORS Support | ||
|
|
||
| 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. | ||
|
|
||
| ## Frontend Integration | ||
|
|
||
| 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: | ||
|
|
||
| - Key-value storage (`saveEntry`, `fetchEntry`) | ||
| - File operations (`saveFile`, `fetchFileContentText`, `list`, `rename`, `delete`) | ||
|
|
||
| Example usage in TypeScript: | ||
| ```typescript | ||
| import { ServerSideStorage } from './storage/server_side_storage'; | ||
|
|
||
| const storage = new ServerSideStorage(); | ||
|
|
||
| // Save and fetch key-value data | ||
| await storage.saveEntry('user_settings', JSON.stringify({theme: 'dark'})); | ||
| const settings = await storage.fetchEntry('user_settings', '{}'); | ||
|
|
||
| // File operations | ||
| await storage.saveFile('projects/robot1/main.py', 'print("Hello Robot!")'); | ||
| const content = await storage.fetchFileContentText('projects/robot1/main.py'); | ||
| const files = await storage.list('projects/'); | ||
| ``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.