tebakaja commited on
Commit
4c360fd
1 Parent(s): a1eed6b

update: fix stock reverse proxy

Browse files
Makefile CHANGED
@@ -1,6 +1,7 @@
1
  ENDPOINT=http://192.168.137.1:7860
2
 
3
- start: go run main.go
 
4
 
5
  haproxy-test:
6
  haproxy -f ./haproxy/haproxy.cfg
 
1
  ENDPOINT=http://192.168.137.1:7860
2
 
3
+ start:
4
+ go run main.go
5
 
6
  haproxy-test:
7
  haproxy -f ./haproxy/haproxy.cfg
proxy/middlewares.go CHANGED
@@ -9,7 +9,10 @@ import (
9
  "github.com/gofiber/fiber/v2/middleware/limiter"
10
  )
11
 
12
- // Logging Middleware
 
 
 
13
  func LoggingMiddleware(c *fiber.Ctx) error {
14
  start := time.Now()
15
  err := c.Next()
@@ -28,7 +31,7 @@ func LoggingMiddleware(c *fiber.Ctx) error {
28
  func RateLimiterMiddleware() func(*fiber.Ctx) error {
29
  return limiter.New(limiter.Config{
30
  Max: 200,
31
- Expiration: 1 * time.Minute,
32
 
33
  KeyGenerator: func(c *fiber.Ctx) string {
34
  return c.IP()
@@ -41,3 +44,4 @@ func RateLimiterMiddleware() func(*fiber.Ctx) error {
41
  },
42
  })
43
  }
 
 
9
  "github.com/gofiber/fiber/v2/middleware/limiter"
10
  )
11
 
12
+
13
+ /*
14
+ * --- Logging Middleware ---
15
+ */
16
  func LoggingMiddleware(c *fiber.Ctx) error {
17
  start := time.Now()
18
  err := c.Next()
 
31
  func RateLimiterMiddleware() func(*fiber.Ctx) error {
32
  return limiter.New(limiter.Config{
33
  Max: 200,
34
+ Expiration: (1 * time.Minute),
35
 
36
  KeyGenerator: func(c *fiber.Ctx) string {
37
  return c.IP()
 
44
  },
45
  })
46
  }
47
+
proxy/stock/list_service.go CHANGED
@@ -5,6 +5,7 @@ import (
5
  "context"
6
  "net/http"
7
  "encoding/json"
 
8
  proxy "tebakaja_lb_proxy/proxy"
9
  )
10
 
@@ -29,6 +30,7 @@ func (s *StockServiceImpl) StockListsService(ctx context.Context) (ApiResponse,
29
  StatusCode: http.StatusInternalServerError,
30
  }, err
31
  }
 
32
  defer resp.Body.Close()
33
 
34
  if resp.StatusCode != http.StatusOK {
 
5
  "context"
6
  "net/http"
7
  "encoding/json"
8
+
9
  proxy "tebakaja_lb_proxy/proxy"
10
  )
11
 
 
30
  StatusCode: http.StatusInternalServerError,
31
  }, err
32
  }
33
+
34
  defer resp.Body.Close()
35
 
36
  if resp.StatusCode != http.StatusOK {
proxy/stock/prediction_handler.go CHANGED
@@ -17,7 +17,7 @@ import (
17
  */
18
  func StockPredictionHandler(service StockService) fiber.Handler {
19
  return func(c *fiber.Ctx) error {
20
- ctx, cancel := context.WithTimeout(c.Context(), 120*time.Second)
21
  defer cancel()
22
 
23
  ch := make(chan ApiResponse, 1)
@@ -55,19 +55,19 @@ func StockPredictionHandler(service StockService) fiber.Handler {
55
  }()
56
 
57
  select {
58
- case apiResponse, ok := <-ch:
59
- if !ok {
60
- return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
61
- "error": "Failed to get a response from the server",
62
- })
63
- }
64
- return c.Status(apiResponse.StatusCode).JSON(apiResponse)
65
 
66
- case <-ctx.Done():
67
- log.Printf("[%s] Timeout: %v", time.Now().Format("2006-01-02 15:04:05"), ctx.Err())
68
- return c.Status(http.StatusRequestTimeout).JSON(fiber.Map{
69
- "error": "Request timeout",
70
- })
71
  }
72
  }
73
  }
 
17
  */
18
  func StockPredictionHandler(service StockService) fiber.Handler {
19
  return func(c *fiber.Ctx) error {
20
+ ctx, cancel := context.WithTimeout(c.Context(), 120 * time.Second)
21
  defer cancel()
22
 
23
  ch := make(chan ApiResponse, 1)
 
55
  }()
56
 
57
  select {
58
+ case apiResponse, ok := <-ch:
59
+ if !ok {
60
+ return c.Status(http.StatusInternalServerError).JSON(fiber.Map{
61
+ "error": "Failed to get a response from the server",
62
+ })
63
+ }
64
+ return c.Status(apiResponse.StatusCode).JSON(apiResponse)
65
 
66
+ case <-ctx.Done():
67
+ log.Printf("[%s] Timeout: %v", time.Now().Format("2006-01-02 15:04:05"), ctx.Err())
68
+ return c.Status(http.StatusRequestTimeout).JSON(fiber.Map{
69
+ "error": "Request timeout",
70
+ })
71
  }
72
  }
73
  }
proxy/stock/prediction_service.go CHANGED
@@ -6,6 +6,7 @@ import (
6
  "context"
7
  "net/http"
8
  "encoding/json"
 
9
  proxy "tebakaja_lb_proxy/proxy"
10
  )
11
 
 
6
  "context"
7
  "net/http"
8
  "encoding/json"
9
+
10
  proxy "tebakaja_lb_proxy/proxy"
11
  )
12