From 3cf9e5b399472ac344b12fb17890b94a66ad5341 Mon Sep 17 00:00:00 2001 From: Bobby Date: Mon, 20 Oct 2025 05:10:04 +0530 Subject: add handler for analysing request code --- handlers/analyze.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 handlers/analyze.go diff --git a/handlers/analyze.go b/handlers/analyze.go new file mode 100644 index 0000000..6943981 --- /dev/null +++ b/handlers/analyze.go @@ -0,0 +1,49 @@ +package handlers + +import ( + "complexity-analyzer/services" + + "github.com/gofiber/fiber/v2" +) + +type AnalyzeRequest struct { + Code string `json:"code"` + Language string `json:"language"` +} + +type AnalyzeResponse struct { + Complexity string `json:"complexity"` + Confidence float64 `json:"confidence"` + StaticAnalysis []string `json:"staticAnalysis"` + PerformanceData []PerformancePoint `json:"performanceData"` + Error string `json:"error,omitempty"` +} + +type PerformancePoint struct { + Size int `json:"size"` + Time float64 `json:"time"` +} + +func AnalyzeCode(c *fiber.Ctx) error { + var req AnalyzeRequest + if err := c.BodyParser(&req); err != nil { + return c.Status(400).JSON(fiber.Map{ + "error": "Invalid request body", + }) + } + + if req.Code == "" { + return c.Status(400).JSON(fiber.Map{ + "error": "Code is required", + }) + } + + result, err := services.AnalyzeWithGemini(req.Code, req.Language) + if err != nil { + return c.Status(500).JSON(fiber.Map{ + "error": err.Error(), + }) + } + + return c.JSON(result) +} -- cgit v1.2.3