Skip to content
Open

Docs #15

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions backend/handler/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
30 changes: 25 additions & 5 deletions backend/route/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,37 @@ 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 {
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
}
17 changes: 17 additions & 0 deletions backend/util/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down