1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- 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)
- }
- }
|