|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "backend/internal/config" |
| 5 | + "backend/internal/utils" |
| 6 | + "context" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/gin-gonic/gin" |
| 12 | +) |
| 13 | + |
| 14 | +func PostPdfSummariser() gin.HandlerFunc { |
| 15 | + return func(c *gin.Context) { |
| 16 | + ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second) |
| 17 | + defer cancel() |
| 18 | + |
| 19 | + var requestBody utils.PdfSummariserInput |
| 20 | + if err := c.BindJSON(&requestBody); err != nil { |
| 21 | + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) |
| 22 | + return |
| 23 | + } |
| 24 | + |
| 25 | + pdfurl := requestBody.PdfUrl |
| 26 | + req, err := http.NewRequestWithContext(ctx, "POST", config.BaseURL+"/v2/summarise/?pdf_url="+string(pdfurl), nil) |
| 27 | + if err != nil { |
| 28 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create request"}) |
| 29 | + return |
| 30 | + } |
| 31 | + req.Header.Set("Content-Type", "application/json") |
| 32 | + |
| 33 | + client := &http.Client{} |
| 34 | + resp, err := client.Do(req) |
| 35 | + if err != nil { |
| 36 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to send request"}) |
| 37 | + return |
| 38 | + } |
| 39 | + defer resp.Body.Close() |
| 40 | + |
| 41 | + body, err := io.ReadAll(resp.Body) |
| 42 | + if err != nil { |
| 43 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to read response"}) |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + c.Data(resp.StatusCode, "application/json", body) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func PostYoutubeVideoSummariser() gin.HandlerFunc { |
| 52 | + return func(c *gin.Context) { |
| 53 | + ctx, cancel := context.WithTimeout(context.Background(), 200*time.Second) |
| 54 | + defer cancel() |
| 55 | + |
| 56 | + var requestBody utils.YoutubeVideoSummariserInput |
| 57 | + if err := c.BindJSON(&requestBody); err != nil { |
| 58 | + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + youtubeUrl := requestBody.YoutubeUrl |
| 63 | + req, err := http.NewRequestWithContext(ctx, "POST", config.BaseURL+"/v2/summarise-youtube/?youtube_url="+string(youtubeUrl), nil) |
| 64 | + if err != nil { |
| 65 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create request"}) |
| 66 | + return |
| 67 | + } |
| 68 | + req.Header.Set("Content-Type", "application/json") |
| 69 | + |
| 70 | + client := &http.Client{} |
| 71 | + resp, err := client.Do(req) |
| 72 | + if err != nil { |
| 73 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to send request"}) |
| 74 | + return |
| 75 | + } |
| 76 | + defer resp.Body.Close() |
| 77 | + |
| 78 | + body, err := io.ReadAll(resp.Body) |
| 79 | + if err != nil { |
| 80 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to read response"}) |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + c.Data(resp.StatusCode, "application/json", body) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func PostGenerateMindMap() gin.HandlerFunc { |
| 89 | + return func(c *gin.Context) { |
| 90 | + ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second) |
| 91 | + defer cancel() |
| 92 | + |
| 93 | + var requestBody utils.MindMapInput |
| 94 | + if err := c.BindJSON(&requestBody); err != nil { |
| 95 | + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + topic := requestBody.Topic |
| 100 | + req, err := http.NewRequestWithContext(ctx, "POST", config.BaseURL+"/v2/generate_mindmap/"+string(topic), nil) |
| 101 | + if err != nil { |
| 102 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create request"}) |
| 103 | + return |
| 104 | + } |
| 105 | + req.Header.Set("Content-Type", "application/json") |
| 106 | + |
| 107 | + client := &http.Client{} |
| 108 | + resp, err := client.Do(req) |
| 109 | + if err != nil { |
| 110 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to send request"}) |
| 111 | + return |
| 112 | + } |
| 113 | + defer resp.Body.Close() |
| 114 | + |
| 115 | + body, err := io.ReadAll(resp.Body) |
| 116 | + if err != nil { |
| 117 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to read response"}) |
| 118 | + return |
| 119 | + } |
| 120 | + |
| 121 | + c.Data(resp.StatusCode, "application/json", body) |
| 122 | + } |
| 123 | +} |
0 commit comments