Skip to content

Commit 7d22100

Browse files
authored
feat: Setup Swagger API documentation for backend (#120)
* feat: Setup Swagger API documentation for backend - Added swaggo/swag package for automated API documentation - Implemented Swagger annotations for all API endpoints: * Task management endpoints (get, add, edit, modify, complete, delete) * Authentication endpoints (OAuth, callback, user info, logout) - Added Swagger UI endpoint at /swagger/index.html - Updated go.mod with required dependencies (swaggo/swag, http-swagger) - Generated comprehensive API documentation - Created setup guides (SWAGGER_SETUP.md, QUICK_START.md) Benefits: - Auto-generated, always up-to-date API documentation - Interactive testing interface replacing Postman collection - Industry-standard OpenAPI/Swagger specification Closes #118 * Address PR review feedback for Swagger implementation - Remove auto-generated documentation files (IMPLEMENTATION_SUMMARY.md, QUICK_START.md, SWAGGER_SETUP.md) - Remove incorrect email address (support@ccsync.com) from swagger.json, swagger.yaml, and main.go - Update misleading comment in main.go about docs generation - Change Swagger endpoint from /swagger/ to /api/docs/ for better convention - Add API documentation section to backend README with endpoint URL * Regenerate swagger docs to remove email address - Run swag init to regenerate docs.go without support@ccsync.com - This fixes the auto-generated file to match main.go annotations * Fix formatting issues (gofmt and prettier) - Run gofmt on backend Go files - Run prettier on all project files - This fixes the failing CI checks * Trigger CI checks re-run
1 parent 740ae6c commit 7d22100

22 files changed

Lines changed: 2022 additions & 81 deletions

.github/workflows/backend-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
- name: Setup Go
1313
uses: actions/setup-go@v5
1414
with:
15-
go-version: '1.21.x'
15+
go-version: "1.21.x"
1616
- name: Install dependencies
1717
run: go get .
1818
working-directory: backend

.github/workflows/docker.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
steps:
1616
- name: Checkout repository code
1717
uses: actions/checkout@v4
18-
18+
1919
- name: Set up Docker Buildx
2020
uses: docker/setup-buildx-action@v3
2121

.github/workflows/prettier.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on:
55
- "main"
66
pull_request:
77
branches:
8-
- "main"
8+
- "main"
99
- "dev"
1010
jobs:
1111
prettier:

CONTRIBUTING.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ Scan through our [existing issues](https://github.com/its-me-abhishek/ccsync/iss
2525
1. Fork the repository.
2626

2727
- Using GitHub Desktop:
28-
2928
- [Getting started with GitHub Desktop](https://docs.github.com/en/desktop/installing-and-configuring-github-desktop/getting-started-with-github-desktop) will guide you through setting up Desktop.
3029
- Once Desktop is set up, you can use it to [fork the repo](https://docs.github.com/en/desktop/contributing-and-collaborating-using-github-desktop/cloning-and-forking-repositories-from-github-desktop)!
3130

backend/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,11 @@
5555
docker-compose build backend
5656
docker-compose up
5757
```
58+
59+
## API Documentation
60+
61+
Once the backend server is running, you can view the interactive API documentation (Swagger UI) at:
62+
63+
- **Local development**: http://localhost:8000/api/docs/index.html
64+
65+
The documentation provides detailed information about all available endpoints, request/response schemas, and allows you to test the API directly from your browser.

backend/controllers/add_task.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ import (
1111

1212
var GlobalJobQueue *JobQueue
1313

14+
// AddTaskHandler godoc
15+
// @Summary Add a new task
16+
// @Description Add a new task to Taskwarrior for a specific user
17+
// @Tags Tasks
18+
// @Accept json
19+
// @Produce json
20+
// @Param task body models.AddTaskRequestBody true "Task details"
21+
// @Success 202 {string} string "Task accepted for processing"
22+
// @Failure 400 {string} string "Bad request - invalid input or missing required fields"
23+
// @Failure 405 {string} string "Method not allowed"
24+
// @Router /add-task [post]
1425
func AddTaskHandler(w http.ResponseWriter, r *http.Request) {
1526
if r.Method == http.MethodPost {
1627
body, err := io.ReadAll(r.Body)

backend/controllers/app_handlers.go

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,30 @@ type App struct {
2020
UUID string
2121
}
2222

23+
// OAuthHandler godoc
24+
// @Summary Initiate OAuth login
25+
// @Description Redirects user to Google OAuth login page
26+
// @Tags Auth
27+
// @Accept json
28+
// @Produce json
29+
// @Success 307 {string} string "Redirect to OAuth provider"
30+
// @Router /auth/oauth [get]
2331
func (a *App) OAuthHandler(w http.ResponseWriter, r *http.Request) {
2432
url := a.Config.AuthCodeURL("state", oauth2.AccessTypeOffline)
2533
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
2634
}
2735

28-
// fetching the info
36+
// OAuthCallbackHandler godoc
37+
// @Summary OAuth callback handler
38+
// @Description Handles the OAuth callback and creates user session
39+
// @Tags Auth
40+
// @Accept json
41+
// @Produce json
42+
// @Param code query string true "OAuth authorization code"
43+
// @Success 303 {string} string "Redirect to frontend home page"
44+
// @Failure 400 {string} string "Bad request"
45+
// @Failure 500 {string} string "Internal server error"
46+
// @Router /auth/callback [get]
2947
func (a *App) OAuthCallbackHandler(w http.ResponseWriter, r *http.Request) {
3048
log.Println("Fetching user info...")
3149

@@ -75,6 +93,15 @@ func (a *App) OAuthCallbackHandler(w http.ResponseWriter, r *http.Request) {
7593
http.Redirect(w, r, frontendOriginDev+"/home", http.StatusSeeOther)
7694
}
7795

96+
// UserInfoHandler godoc
97+
// @Summary Get user information
98+
// @Description Retrieve user information from the session
99+
// @Tags Auth
100+
// @Accept json
101+
// @Produce json
102+
// @Success 200 {object} map[string]interface{} "User information including email, id, uuid, and encryption_secret"
103+
// @Failure 401 {string} string "No user info available - user not authenticated"
104+
// @Router /api/user [get]
78105
func (a *App) UserInfoHandler(w http.ResponseWriter, r *http.Request) {
79106
session, _ := a.SessionStore.Get(r, "session-name")
80107
userInfo, ok := session.Values["user"].(map[string]interface{})
@@ -103,7 +130,15 @@ func (a *App) EnableCORS(handler http.Handler) http.Handler {
103130
})
104131
}
105132

106-
// logout and delete session
133+
// LogoutHandler godoc
134+
// @Summary Logout user
135+
// @Description Logout user and delete session
136+
// @Tags Auth
137+
// @Accept json
138+
// @Produce json
139+
// @Success 200 {string} string "Successfully logged out"
140+
// @Failure 500 {string} string "Internal server error"
141+
// @Router /auth/logout [get]
107142
func (a *App) LogoutHandler(w http.ResponseWriter, r *http.Request) {
108143
session, _ := a.SessionStore.Get(r, "session-name")
109144
session.Options.MaxAge = -1

backend/controllers/complete_task.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ import (
99
"net/http"
1010
)
1111

12+
// CompleteTaskHandler godoc
13+
// @Summary Complete a task
14+
// @Description Mark a task as completed in Taskwarrior
15+
// @Tags Tasks
16+
// @Accept json
17+
// @Produce json
18+
// @Param task body models.CompleteTaskRequestBody true "Task completion details"
19+
// @Success 202 {string} string "Task completion accepted for processing"
20+
// @Failure 400 {string} string "Bad request - invalid input or missing taskuuid"
21+
// @Failure 405 {string} string "Method not allowed"
22+
// @Router /complete-task [post]
1223
func CompleteTaskHandler(w http.ResponseWriter, r *http.Request) {
1324
if r.Method == http.MethodPost {
1425
body, err := io.ReadAll(r.Body)

backend/controllers/delete_task.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ import (
99
"net/http"
1010
)
1111

12+
// DeleteTaskHandler godoc
13+
// @Summary Delete a task
14+
// @Description Delete a task from Taskwarrior
15+
// @Tags Tasks
16+
// @Accept json
17+
// @Produce json
18+
// @Param task body models.DeleteTaskRequestBody true "Task deletion details"
19+
// @Success 202 {string} string "Task deletion accepted for processing"
20+
// @Failure 400 {string} string "Bad request - invalid input or missing taskuuid"
21+
// @Failure 405 {string} string "Method not allowed"
22+
// @Router /delete-task [post]
1223
func DeleteTaskHandler(w http.ResponseWriter, r *http.Request) {
1324
if r.Method == http.MethodPost {
1425
body, err := io.ReadAll(r.Body)

backend/controllers/edit_task.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ import (
99
"net/http"
1010
)
1111

12+
// EditTaskHandler godoc
13+
// @Summary Edit a task
14+
// @Description Edit task description and tags in Taskwarrior
15+
// @Tags Tasks
16+
// @Accept json
17+
// @Produce json
18+
// @Param task body models.EditTaskRequestBody true "Task edit details"
19+
// @Success 202 {string} string "Task edit accepted for processing"
20+
// @Failure 400 {string} string "Bad request - invalid input or missing taskID"
21+
// @Failure 405 {string} string "Method not allowed"
22+
// @Router /edit-task [post]
1223
func EditTaskHandler(w http.ResponseWriter, r *http.Request) {
1324
if r.Method == http.MethodPost {
1425
body, err := io.ReadAll(r.Body)

0 commit comments

Comments
 (0)