LukeZ-WIN commited on
Commit
b299b34
·
verified ·
1 Parent(s): b7d67d4

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +303 -8
README.md CHANGED
@@ -1,12 +1,307 @@
1
  ---
2
- title: ToDo.API
3
- emoji: 🏃
4
- colorFrom: purple
5
- colorTo: red
6
- sdk: static
7
- pinned: false
8
  license: gpl-3.0
9
- short_description: https://github.com/siiway/Todo.API
 
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: ToDo API
3
+ emoji: 📝
4
+ colorFrom: blue
5
+ colorTo: green
6
+ sdk: docker
 
7
  license: gpl-3.0
8
+ pinned: false
9
+ app_port: 5000
10
  ---
11
 
12
+ The table above contains the configuration for the Huggingface Space.
13
+ Just ignore it.
14
+
15
+ # Simple ToDo API
16
+
17
+ A simple RESTful API for managing todo items built with Python and Flask.
18
+
19
+ ## Setup
20
+
21
+ 1. Create a virtual environment:
22
+ ```
23
+ python -m venv venv
24
+ ```
25
+
26
+ 2. Activate the virtual environment:
27
+ - Windows: `venv\Scripts\activate`
28
+ - Unix/MacOS: `source venv/bin/activate`
29
+
30
+ 3. Install dependencies:
31
+ ```
32
+ pip install -r requirements.txt
33
+ ```
34
+
35
+ 4. Set the environment variables:
36
+ - Required:
37
+ - Windows: `set TODO_TOKEN=your_secret_token`
38
+ - Unix/MacOS: `export TODO_TOKEN=your_secret_token`
39
+
40
+ - Optional:
41
+ - Windows:
42
+ ```
43
+ set PAGE_TITLE=Your Custom Title
44
+ set SHOW_ADMIN_PANEL_BUTTON=true
45
+ set DEBUG_MODE=false
46
+ ```
47
+ - Unix/MacOS:
48
+ ```
49
+ export PAGE_TITLE="Your Custom Title"
50
+ export SHOW_ADMIN_PANEL_BUTTON=true
51
+ export DEBUG_MODE=false
52
+ ```
53
+
54
+ 5. Run the application:
55
+ ```
56
+ python app.py
57
+ ```
58
+
59
+ The API will be available at `http://127.0.0.1:5000`.
60
+
61
+ ## Environment Variables
62
+
63
+ The application supports the following environment variables:
64
+
65
+ | Variable | Description | Default Value | Required |
66
+ |----------|-------------|---------------|----------|
67
+ | `TODO_TOKEN` | Authentication token for API access | None | Yes |
68
+ | `PAGE_TITLE` | Custom title for the application | "ToDo App" | No |
69
+ | `SHOW_ADMIN_PANEL_BUTTON` | Whether to show the admin panel button on the home page | "true" | No |
70
+ | `DEBUG_MODE` | Enable Flask debug mode with auto-reloading and detailed error pages | "false" | No |
71
+
72
+ Note: For boolean environment variables like `SHOW_ADMIN_PANEL_BUTTON`, the values "true", "yes", "1", "y" (case-insensitive) are considered true. Any other value is considered false.
73
+
74
+ ## Authentication
75
+
76
+ The API uses two levels of authentication:
77
+
78
+ 1. **Guest Access (Default Mode)**:
79
+ - Guests can read todos (GET operations) without authentication
80
+ - All write operations (POST, PUT, DELETE) require authentication
81
+
82
+ 2. **Private Mode**:
83
+ - When enabled, all operations (including GET) require authentication
84
+ - This mode can be toggled by authenticated users
85
+
86
+ Authentication is done using a Bearer token in the Authorization header:
87
+
88
+ ```
89
+ Authorization: Bearer your_secret_token
90
+ ```
91
+
92
+ The token must match the value set in the `TODO_TOKEN` environment variable.
93
+
94
+ ## Data Persistence
95
+
96
+ The application stores all data in JSON files located in the `data` directory:
97
+
98
+ - `todos.json`: Contains all todo items and the next ID counter
99
+ - `settings.json`: Contains application settings like private mode status
100
+
101
+ Data is automatically saved to these files whenever:
102
+ - A new todo is created
103
+ - A todo is updated
104
+ - A todo is deleted
105
+ - Private mode is toggled
106
+
107
+ This ensures that your data persists between application restarts.
108
+
109
+ The application also includes an automatic file repair mechanism that:
110
+ - Detects corrupted or empty JSON files on startup
111
+ - Creates new valid files with default values if needed
112
+ - Ensures the application can start even if data files are damaged
113
+
114
+ ## Web Interface
115
+
116
+ The application provides two interfaces:
117
+
118
+ ### Public View (`/`)
119
+ - View all todos (read-only)
120
+ - Link to admin panel
121
+
122
+ ### Admin Panel (`/admin/`)
123
+ - Requires authentication with a valid token
124
+ - Add new todos
125
+ - Mark todos as completed
126
+ - Delete todos
127
+ - Toggle Private Mode
128
+ - Export todos as JSON file
129
+
130
+ ## API Endpoints
131
+
132
+ ### Get all todos
133
+ ```
134
+ GET /api/todos
135
+ ```
136
+ Requires authentication in private mode.
137
+
138
+ ### Get a specific todo
139
+ ```
140
+ GET /api/todos/<id>
141
+ ```
142
+ Requires authentication in private mode.
143
+
144
+ ### Create a new todo
145
+ ```
146
+ POST /api/todos
147
+ ```
148
+ Requires authentication.
149
+
150
+ Request body:
151
+ ```json
152
+ {
153
+ "title": "Task title",
154
+ "description": "Task description",
155
+ "completed": false
156
+ }
157
+ ```
158
+
159
+ ### Update a todo
160
+ ```
161
+ PUT /api/todos/<id>
162
+ ```
163
+ Requires authentication.
164
+
165
+ Request body:
166
+ ```json
167
+ {
168
+ "title": "Updated title",
169
+ "description": "Updated description",
170
+ "completed": true
171
+ }
172
+ ```
173
+
174
+ ### Delete a todo
175
+ ```
176
+ DELETE /api/todos/<id>
177
+ ```
178
+ Requires authentication.
179
+
180
+ ### Get Private Mode status
181
+ ```
182
+ GET /api/private-mode
183
+ ```
184
+ No authentication required.
185
+
186
+ ### Toggle Private Mode
187
+ ```
188
+ POST /api/private-mode
189
+ ```
190
+ Requires authentication.
191
+
192
+ Request body:
193
+ ```json
194
+ {
195
+ "enabled": true
196
+ }
197
+ ```
198
+
199
+ ### Export Todos as JSON
200
+ ```
201
+ GET /api/todos/export
202
+ ```
203
+ Requires authentication.
204
+
205
+ Returns a downloadable JSON file containing all todos with additional metadata. The response includes:
206
+ - Content-Disposition header for browser download
207
+ - Content-Type: application/json
208
+ - A JSON object with todos, next_id, exported_at timestamp, and count fields
209
+
210
+ ## Example API Calls
211
+
212
+ ### Reading todos (Guest Access)
213
+ ```
214
+ curl -X GET http://127.0.0.1:5000/api/todos
215
+ ```
216
+
217
+ ### Reading todos (with Authentication)
218
+ ```
219
+ curl -X GET http://127.0.0.1:5000/api/todos -H "Authorization: Bearer your_secret_token"
220
+ ```
221
+
222
+ ### Creating a todo (Always requires authentication)
223
+ ```
224
+ curl -X POST http://127.0.0.1:5000/api/todos -H "Content-Type: application/json" -H "Authorization: Bearer your_secret_token" -d '{"title": "New Task"}'
225
+ ```
226
+
227
+ ### Checking Private Mode status
228
+ ```
229
+ curl -X GET http://127.0.0.1:5000/api/private-mode
230
+ ```
231
+
232
+ ### Enabling Private Mode
233
+ ```
234
+ curl -X POST http://127.0.0.1:5000/api/private-mode -H "Content-Type: application/json" -H "Authorization: Bearer your_secret_token" -d '{"enabled": true}'
235
+ ```
236
+
237
+ ### Exporting Todos as JSON
238
+ ```
239
+ curl -X GET http://127.0.0.1:5000/api/todos/export -H "Authorization: Bearer your_secret_token" -o todos_export.json
240
+ ```
241
+
242
+ ### Using PowerShell
243
+ ```
244
+ # Get todos (Guest Access)
245
+ Invoke-RestMethod -Uri 'http://127.0.0.1:5000/api/todos' -Method Get
246
+
247
+ # Get todos (with Authentication)
248
+ Invoke-RestMethod -Uri 'http://127.0.0.1:5000/api/todos' -Method Get -Headers @{Authorization = "Bearer your_secret_token"}
249
+
250
+ # Check Private Mode status (no authentication required)
251
+ Invoke-RestMethod -Uri 'http://127.0.0.1:5000/api/private-mode' -Method Get
252
+
253
+ # Create a todo
254
+ $body = @{ title = 'New Task' } | ConvertTo-Json
255
+ Invoke-RestMethod -Uri 'http://127.0.0.1:5000/api/todos' -Method Post -Body $body -ContentType 'application/json' -Headers @{Authorization = "Bearer your_secret_token"}
256
+
257
+ # Enable Private Mode
258
+ $body = @{ enabled = $true } | ConvertTo-Json
259
+ Invoke-RestMethod -Uri 'http://127.0.0.1:5000/api/private-mode' -Method Post -Body $body -ContentType 'application/json' -Headers @{Authorization = "Bearer your_secret_token"}
260
+
261
+ # Export Todos as JSON
262
+ Invoke-RestMethod -Uri 'http://127.0.0.1:5000/api/todos/export' -Method Get -Headers @{Authorization = "Bearer your_secret_token"} -OutFile 'todos_export.json'
263
+ ```
264
+
265
+ ## Docker Support
266
+
267
+ This project includes Docker support for easy deployment on various platforms. The Dockerfile uses Ubuntu 22.04 as the base image and clones the repository during the build process, ensuring a lightweight and up-to-date deployment.
268
+
269
+ ### Building the Docker Image Locally
270
+
271
+ ```bash
272
+ docker build -t todo-api .
273
+ ```
274
+
275
+ ### Running the Container Locally
276
+
277
+ ```bash
278
+ docker run -p 5000:5000 -e TODO_TOKEN=your_secret_token todo-api
279
+ ```
280
+
281
+ ### Using Environment Variables
282
+
283
+ You can pass environment variables to customize the application:
284
+
285
+ ```bash
286
+ docker run -p 5000:5000 \
287
+ -e TODO_TOKEN=your_secret_token \
288
+ -e PAGE_TITLE="My Custom ToDo App" \
289
+ -e SHOW_ADMIN_PANEL_BUTTON=true \
290
+ -e DEBUG_MODE=false \
291
+ todo-api
292
+ ```
293
+
294
+ ### Persisting Data
295
+
296
+ To persist data between container restarts, mount a volume to the `/app/data` directory:
297
+
298
+ ```bash
299
+ docker run -p 5000:5000 \
300
+ -e TODO_TOKEN=your_secret_token \
301
+ -v $(pwd)/data:/app/data \
302
+ todo-api
303
+ ```
304
+
305
+ ## Open Source
306
+
307
+ This project is open source and available under the [GNU GPLv3 License](LICENSE). Feel free to contribute and improve it!