Spaces:
Sleeping
Sleeping
feat: crafting stock and national currency proxy
Browse files- Makefile +16 -0
- main.go +29 -7
- proxy/crypto/constants.go +0 -3
- proxy/crypto/list_service.go +2 -1
- proxy/crypto/prediction_service.go +2 -1
- proxy/loadbalancers.go +0 -1
- proxy/middlewares.go +14 -5
- proxy/national_currency/constants.go +0 -3
- proxy/national_currency/interfaces.go +4 -4
- proxy/national_currency/list_handler.go +3 -3
- proxy/national_currency/list_service.go +5 -3
- proxy/national_currency/prediction_handler.go +3 -3
- proxy/national_currency/prediction_service.go +5 -4
- proxy/stock/constants.go +0 -3
- proxy/stock/interfaces.go +4 -4
- proxy/stock/list_handler.go +3 -3
- proxy/stock/list_service.go +4 -3
- proxy/stock/prediction_handler.go +3 -3
- proxy/stock/prediction_service.go +4 -3
- proxy/stock/structs.go +3 -0
- proxy/utils.go +45 -0
Makefile
CHANGED
@@ -18,6 +18,14 @@ traefik-test:
|
|
18 |
--api.dashboard=true \
|
19 |
--api.insecure=false
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
crypto-list-test:
|
22 |
curl -X GET $(ENDPOINT)/crypto/lists
|
23 |
|
@@ -25,4 +33,12 @@ crypto-prediction-test:
|
|
25 |
curl -X POST $(ENDPOINT)/crypto/prediction \
|
26 |
-H "Content-Type: application/json" \
|
27 |
-d "{\"days\": 2, \"currency\": \"BTC-USD\"}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
|
|
18 |
--api.dashboard=true \
|
19 |
--api.insecure=false
|
20 |
|
21 |
+
stock-list-test:
|
22 |
+
curl -X GET $(ENDPOINT)/stock/lists
|
23 |
+
|
24 |
+
stock-prediction-test:
|
25 |
+
curl -X POST $(ENDPOINT)/stock/prediction \
|
26 |
+
-H "Content-Type: application/json" \
|
27 |
+
-d "{\"days\": 2, \"currency\": \"BTC-USD\"}"
|
28 |
+
|
29 |
crypto-list-test:
|
30 |
curl -X GET $(ENDPOINT)/crypto/lists
|
31 |
|
|
|
33 |
curl -X POST $(ENDPOINT)/crypto/prediction \
|
34 |
-H "Content-Type: application/json" \
|
35 |
-d "{\"days\": 2, \"currency\": \"BTC-USD\"}"
|
36 |
+
|
37 |
+
natcurr-list-test:
|
38 |
+
curl -X GET $(ENDPOINT)/national-currency/lists
|
39 |
+
|
40 |
+
natcurr-prediction-test:
|
41 |
+
curl -X POST $(ENDPOINT)/national-currency/prediction \
|
42 |
+
-H "Content-Type: application/json" \
|
43 |
+
-d "{\"days\": 2, \"currency\": \"BTC-USD\"}"
|
44 |
|
main.go
CHANGED
@@ -7,19 +7,41 @@ import (
|
|
7 |
"github.com/gofiber/fiber/v2"
|
8 |
|
9 |
proxy "tebakaja_lb_proxy/proxy"
|
|
|
|
|
10 |
crypto_proxy "tebakaja_lb_proxy/proxy/crypto"
|
|
|
11 |
)
|
12 |
|
13 |
func main() {
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
22 |
|
23 |
port := 7860
|
24 |
-
log.Fatal(
|
25 |
}
|
|
|
7 |
"github.com/gofiber/fiber/v2"
|
8 |
|
9 |
proxy "tebakaja_lb_proxy/proxy"
|
10 |
+
|
11 |
+
stock_proxy "tebakaja_lb_proxy/proxy/stock"
|
12 |
crypto_proxy "tebakaja_lb_proxy/proxy/crypto"
|
13 |
+
national_currency_proxy "tebakaja_lb_proxy/proxy/national_currency"
|
14 |
)
|
15 |
|
16 |
func main() {
|
17 |
+
proxyService := fiber.New()
|
18 |
+
proxyService.Use(proxy.LoggingMiddleware)
|
19 |
+
proxyService.Use(proxy.RateLimiterMiddleware())
|
20 |
+
|
21 |
+
stockGroup := proxyService.Group("/stock")
|
22 |
+
stockGroup.Get("/lists",
|
23 |
+
stock_proxy.StockListsHandler(
|
24 |
+
&stock_proxy.StockServiceImpl{}))
|
25 |
+
stockGroup.Post("/prediction",
|
26 |
+
stock_proxy.StockPredictionHandler(
|
27 |
+
&stock_proxy.StockServiceImpl{}))
|
28 |
|
29 |
+
cryptoGroup := proxyService.Group("/crypto")
|
30 |
+
cryptoGroup.Get("/lists",
|
31 |
+
crypto_proxy.CryptoListsHandler(
|
32 |
+
&crypto_proxy.CryptoServiceImpl{}))
|
33 |
+
cryptoGroup.Post("/prediction",
|
34 |
+
crypto_proxy.CryptoPredictionHandler(
|
35 |
+
&crypto_proxy.CryptoServiceImpl{}))
|
36 |
|
37 |
+
nationalCurrencyGroup := proxyService.Group("/national-currency")
|
38 |
+
nationalCurrencyGroup.Get("/lists",
|
39 |
+
national_currency_proxy.NationalCurrencyListsHandler(
|
40 |
+
&national_currency_proxy.NationalCurrencyServiceImpl{}))
|
41 |
+
nationalCurrencyGroup.Post("/prediction",
|
42 |
+
national_currency_proxy.NationalCurrencyPredictionHandler(
|
43 |
+
&national_currency_proxy.NationalCurrencyServiceImpl{}))
|
44 |
|
45 |
port := 7860
|
46 |
+
log.Fatal(proxyService.Listen(fmt.Sprintf("0.0.0.0:%d", port)))
|
47 |
}
|
proxy/crypto/constants.go
DELETED
@@ -1,3 +0,0 @@
|
|
1 |
-
package crypto
|
2 |
-
|
3 |
-
const CRYPTO_ENDPOINT = "https://qywok-cryptocurrency-prediction.hf.space/crypto"
|
|
|
|
|
|
|
|
proxy/crypto/list_service.go
CHANGED
@@ -5,6 +5,7 @@ import (
|
|
5 |
"context"
|
6 |
"net/http"
|
7 |
"encoding/json"
|
|
|
8 |
)
|
9 |
|
10 |
|
@@ -12,7 +13,7 @@ import (
|
|
12 |
* --- Cryptocurrency Prediction Model Lists Service ---
|
13 |
*/
|
14 |
func (s *CryptoServiceImpl) CryptoListsService(ctx context.Context) (ApiResponse, error) {
|
15 |
-
endpoint := fmt.Sprintf("%s/lists",
|
16 |
req, err := http.NewRequestWithContext(ctx, "GET", endpoint, nil)
|
17 |
if err != nil {
|
18 |
return ApiResponse{
|
|
|
5 |
"context"
|
6 |
"net/http"
|
7 |
"encoding/json"
|
8 |
+
proxy "tebakaja_lb_proxy/proxy"
|
9 |
)
|
10 |
|
11 |
|
|
|
13 |
* --- Cryptocurrency Prediction Model Lists Service ---
|
14 |
*/
|
15 |
func (s *CryptoServiceImpl) CryptoListsService(ctx context.Context) (ApiResponse, error) {
|
16 |
+
endpoint := fmt.Sprintf("%s/lists", proxy.GetEndpointByRestService("crypto"))
|
17 |
req, err := http.NewRequestWithContext(ctx, "GET", endpoint, nil)
|
18 |
if err != nil {
|
19 |
return ApiResponse{
|
proxy/crypto/prediction_service.go
CHANGED
@@ -6,6 +6,7 @@ import (
|
|
6 |
"context"
|
7 |
"net/http"
|
8 |
"encoding/json"
|
|
|
9 |
)
|
10 |
|
11 |
|
@@ -21,7 +22,7 @@ func (s *CryptoServiceImpl) CryptoPredictionService(ctx context.Context, req Pre
|
|
21 |
}, err
|
22 |
}
|
23 |
|
24 |
-
endpoint := fmt.Sprintf("%s/prediction",
|
25 |
httpReq, err := http.NewRequestWithContext(ctx, "POST", endpoint, bytes.NewBuffer(reqBody))
|
26 |
if err != nil {
|
27 |
return ApiResponse{
|
|
|
6 |
"context"
|
7 |
"net/http"
|
8 |
"encoding/json"
|
9 |
+
proxy "tebakaja_lb_proxy/proxy"
|
10 |
)
|
11 |
|
12 |
|
|
|
22 |
}, err
|
23 |
}
|
24 |
|
25 |
+
endpoint := fmt.Sprintf("%s/prediction", proxy.GetEndpointByRestService("crypto"))
|
26 |
httpReq, err := http.NewRequestWithContext(ctx, "POST", endpoint, bytes.NewBuffer(reqBody))
|
27 |
if err != nil {
|
28 |
return ApiResponse{
|
proxy/loadbalancers.go
DELETED
@@ -1 +0,0 @@
|
|
1 |
-
package proxy
|
|
|
|
proxy/middlewares.go
CHANGED
@@ -9,22 +9,31 @@ import (
|
|
9 |
"github.com/gofiber/fiber/v2/middleware/limiter"
|
10 |
)
|
11 |
|
12 |
-
// Middleware
|
13 |
func LoggingMiddleware(c *fiber.Ctx) error {
|
14 |
start := time.Now()
|
15 |
-
err
|
16 |
-
|
|
|
|
|
|
|
|
|
17 |
return err
|
18 |
}
|
19 |
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
22 |
return limiter.New(limiter.Config{
|
23 |
Max: 200,
|
24 |
Expiration: 1 * time.Minute,
|
|
|
25 |
KeyGenerator: func(c *fiber.Ctx) string {
|
26 |
return c.IP()
|
27 |
},
|
|
|
28 |
LimitReached: func(c *fiber.Ctx) error {
|
29 |
return c.Status(http.StatusTooManyRequests).JSON(fiber.Map{
|
30 |
"error": "Rate limit exceeded",
|
|
|
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()
|
16 |
+
|
17 |
+
log.Printf("[%s] %s %s - %d %s in %v", time.Now().Format("2006-01-02 15:04:05"),
|
18 |
+
c.Method(), c.Path(), c.Response().StatusCode(),
|
19 |
+
http.StatusText(c.Response().StatusCode()), time.Since(start))
|
20 |
+
|
21 |
return err
|
22 |
}
|
23 |
|
24 |
+
|
25 |
+
/*
|
26 |
+
* --- Rate Limiter Middleware ---
|
27 |
+
*/
|
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()
|
35 |
},
|
36 |
+
|
37 |
LimitReached: func(c *fiber.Ctx) error {
|
38 |
return c.Status(http.StatusTooManyRequests).JSON(fiber.Map{
|
39 |
"error": "Rate limit exceeded",
|
proxy/national_currency/constants.go
DELETED
@@ -1,3 +0,0 @@
|
|
1 |
-
package national_currency
|
2 |
-
|
3 |
-
const CRYPTO_ENDPOINT = "https://qywok-cryptocurrency-prediction.hf.space/crypto"
|
|
|
|
|
|
|
|
proxy/national_currency/interfaces.go
CHANGED
@@ -2,9 +2,9 @@ package national_currency
|
|
2 |
|
3 |
import "context"
|
4 |
|
5 |
-
type
|
6 |
-
|
7 |
-
|
8 |
}
|
9 |
|
10 |
-
type
|
|
|
2 |
|
3 |
import "context"
|
4 |
|
5 |
+
type NationalCurrencyService interface {
|
6 |
+
NationalCurrencyListsService(ctx context.Context) (ApiResponse, error)
|
7 |
+
NationalCurrencyPredictionService(ctx context.Context, req PredictionRequest) (ApiResponse, error)
|
8 |
}
|
9 |
|
10 |
+
type NationalCurrencyServiceImpl struct{}
|
proxy/national_currency/list_handler.go
CHANGED
@@ -12,9 +12,9 @@ import (
|
|
12 |
|
13 |
|
14 |
/*
|
15 |
-
* ---
|
16 |
*/
|
17 |
-
func
|
18 |
return func(c *fiber.Ctx) error {
|
19 |
ctx, cancel := context.WithTimeout(c.Context(), 120*time.Second)
|
20 |
defer cancel()
|
@@ -26,7 +26,7 @@ func CryptoListsHandler(service CryptoService) fiber.Handler {
|
|
26 |
go func() {
|
27 |
defer wg.Done()
|
28 |
|
29 |
-
apiResponse, err := service.
|
30 |
if err != nil {
|
31 |
log.Printf("[%s] %v", time.Now().Format("2006-01-02 15:04:05"), err)
|
32 |
ch <- apiResponse
|
|
|
12 |
|
13 |
|
14 |
/*
|
15 |
+
* --- National Currency Prediction Model Lists Handler ---
|
16 |
*/
|
17 |
+
func NationalCurrencyListsHandler(service NationalCurrencyService) fiber.Handler {
|
18 |
return func(c *fiber.Ctx) error {
|
19 |
ctx, cancel := context.WithTimeout(c.Context(), 120*time.Second)
|
20 |
defer cancel()
|
|
|
26 |
go func() {
|
27 |
defer wg.Done()
|
28 |
|
29 |
+
apiResponse, err := service.NationalCurrencyListsService(ctx)
|
30 |
if err != nil {
|
31 |
log.Printf("[%s] %v", time.Now().Format("2006-01-02 15:04:05"), err)
|
32 |
ch <- apiResponse
|
proxy/national_currency/list_service.go
CHANGED
@@ -5,14 +5,15 @@ import (
|
|
5 |
"context"
|
6 |
"net/http"
|
7 |
"encoding/json"
|
|
|
8 |
)
|
9 |
|
10 |
|
11 |
/*
|
12 |
-
* ---
|
13 |
*/
|
14 |
-
func (s *
|
15 |
-
endpoint := fmt.Sprintf("%s/lists",
|
16 |
req, err := http.NewRequestWithContext(ctx, "GET", endpoint, nil)
|
17 |
if err != nil {
|
18 |
return ApiResponse{
|
@@ -28,6 +29,7 @@ func (s *CryptoServiceImpl) CryptoListsService(ctx context.Context) (ApiResponse
|
|
28 |
StatusCode: http.StatusInternalServerError,
|
29 |
}, err
|
30 |
}
|
|
|
31 |
defer resp.Body.Close()
|
32 |
|
33 |
if resp.StatusCode != http.StatusOK {
|
|
|
5 |
"context"
|
6 |
"net/http"
|
7 |
"encoding/json"
|
8 |
+
proxy "tebakaja_lb_proxy/proxy"
|
9 |
)
|
10 |
|
11 |
|
12 |
/*
|
13 |
+
* --- National Currency Prediction Model Lists Service ---
|
14 |
*/
|
15 |
+
func (s *NationalCurrencyServiceImpl) NationalCurrencyListsService(ctx context.Context) (ApiResponse, error) {
|
16 |
+
endpoint := fmt.Sprintf("%s/lists", proxy.GetEndpointByRestService("national"))
|
17 |
req, err := http.NewRequestWithContext(ctx, "GET", endpoint, nil)
|
18 |
if err != nil {
|
19 |
return ApiResponse{
|
|
|
29 |
StatusCode: http.StatusInternalServerError,
|
30 |
}, err
|
31 |
}
|
32 |
+
|
33 |
defer resp.Body.Close()
|
34 |
|
35 |
if resp.StatusCode != http.StatusOK {
|
proxy/national_currency/prediction_handler.go
CHANGED
@@ -13,9 +13,9 @@ import (
|
|
13 |
|
14 |
|
15 |
/*
|
16 |
-
* ---
|
17 |
*/
|
18 |
-
func
|
19 |
return func(c *fiber.Ctx) error {
|
20 |
ctx, cancel := context.WithTimeout(c.Context(), 120*time.Second)
|
21 |
defer cancel()
|
@@ -39,7 +39,7 @@ func CryptoPredictionHandler(service CryptoService) fiber.Handler {
|
|
39 |
return
|
40 |
}
|
41 |
|
42 |
-
apiResponse, err := service.
|
43 |
if err != nil {
|
44 |
log.Printf("[%s] %v", time.Now().Format("2006-01-02 15:04:05"), err)
|
45 |
ch <- apiResponse
|
|
|
13 |
|
14 |
|
15 |
/*
|
16 |
+
* --- National Currency Prediction Handler ---
|
17 |
*/
|
18 |
+
func NationalCurrencyPredictionHandler(service NationalCurrencyService) fiber.Handler {
|
19 |
return func(c *fiber.Ctx) error {
|
20 |
ctx, cancel := context.WithTimeout(c.Context(), 120*time.Second)
|
21 |
defer cancel()
|
|
|
39 |
return
|
40 |
}
|
41 |
|
42 |
+
apiResponse, err := service.NationalCurrencyPredictionService(ctx, predictionReq)
|
43 |
if err != nil {
|
44 |
log.Printf("[%s] %v", time.Now().Format("2006-01-02 15:04:05"), err)
|
45 |
ch <- apiResponse
|
proxy/national_currency/prediction_service.go
CHANGED
@@ -6,13 +6,14 @@ import (
|
|
6 |
"context"
|
7 |
"net/http"
|
8 |
"encoding/json"
|
9 |
-
|
|
|
10 |
|
11 |
|
12 |
/*
|
13 |
-
* ---
|
14 |
*/
|
15 |
-
func (s *
|
16 |
reqBody, err := json.Marshal(req)
|
17 |
if err != nil {
|
18 |
return ApiResponse{
|
@@ -21,7 +22,7 @@ func (s *CryptoServiceImpl) CryptoPredictionService(ctx context.Context, req Pre
|
|
21 |
}, err
|
22 |
}
|
23 |
|
24 |
-
endpoint := fmt.Sprintf("%s/prediction",
|
25 |
httpReq, err := http.NewRequestWithContext(ctx, "POST", endpoint, bytes.NewBuffer(reqBody))
|
26 |
if err != nil {
|
27 |
return ApiResponse{
|
|
|
6 |
"context"
|
7 |
"net/http"
|
8 |
"encoding/json"
|
9 |
+
proxy "tebakaja_lb_proxy/proxy"
|
10 |
+
)
|
11 |
|
12 |
|
13 |
/*
|
14 |
+
* --- National Currency Prediction Service ---
|
15 |
*/
|
16 |
+
func (s *NationalCurrencyServiceImpl) NationalCurrencyPredictionService(ctx context.Context, req PredictionRequest) (ApiResponse, error) {
|
17 |
reqBody, err := json.Marshal(req)
|
18 |
if err != nil {
|
19 |
return ApiResponse{
|
|
|
22 |
}, err
|
23 |
}
|
24 |
|
25 |
+
endpoint := fmt.Sprintf("%s/prediction", proxy.GetEndpointByRestService("national"))
|
26 |
httpReq, err := http.NewRequestWithContext(ctx, "POST", endpoint, bytes.NewBuffer(reqBody))
|
27 |
if err != nil {
|
28 |
return ApiResponse{
|
proxy/stock/constants.go
DELETED
@@ -1,3 +0,0 @@
|
|
1 |
-
package stock
|
2 |
-
|
3 |
-
const CRYPTO_ENDPOINT = "https://qywok-cryptocurrency-prediction.hf.space/crypto"
|
|
|
|
|
|
|
|
proxy/stock/interfaces.go
CHANGED
@@ -2,9 +2,9 @@ package stock
|
|
2 |
|
3 |
import "context"
|
4 |
|
5 |
-
type
|
6 |
-
|
7 |
-
|
8 |
}
|
9 |
|
10 |
-
type
|
|
|
2 |
|
3 |
import "context"
|
4 |
|
5 |
+
type StockService interface {
|
6 |
+
StockListsService(ctx context.Context) (ApiResponse, error)
|
7 |
+
StockPredictionService(ctx context.Context, req PredictionRequest) (ApiResponse, error)
|
8 |
}
|
9 |
|
10 |
+
type StockServiceImpl struct{}
|
proxy/stock/list_handler.go
CHANGED
@@ -12,9 +12,9 @@ import (
|
|
12 |
|
13 |
|
14 |
/*
|
15 |
-
* ---
|
16 |
*/
|
17 |
-
func
|
18 |
return func(c *fiber.Ctx) error {
|
19 |
ctx, cancel := context.WithTimeout(c.Context(), 120*time.Second)
|
20 |
defer cancel()
|
@@ -26,7 +26,7 @@ func CryptoListsHandler(service CryptoService) fiber.Handler {
|
|
26 |
go func() {
|
27 |
defer wg.Done()
|
28 |
|
29 |
-
apiResponse, err := service.
|
30 |
if err != nil {
|
31 |
log.Printf("[%s] %v", time.Now().Format("2006-01-02 15:04:05"), err)
|
32 |
ch <- apiResponse
|
|
|
12 |
|
13 |
|
14 |
/*
|
15 |
+
* --- Stock Prediction Model Lists Handler ---
|
16 |
*/
|
17 |
+
func StockListsHandler(service StockService) fiber.Handler {
|
18 |
return func(c *fiber.Ctx) error {
|
19 |
ctx, cancel := context.WithTimeout(c.Context(), 120*time.Second)
|
20 |
defer cancel()
|
|
|
26 |
go func() {
|
27 |
defer wg.Done()
|
28 |
|
29 |
+
apiResponse, err := service.StockListsService(ctx)
|
30 |
if err != nil {
|
31 |
log.Printf("[%s] %v", time.Now().Format("2006-01-02 15:04:05"), err)
|
32 |
ch <- apiResponse
|
proxy/stock/list_service.go
CHANGED
@@ -5,14 +5,15 @@ import (
|
|
5 |
"context"
|
6 |
"net/http"
|
7 |
"encoding/json"
|
|
|
8 |
)
|
9 |
|
10 |
|
11 |
/*
|
12 |
-
* ---
|
13 |
*/
|
14 |
-
func (s *
|
15 |
-
endpoint := fmt.Sprintf("%s/lists",
|
16 |
req, err := http.NewRequestWithContext(ctx, "GET", endpoint, nil)
|
17 |
if err != nil {
|
18 |
return ApiResponse{
|
|
|
5 |
"context"
|
6 |
"net/http"
|
7 |
"encoding/json"
|
8 |
+
proxy "tebakaja_lb_proxy/proxy"
|
9 |
)
|
10 |
|
11 |
|
12 |
/*
|
13 |
+
* --- Stock Prediction Model Lists Service ---
|
14 |
*/
|
15 |
+
func (s *StockServiceImpl) StockListsService(ctx context.Context) (ApiResponse, error) {
|
16 |
+
endpoint := fmt.Sprintf("%s/lists", proxy.GetEndpointByRestService("stock"))
|
17 |
req, err := http.NewRequestWithContext(ctx, "GET", endpoint, nil)
|
18 |
if err != nil {
|
19 |
return ApiResponse{
|
proxy/stock/prediction_handler.go
CHANGED
@@ -13,9 +13,9 @@ import (
|
|
13 |
|
14 |
|
15 |
/*
|
16 |
-
* ---
|
17 |
*/
|
18 |
-
func
|
19 |
return func(c *fiber.Ctx) error {
|
20 |
ctx, cancel := context.WithTimeout(c.Context(), 120*time.Second)
|
21 |
defer cancel()
|
@@ -39,7 +39,7 @@ func CryptoPredictionHandler(service CryptoService) fiber.Handler {
|
|
39 |
return
|
40 |
}
|
41 |
|
42 |
-
apiResponse, err := service.
|
43 |
if err != nil {
|
44 |
log.Printf("[%s] %v", time.Now().Format("2006-01-02 15:04:05"), err)
|
45 |
ch <- apiResponse
|
|
|
13 |
|
14 |
|
15 |
/*
|
16 |
+
* --- Stock Prediction Handler ---
|
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()
|
|
|
39 |
return
|
40 |
}
|
41 |
|
42 |
+
apiResponse, err := service.StockPredictionService(ctx, predictionReq)
|
43 |
if err != nil {
|
44 |
log.Printf("[%s] %v", time.Now().Format("2006-01-02 15:04:05"), err)
|
45 |
ch <- apiResponse
|
proxy/stock/prediction_service.go
CHANGED
@@ -6,13 +6,14 @@ import (
|
|
6 |
"context"
|
7 |
"net/http"
|
8 |
"encoding/json"
|
|
|
9 |
)
|
10 |
|
11 |
|
12 |
/*
|
13 |
-
* ---
|
14 |
*/
|
15 |
-
func (s *
|
16 |
reqBody, err := json.Marshal(req)
|
17 |
if err != nil {
|
18 |
return ApiResponse{
|
@@ -21,7 +22,7 @@ func (s *CryptoServiceImpl) CryptoPredictionService(ctx context.Context, req Pre
|
|
21 |
}, err
|
22 |
}
|
23 |
|
24 |
-
endpoint := fmt.Sprintf("%s/prediction",
|
25 |
httpReq, err := http.NewRequestWithContext(ctx, "POST", endpoint, bytes.NewBuffer(reqBody))
|
26 |
if err != nil {
|
27 |
return ApiResponse{
|
|
|
6 |
"context"
|
7 |
"net/http"
|
8 |
"encoding/json"
|
9 |
+
proxy "tebakaja_lb_proxy/proxy"
|
10 |
)
|
11 |
|
12 |
|
13 |
/*
|
14 |
+
* --- Stock Prediction Service ---
|
15 |
*/
|
16 |
+
func (s *StockServiceImpl) StockPredictionService(ctx context.Context, req PredictionRequest) (ApiResponse, error) {
|
17 |
reqBody, err := json.Marshal(req)
|
18 |
if err != nil {
|
19 |
return ApiResponse{
|
|
|
22 |
}, err
|
23 |
}
|
24 |
|
25 |
+
endpoint := fmt.Sprintf("%s/prediction", proxy.GetEndpointByRestService("stock"))
|
26 |
httpReq, err := http.NewRequestWithContext(ctx, "POST", endpoint, bytes.NewBuffer(reqBody))
|
27 |
if err != nil {
|
28 |
return ApiResponse{
|
proxy/stock/structs.go
CHANGED
@@ -18,14 +18,17 @@ type PredictionResponse struct {
|
|
18 |
Data struct {
|
19 |
Currency string `json:"currency"`
|
20 |
Predictions struct {
|
|
|
21 |
Actuals []struct {
|
22 |
Date string `json:"date"`
|
23 |
Price float64 `json:"price"`
|
24 |
} `json:"actuals"`
|
|
|
25 |
Predictions []struct {
|
26 |
Date string `json:"date"`
|
27 |
Price float64 `json:"price"`
|
28 |
} `json:"predictions"`
|
|
|
29 |
} `json:"predictions"`
|
30 |
} `json:"data"`
|
31 |
}
|
|
|
18 |
Data struct {
|
19 |
Currency string `json:"currency"`
|
20 |
Predictions struct {
|
21 |
+
|
22 |
Actuals []struct {
|
23 |
Date string `json:"date"`
|
24 |
Price float64 `json:"price"`
|
25 |
} `json:"actuals"`
|
26 |
+
|
27 |
Predictions []struct {
|
28 |
Date string `json:"date"`
|
29 |
Price float64 `json:"price"`
|
30 |
} `json:"predictions"`
|
31 |
+
|
32 |
} `json:"predictions"`
|
33 |
} `json:"data"`
|
34 |
}
|
proxy/utils.go
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
package proxy
|
2 |
+
|
3 |
+
import "time"
|
4 |
+
|
5 |
+
func GetEndpointByRestService(svc_name string) string {
|
6 |
+
second := time.Now().Second()
|
7 |
+
|
8 |
+
var divisorToString map[int]string
|
9 |
+
switch svc_name {
|
10 |
+
case "crypto":
|
11 |
+
divisorToString = map[int]string{
|
12 |
+
9: "https://qywok-cryptocurrency-prediction.hf.space/crypto",
|
13 |
+
7: "https://qywok-cryptocurrency-prediction.hf.space/crypto",
|
14 |
+
5: "https://qywok-cryptocurrency-prediction.hf.space/crypto",
|
15 |
+
3: "https://qywok-cryptocurrency-prediction.hf.space/crypto",
|
16 |
+
2: "https://qywok-cryptocurrency-prediction.hf.space/crypto",
|
17 |
+
}
|
18 |
+
case "national":
|
19 |
+
divisorToString = map[int]string{
|
20 |
+
9: "https://qywok-cryptocurrency-prediction.hf.space/crypto",
|
21 |
+
7: "https://qywok-cryptocurrency-prediction.hf.space/crypto",
|
22 |
+
5: "https://qywok-cryptocurrency-prediction.hf.space/crypto",
|
23 |
+
3: "https://qywok-cryptocurrency-prediction.hf.space/crypto",
|
24 |
+
2: "https://qywok-cryptocurrency-prediction.hf.space/crypto",
|
25 |
+
}
|
26 |
+
default:
|
27 |
+
divisorToString = map[int]string{
|
28 |
+
9: "https://qywok-cryptocurrency-prediction.hf.space/crypto",
|
29 |
+
7: "https://qywok-cryptocurrency-prediction.hf.space/crypto",
|
30 |
+
5: "https://qywok-cryptocurrency-prediction.hf.space/crypto",
|
31 |
+
3: "https://qywok-cryptocurrency-prediction.hf.space/crypto",
|
32 |
+
2: "https://qywok-cryptocurrency-prediction.hf.space/crypto",
|
33 |
+
}
|
34 |
+
}
|
35 |
+
|
36 |
+
var result string
|
37 |
+
for divisor, str := range divisorToString {
|
38 |
+
if (second % divisor) == 0 {
|
39 |
+
result += str
|
40 |
+
break
|
41 |
+
}
|
42 |
+
}
|
43 |
+
|
44 |
+
return result
|
45 |
+
}
|