|
@@ -0,0 +1,50 @@
|
|
|
+package websubsvc
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "github.com/gin-gonic/gin"
|
|
|
+ "github.com/yhat/wsutil"
|
|
|
+ "net/http/httputil"
|
|
|
+ "net/url"
|
|
|
+)
|
|
|
+
|
|
|
+type FrontendProxy struct {
|
|
|
+ debugRedirectNotFoundURLhttp *url.URL
|
|
|
+ debugRedirectNotFoundURLws *url.URL
|
|
|
+}
|
|
|
+
|
|
|
+func NewFrontendProxy(proxyURL string) (*FrontendProxy, error) {
|
|
|
+ rurl, err := url.Parse(proxyURL)
|
|
|
+ if err != nil {
|
|
|
+ return nil, fmt.Errorf("invalid proxy url: %w", err)
|
|
|
+ }
|
|
|
+ rurl4ws, err := url.Parse(proxyURL)
|
|
|
+ if err != nil {
|
|
|
+ return nil, fmt.Errorf("invalid proxy url: %w", err)
|
|
|
+ }
|
|
|
+ if rurl4ws.Scheme == "https" {
|
|
|
+ rurl4ws.Scheme = "wss"
|
|
|
+ } else {
|
|
|
+ rurl4ws.Scheme = "ws"
|
|
|
+ }
|
|
|
+ return &FrontendProxy{
|
|
|
+ debugRedirectNotFoundURLhttp: rurl,
|
|
|
+ debugRedirectNotFoundURLws: rurl4ws,
|
|
|
+ }, nil
|
|
|
+}
|
|
|
+
|
|
|
+func (p *FrontendProxy) RedirectNotFoundHandler(context *gin.Context) {
|
|
|
+ if context.IsWebsocket() {
|
|
|
+ if p.debugRedirectNotFoundURLws == nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ proxy := wsutil.NewSingleHostReverseProxy(p.debugRedirectNotFoundURLws)
|
|
|
+ proxy.ServeHTTP(context.Writer, context.Request)
|
|
|
+ } else {
|
|
|
+ if p.debugRedirectNotFoundURLhttp == nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ proxy := httputil.NewSingleHostReverseProxy(p.debugRedirectNotFoundURLhttp)
|
|
|
+ proxy.ServeHTTP(context.Writer, context.Request)
|
|
|
+ }
|
|
|
+}
|