yeqown / fasthttp-reverse-proxy

reverse http / websocket proxy based on fasthttp
MIT License
213 stars 56 forks source link

Conflict with fasthttp/router #34

Closed sdfwds4 closed 2 years ago

sdfwds4 commented 2 years ago

there is a conflict with fasthttp/router code modified from https://github.com/yeqown/fasthttp-reverse-proxy/blob/master/examples/fasthttp-reverse-proxy/proxy.go

package main

import (
    "strings"
    "time"

    "github.com/yeqown/log"
    "github.com/fasthttp/router"
    "github.com/valyala/fasthttp"
    proxy "github.com/yeqown/fasthttp-reverse-proxy/v2"
)

var (
    proxyServer  = proxy.NewReverseProxy("localhost:8080", proxy.WithTimeout(5*time.Second))
    proxyServer2 = proxy.NewReverseProxy("api-js.mixpanel.com")
    proxyServer3 = proxy.NewReverseProxy("baidu.com")
)

func Index(ctx *fasthttp.RequestCtx) {
    ctx.WriteString("Welcome!")
}

func Hello(ctx *fasthttp.RequestCtx) {
    fmt.Fprintf(ctx, "Hello, %s!\n", ctx.UserValue("name"))
}

// ProxyHandler ... fasthttp.RequestHandler func
func ProxyHandler(ctx *fasthttp.RequestCtx) {
    requestURI := string(ctx.RequestURI())
    log.Info("requestURI=", requestURI)

    if strings.HasPrefix(requestURI, "/local") {
        // "/local" path proxy to localhost
        arr := strings.Split(requestURI, "?")
        if len(arr) > 1 {
            arr = append([]string{"/foo"}, arr[1:]...)
            requestURI = strings.Join(arr, "?")
        }

        ctx.Request.SetRequestURI(requestURI)
        proxyServer.ServeHTTP(ctx)
    } else if strings.HasPrefix(requestURI, "/baidu") {
        proxyServer3.ServeHTTP(ctx)
    } else {
        proxyServer2.ServeHTTP(ctx)
    }
}

func main() {
    r := router.New()
    r.GET("/", Index)
    r.GET("/hello/{name}", Hello)

    if err := fasthttp.ListenAndServe(":8081", ProxyHandler); err != nil {
        log.Fatal(err)
    }
}

After added fasthttp/router code, go mod tidy got a import error. if removed fasthttp/router code, go mod tidy succeeded.

$ go version
go version go1.18 windows/amd64

$ go mod init fasthttp-router
go: creating new go.mod: module fasthttp-router
go: to add module requirements and sums:
        go mod tidy

$ go mod tidy
go: finding module for package github.com/yeqown/log
go: finding module for package github.com/fasthttp/router
go: finding module for package github.com/valyala/fasthttp
go: finding module for package github.com/yeqown/fasthttp-reverse-proxy/v2
go: found github.com/fasthttp/router in github.com/fasthttp/router v1.4.11
go: found github.com/valyala/fasthttp in github.com/valyala/fasthttp v1.39.0
go: found github.com/yeqown/fasthttp-reverse-proxy/v2 in github.com/yeqown/fasthttp-reverse-proxy/v2 v2.2.2
go: found github.com/yeqown/log in github.com/yeqown/log v1.1.1
go: finding module for package github.com/savsgio/gotils
fasthttp-router imports
        github.com/yeqown/fasthttp-reverse-proxy/v2 imports
        github.com/fasthttp/websocket imports
        github.com/savsgio/gotils: module github.com/savsgio/gotils@latest found (v0.0.0-20220530130905-52f3993e8d6d), but does not contain package github.com/savsgio/gotils
yeqown commented 2 years ago

As you see in the output this is a problem about go.mod not this library, and what's more is that this LIB does not use github.com/savsgio/gotils directly, I can hardly change this.

sdfwds4 commented 2 years ago

As you see in the output this is a problem about go.mod not this library, and what's more is that this LIB does not use github.com/savsgio/gotils directly, I can hardly change this.

ok, I changed to other lib, not to use fasthttp, thank you all the same.