File size: 2,042 Bytes
542878f
 
 
 
 
 
 
 
 
 
 
 
 
 
f118f03
 
 
 
 
 
 
 
 
460c52a
a1eed6b
542878f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1eed6b
542878f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package national_currency

import (
	"fmt"
	"log"
	"sync"
	"time"
	"context"
	"net/http"

	"github.com/gofiber/fiber/v2"
)



// NationalCurrencyPredictionHandler  godoc
// @Summary                 National Currency Prediction
// @Description             National Currency Prediction
// @Tags                    National Currency
// @Accept                  json
// @Produce                 json
// @Param                   requestBody body PredictionRequest true "Request Body"
// @Success                 200  {object} PredictionResponse
// @Router                  /national-currency/prediction [post]
func NationalCurrencyPredictionHandler(service NationalCurrencyService) fiber.Handler {
	return func(c *fiber.Ctx) error {
		ctx, cancel := context.WithTimeout(c.Context(), 120*time.Second)
		defer cancel()

		ch := make(chan ApiResponse, 1)
		var wg sync.WaitGroup
		wg.Add(1)

		go func() {
			defer wg.Done()

			var predictionReq PredictionRequest
			if err := c.BodyParser(&predictionReq); err != nil {
				log.Printf("[%s] Failed to parse request body: %v",
					time.Now().Format("2006-01-02 15:04:05"), err)

				ch <- ApiResponse{
					Message:    fmt.Sprintf("Failed to parse request body: %v", err),
					StatusCode: http.StatusBadRequest,
				}
				return
			}

			apiResponse, err := service.NationalCurrencyPredictionService(ctx, predictionReq)
			if err != nil {
				log.Printf("[%s] %v", time.Now().Format("2006-01-02 15:04:05"), err)
				ch <- apiResponse
				return
			}

			ch <- apiResponse
		}()

		go func() {
			wg.Wait()
			close(ch)
		}()

		select {
		case apiResponse, ok := <-ch:
			if !ok {
				return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
					"error": "Failed to get a response from the server",
				})
			}
			return c.Status(apiResponse.StatusCode).JSON(apiResponse)

		case <-ctx.Done():
			log.Printf("[%s] Timeout: %v", time.Now().Format("2006-01-02 15:04:05"), ctx.Err())
			return c.Status(http.StatusRequestTimeout).JSON(fiber.Map{
				"error": "Request timeout",
			})
		}
	}
}