Skip to content

Commit

Permalink
[#28] refactor: 분석 서버 변화 대응
Browse files Browse the repository at this point in the history
  • Loading branch information
YehyeokBang committed Sep 26, 2024
1 parent 03114e3 commit bb73e1e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
25 changes: 18 additions & 7 deletions app/report/services/report_service.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package services

import (
"bytes"
"encoding/json"
"fmt"
"gdsc/baro/app/report/models"
Expand Down Expand Up @@ -41,11 +42,9 @@ func NewReportService(reportRepository repositories.ReportRepositoryInterface, u
}

var REQUEST_URL string
var client *http.Client

func init() {
REQUEST_URL = os.Getenv("AI_SERVER_API_URL")
client = &http.Client{}
}

func (service *ReportService) Analysis(c *gin.Context, input types.RequestAnalysis) (string, error) {
Expand Down Expand Up @@ -77,11 +76,13 @@ func (service *ReportService) Analysis(c *gin.Context, input types.RequestAnalys
}

func Predict(service ReportService, url string, user usermodel.User, input types.RequestAnalysis) error {
response, err := HandleRequest(url)
response, err := HandleRequest(url, input.VideoURL)
if err != nil {
return err
}

fmt.Println("응답: ", response)

result, scores, nomalRatio, statusFrequencies, distances, landmarksInfo := ParseAnalysis(&response)
score := CalculateScores(result, scores)

Expand Down Expand Up @@ -110,26 +111,36 @@ func Predict(service ReportService, url string, user usermodel.User, input types
return nil
}

func HandleRequest(url string) (types.ResponseAnalysis, error) {
req, err := http.NewRequest("GET", url, nil)
func HandleRequest(url string, videoURL string) (types.ResponseAnalysis, error) {
requestBody, err := json.Marshal(map[string]string{
"video_url": videoURL,
})
if err != nil {
return types.ResponseAnalysis{}, err
}

response, err := client.Do(req)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
if err != nil {
return types.ResponseAnalysis{}, err
}
req.Header.Set("Content-Type", "application/json")

// HTTP 클라이언트 요청 보내기
client := &http.Client{}
response, err := client.Do(req)
if err != nil {
return types.ResponseAnalysis{}, err
}
defer response.Body.Close()

// 응답 바디 읽기
body, err := io.ReadAll(response.Body)
if err != nil {
return types.ResponseAnalysis{}, err
}

var data types.ResponseAnalysis
err = json.Unmarshal([]byte(body), &data)
err = json.Unmarshal(body, &data)
if err != nil {
fmt.Println("응답 파싱 에러:", err)
return types.ResponseAnalysis{}, err
Expand Down
2 changes: 1 addition & 1 deletion app/report/services/report_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func TestHandleRequest_Error(t *testing.T) {
url := ""

// Call the service
_, err := services.HandleRequest(url)
_, err := services.HandleRequest(url, url)
assert.Error(t, err)
}

Expand Down
4 changes: 4 additions & 0 deletions app/report/types/request_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ type RequestAnalysis struct {
Type string `json:"type" validate:"required"`
}

type RequestAnalysisToAi struct {
VideoURL string `json:"video_url"`
}

type RequestReportSummary struct {
YearAndMonth string `json:"year_and_month" validate:"required"`
}
Expand Down
1 change: 0 additions & 1 deletion app/report/types/response_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ type LandmarkInfo struct {
Angle float64 `json:"angle"`
}

// 전체 수정된 구조체
type ResponseReportSummary struct {
ID uint `json:"id"`
CreatedAt time.Time `json:"created_at"`
Expand Down

0 comments on commit bb73e1e

Please sign in to comment.