-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
47 lines (43 loc) · 1.49 KB
/
api.go
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
package main
import (
proxy "github.com/TiyaAnlite/FocotServices/client-http-proxy/api"
"github.com/TiyaAnlite/FocotServicesCommon/echox"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"go.opentelemetry.io/otel/attribute"
"k8s.io/klog/v2"
"net/http"
"strings"
)
func setupRoutes(e *echo.Echo) {
assigned := e.Group("")
if echox.JwtEnabled(cfg.EchoConfig) {
jwtConfig := echox.DefaultJwtConfig(cfg.EchoConfig)
assigned.Use(middleware.JWTWithConfig(jwtConfig))
klog.Info("JWT enabled")
}
assigned.POST("/request", requestProxy)
}
func requestProxy(c echo.Context) error {
ctx, trace := cfg.worker.Start(cfg.worker.Ctx, "requestProxy")
defer trace.End()
req, err := echox.CheckInput[ProxyRequest](c)
if err != nil {
trace.RecordError(err)
return echox.NormalErrorResponse(c, http.StatusBadGateway, http.StatusBadRequest, err.Error())
}
if req.Timeout == 0 {
req.Timeout = 10
}
req.Payload.ResponseHeaders = true // Request headers info
_, mqTrace := cfg.worker.Start(ctx, "sendNATSRequest")
defer mqTrace.End()
mqTrace.SetAttributes(attribute.String("node", req.Node))
resp, err := proxy.SendRequest(mq, strings.Join([]string{cfg.ServiceId, req.Node, "request"}, "."), req.Payload, req.Timeout)
if err != nil {
mqTrace.RecordError(err)
klog.Errorf("At send request: %s", err.Error())
return echox.NormalErrorResponse(c, http.StatusBadGateway, http.StatusBadRequest, err.Error())
}
return c.Blob(resp.StatusCode, resp.Header.Get("Content-Type"), resp.Data)
}