Automated Action 887703b6a2 Add Go Todo App implementation
This commit adds a complete Go implementation of the Todo application. The
application uses Gin framework for the web server, GORM for database access,
and SQLite for storage.

Key features:
- Todo CRUD operations with the same API endpoints
- Health check endpoint
- Database migrations
- Tests for models, services, and API handlers
- Documentation for the API
- Configurable settings
2025-05-17 22:20:05 +00:00

154 lines
4.3 KiB
Go

package api
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
"github.com/simpletodoapp/go-todo-app/internal/config"
)
// SetupDocs sets up the API documentation
func SetupDocs(router *gin.Engine, cfg *config.Config) {
// Simple documentation page
router.GET("/docs", func(c *gin.Context) {
html := fmt.Sprintf(`
<!DOCTYPE html>
<html>
<head>
<title>%s API Documentation</title>
<style>
body { font-family: Arial, sans-serif; line-height: 1.6; padding: 20px; max-width: 800px; margin: 0 auto; }
pre { background-color: #f5f5f5; padding: 10px; border-radius: 5px; overflow-x: auto; }
h1, h2, h3 { color: #333; }
.endpoint { margin-bottom: 30px; border-bottom: 1px solid #eee; padding-bottom: 20px; }
.method { font-weight: bold; display: inline-block; width: 80px; }
.url { font-family: monospace; }
.description { margin: 10px 0; }
</style>
</head>
<body>
<h1>%s API Documentation</h1>
<p>%s</p>
<p>Version: %s</p>
<h2>Endpoints</h2>
<div class="endpoint">
<h3>Health Check</h3>
<p><span class="method">GET</span> <span class="url">/health</span></p>
<p class="description">Check the health of the application and database.</p>
<h4>Response</h4>
<pre>{
"status": "healthy",
"db_status": "healthy"
}</pre>
</div>
<div class="endpoint">
<h3>List Todos</h3>
<p><span class="method">GET</span> <span class="url">/api/todos</span></p>
<p class="description">Retrieve a list of todos with optional filtering and pagination.</p>
<h4>Query Parameters</h4>
<ul>
<li><code>skip</code> - Number of items to skip (default: 0)</li>
<li><code>limit</code> - Maximum number of items to return (default: 100)</li>
<li><code>completed</code> - Filter by completion status (boolean, optional)</li>
</ul>
<h4>Response</h4>
<pre>[
{
"id": 1,
"title": "Learn Go",
"description": "Learn Go programming language",
"completed": false,
"created_at": "2023-09-10T12:00:00Z",
"updated_at": "2023-09-10T12:00:00Z"
}
]</pre>
</div>
<div class="endpoint">
<h3>Get Todo</h3>
<p><span class="method">GET</span> <span class="url">/api/todos/:id</span></p>
<p class="description">Retrieve a specific todo by ID.</p>
<h4>Path Parameters</h4>
<ul>
<li><code>id</code> - Todo ID</li>
</ul>
<h4>Response</h4>
<pre>{
"id": 1,
"title": "Learn Go",
"description": "Learn Go programming language",
"completed": false,
"created_at": "2023-09-10T12:00:00Z",
"updated_at": "2023-09-10T12:00:00Z"
}</pre>
</div>
<div class="endpoint">
<h3>Create Todo</h3>
<p><span class="method">POST</span> <span class="url">/api/todos</span></p>
<p class="description">Create a new todo item.</p>
<h4>Request Body</h4>
<pre>{
"title": "Learn Go",
"description": "Learn Go programming language",
"completed": false
}</pre>
<h4>Response</h4>
<pre>{
"id": 1,
"title": "Learn Go",
"description": "Learn Go programming language",
"completed": false,
"created_at": "2023-09-10T12:00:00Z",
"updated_at": "2023-09-10T12:00:00Z"
}</pre>
</div>
<div class="endpoint">
<h3>Update Todo</h3>
<p><span class="method">PUT</span> <span class="url">/api/todos/:id</span></p>
<p class="description">Update an existing todo.</p>
<h4>Path Parameters</h4>
<ul>
<li><code>id</code> - Todo ID</li>
</ul>
<h4>Request Body</h4>
<pre>{
"title": "Updated title", // Optional
"description": "Updated description", // Optional
"completed": true // Optional
}</pre>
<h4>Response</h4>
<pre>{
"id": 1,
"title": "Updated title",
"description": "Updated description",
"completed": true,
"created_at": "2023-09-10T12:00:00Z",
"updated_at": "2023-09-10T12:05:00Z"
}</pre>
</div>
<div class="endpoint">
<h3>Delete Todo</h3>
<p><span class="method">DELETE</span> <span class="url">/api/todos/:id</span></p>
<p class="description">Delete a todo.</p>
<h4>Path Parameters</h4>
<ul>
<li><code>id</code> - Todo ID</li>
</ul>
<h4>Response</h4>
<p>No content (HTTP 204)</p>
</div>
</body>
</html>
`, cfg.AppName, cfg.AppName, cfg.AppDescription, cfg.AppVersion)
c.Header("Content-Type", "text/html")
c.String(http.StatusOK, html)
})
}