From ba29133a8fd6591480d61335a47ed33c3a062be2 Mon Sep 17 00:00:00 2001 From: vandisi Date: Wed, 28 May 2025 09:31:50 +0300 Subject: [PATCH 1/4] feat: Add documentation for UploadHandler function --- backend/handler/upload.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/backend/handler/upload.go b/backend/handler/upload.go index 47e02b5..ec99466 100644 --- a/backend/handler/upload.go +++ b/backend/handler/upload.go @@ -11,6 +11,21 @@ import ( "github.com/google/uuid" ) +/* +UploadHandler handles document uploads and converts them to JSON. + +- Accepts multipart form data with a "document" file field (PDF, TXT, DOCX). +- Saves the uploaded file in the "files/" directory with a UUID filename. +- Converts the file to structured JSON via ParseDocumentToJSON(). +- Saves the output in the "data/" directory. + +Responds: +- 200: {"status": "success"} +- 400/500: {"error": "Error message"} + +Used by: POST /upload +*/ + func UploadHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") From e2c8411595401d069c3a0b0a035cb24b49aa5004 Mon Sep 17 00:00:00 2001 From: vandisi Date: Wed, 28 May 2025 09:45:28 +0300 Subject: [PATCH 2/4] fix: Change variable name from r to router, for clarity --- backend/route/routes.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/backend/route/routes.go b/backend/route/routes.go index 3461be1..75aa8a5 100644 --- a/backend/route/routes.go +++ b/backend/route/routes.go @@ -7,16 +7,16 @@ import ( ) func InitRoutes() *http.ServeMux { - r := http.NewServeMux() + router := http.NewServeMux() fs := http.FileServer(http.Dir("frontend/static")) - r.Handle("/static/", http.StripPrefix("/static/", fs)) + router.Handle("/static/", http.StripPrefix("/static/", fs)) - r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "frontend/templates/index.html") }) - r.HandleFunc("/upload", handler.UploadHandler) + router.HandleFunc("/upload", handler.UploadHandler) - return r + return router } From f3f6e661823bbdc91122c62b03aef416eda3f3de Mon Sep 17 00:00:00 2001 From: vandisi Date: Wed, 28 May 2025 09:46:30 +0300 Subject: [PATCH 3/4] feat: Add documentation of InitRoutes function --- backend/route/routes.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/backend/route/routes.go b/backend/route/routes.go index 75aa8a5..c2bd4d5 100644 --- a/backend/route/routes.go +++ b/backend/route/routes.go @@ -6,6 +6,26 @@ import ( "github.com/Vinolia-E/BioTree/backend/handler" ) +/* +InitRoutes sets up and returns the HTTP route multiplexer (ServeMux) for the BioTree web application. + +Routes: +1. /static/ (GET) + - Serves static assets (CSS, JS, images) from the frontend/static/ directory. + - Example: /static/style.css loads frontend/static/style.css. + +2. / (GET) + - Serves the main HTML page (frontend/templates/index.html) when users visit the root URL. + +3. /upload (POST) + - Accepts file uploads via multipart/form-data (PDF, DOCX, TXT). + - Handles uploaded documents and processes them into structured JSON via UploadHandler. + +Returns: +- A configured *http.ServeMux router to be passed to http.ListenAndServe. +*/ + + func InitRoutes() *http.ServeMux { router := http.NewServeMux() From 9a4567dabfed8bacd3138868b76745c1fa020dda Mon Sep 17 00:00:00 2001 From: vandisi Date: Wed, 28 May 2025 09:51:41 +0300 Subject: [PATCH 4/4] feat: Add documentation for RespondError and RespondSuccess functions --- backend/util/response.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/backend/util/response.go b/backend/util/response.go index a2d814d..0119fe7 100644 --- a/backend/util/response.go +++ b/backend/util/response.go @@ -5,6 +5,23 @@ import ( "net/http" ) +/* +RespondError writes a JSON response with status "error" and a message. +It sets the HTTP status code to 500 (Internal Server Error). +Example response: +{ + "status": "error", + "message": "Something went wrong" +} + +RespondSuccess writes a JSON response with status "ok" indicating success. +Example response: +{ + "status": "ok" +} +*/ + + func RespondError(w http.ResponseWriter, message string) { w.WriteHeader(http.StatusInternalServerError) json.NewEncoder(w).Encode(map[string]string{