aboutsummaryrefslogtreecommitdiff
path: root/handlers/analyze.go
blob: 6943981673b414b9b472bfed6272c384e7b8ead3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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)
}