File size: 7,780 Bytes
b7d67d4
b299b34
 
 
 
 
b7d67d4
b299b34
 
b7d67d4
 
b299b34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
---
title: ToDo API
emoji: 📝
colorFrom: blue
colorTo: green
sdk: docker
license: gpl-3.0
pinned: false
app_port: 5000
---

The table above contains the configuration for the Huggingface Space.
Just ignore it.

# Simple ToDo API

A simple RESTful API for managing todo items built with Python and Flask.

## Setup

1. Create a virtual environment:
   ```
   python -m venv venv
   ```

2. Activate the virtual environment:
   - Windows: `venv\Scripts\activate`
   - Unix/MacOS: `source venv/bin/activate`

3. Install dependencies:
   ```
   pip install -r requirements.txt
   ```

4. Set the environment variables:
   - Required:
     - Windows: `set TODO_TOKEN=your_secret_token`
     - Unix/MacOS: `export TODO_TOKEN=your_secret_token`

   - Optional:
     - Windows:
       ```
       set PAGE_TITLE=Your Custom Title
       set SHOW_ADMIN_PANEL_BUTTON=true
       set DEBUG_MODE=false
       ```
     - Unix/MacOS:
       ```
       export PAGE_TITLE="Your Custom Title"
       export SHOW_ADMIN_PANEL_BUTTON=true
       export DEBUG_MODE=false
       ```

5. Run the application:
   ```
   python app.py
   ```

The API will be available at `http://127.0.0.1:5000`.

## Environment Variables

The application supports the following environment variables:

| Variable | Description | Default Value | Required |
|----------|-------------|---------------|----------|
| `TODO_TOKEN` | Authentication token for API access | None | Yes |
| `PAGE_TITLE` | Custom title for the application | "ToDo App" | No |
| `SHOW_ADMIN_PANEL_BUTTON` | Whether to show the admin panel button on the home page | "true" | No |
| `DEBUG_MODE` | Enable Flask debug mode with auto-reloading and detailed error pages | "false" | No |

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.

## Authentication

The API uses two levels of authentication:

1. **Guest Access (Default Mode)**:
   - Guests can read todos (GET operations) without authentication
   - All write operations (POST, PUT, DELETE) require authentication

2. **Private Mode**:
   - When enabled, all operations (including GET) require authentication
   - This mode can be toggled by authenticated users

Authentication is done using a Bearer token in the Authorization header:

```
Authorization: Bearer your_secret_token
```

The token must match the value set in the `TODO_TOKEN` environment variable.

## Data Persistence

The application stores all data in JSON files located in the `data` directory:

- `todos.json`: Contains all todo items and the next ID counter
- `settings.json`: Contains application settings like private mode status

Data is automatically saved to these files whenever:
- A new todo is created
- A todo is updated
- A todo is deleted
- Private mode is toggled

This ensures that your data persists between application restarts.

The application also includes an automatic file repair mechanism that:
- Detects corrupted or empty JSON files on startup
- Creates new valid files with default values if needed
- Ensures the application can start even if data files are damaged

## Web Interface

The application provides two interfaces:

### Public View (`/`)
- View all todos (read-only)
- Link to admin panel

### Admin Panel (`/admin/`)
- Requires authentication with a valid token
- Add new todos
- Mark todos as completed
- Delete todos
- Toggle Private Mode
- Export todos as JSON file

## API Endpoints

### Get all todos
```
GET /api/todos
```
Requires authentication in private mode.

### Get a specific todo
```
GET /api/todos/<id>
```
Requires authentication in private mode.

### Create a new todo
```
POST /api/todos
```
Requires authentication.

Request body:
```json
{
  "title": "Task title",
  "description": "Task description",
  "completed": false
}
```

### Update a todo
```
PUT /api/todos/<id>
```
Requires authentication.

Request body:
```json
{
  "title": "Updated title",
  "description": "Updated description",
  "completed": true
}
```

### Delete a todo
```
DELETE /api/todos/<id>
```
Requires authentication.

### Get Private Mode status
```
GET /api/private-mode
```
No authentication required.

### Toggle Private Mode
```
POST /api/private-mode
```
Requires authentication.

Request body:
```json
{
  "enabled": true
}
```

### Export Todos as JSON
```
GET /api/todos/export
```
Requires authentication.

Returns a downloadable JSON file containing all todos with additional metadata. The response includes:
- Content-Disposition header for browser download
- Content-Type: application/json
- A JSON object with todos, next_id, exported_at timestamp, and count fields

## Example API Calls

### Reading todos (Guest Access)
```
curl -X GET http://127.0.0.1:5000/api/todos
```

### Reading todos (with Authentication)
```
curl -X GET http://127.0.0.1:5000/api/todos -H "Authorization: Bearer your_secret_token"
```

### Creating a todo (Always requires authentication)
```
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"}'
```

### Checking Private Mode status
```
curl -X GET http://127.0.0.1:5000/api/private-mode
```

### Enabling Private Mode
```
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}'
```

### Exporting Todos as JSON
```
curl -X GET http://127.0.0.1:5000/api/todos/export -H "Authorization: Bearer your_secret_token" -o todos_export.json
```

### Using PowerShell
```
# Get todos (Guest Access)
Invoke-RestMethod -Uri 'http://127.0.0.1:5000/api/todos' -Method Get

# Get todos (with Authentication)
Invoke-RestMethod -Uri 'http://127.0.0.1:5000/api/todos' -Method Get -Headers @{Authorization = "Bearer your_secret_token"}

# Check Private Mode status (no authentication required)
Invoke-RestMethod -Uri 'http://127.0.0.1:5000/api/private-mode' -Method Get

# Create a todo
$body = @{ title = 'New Task' } | ConvertTo-Json
Invoke-RestMethod -Uri 'http://127.0.0.1:5000/api/todos' -Method Post -Body $body -ContentType 'application/json' -Headers @{Authorization = "Bearer your_secret_token"}

# Enable Private Mode
$body = @{ enabled = $true } | ConvertTo-Json
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"}

# Export Todos as JSON
Invoke-RestMethod -Uri 'http://127.0.0.1:5000/api/todos/export' -Method Get -Headers @{Authorization = "Bearer your_secret_token"} -OutFile 'todos_export.json'
```

## Docker Support

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.

### Building the Docker Image Locally

```bash
docker build -t todo-api .
```

### Running the Container Locally

```bash
docker run -p 5000:5000 -e TODO_TOKEN=your_secret_token todo-api
```

### Using Environment Variables

You can pass environment variables to customize the application:

```bash
docker run -p 5000:5000 \
  -e TODO_TOKEN=your_secret_token \
  -e PAGE_TITLE="My Custom ToDo App" \
  -e SHOW_ADMIN_PANEL_BUTTON=true \
  -e DEBUG_MODE=false \
  todo-api
```

### Persisting Data

To persist data between container restarts, mount a volume to the `/app/data` directory:

```bash
docker run -p 5000:5000 \
  -e TODO_TOKEN=your_secret_token \
  -v $(pwd)/data:/app/data \
  todo-api
```

## Open Source

This project is open source and available under the [GNU GPLv3 License](LICENSE). Feel free to contribute and improve it!