Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can you help with this example? How to make it work? #9

Open
hiqsociety opened this issue Feb 14, 2021 · 0 comments
Open

Can you help with this example? How to make it work? #9

hiqsociety opened this issue Feb 14, 2021 · 0 comments

Comments

@hiqsociety
Copy link

hiqsociety commented Feb 14, 2021

Hi, Can you be kind enough to show to work memconn with fasthttp <-> fasthttp and/or net/http <-> fasthttp?


//Backend

package main
  
import (
        "flag"
        "fmt"
//      "log"

        "github.com/valyala/fasthttp"
        "github.com/akutz/memconn"
)

var (
//      addr     = flag.String("addr", ":8080", "TCP address to listen to")
        compress = flag.Bool("compress", false, "Whether to enable transparent response compression")
)

func main() {
        flag.Parse()

        h := requestHandler
        if *compress {
                h = fasthttp.CompressHandler(h)
        }

        lis, err := memconn.Listen("memu", "MYTEST")
        if err != nil {
                fmt.Printf("err = %v\n",err)
        }

        fasthttp.Serve(lis, h)
}

func requestHandler(ctx *fasthttp.RequestCtx) {
        fmt.Fprintf(ctx, "Hello, world!\n\n")

        fmt.Fprintf(ctx, "Request method is %q\n", ctx.Method())
        fmt.Fprintf(ctx, "RequestURI is %q\n", ctx.RequestURI())
        fmt.Fprintf(ctx, "Requested path is %q\n", ctx.Path())
        fmt.Fprintf(ctx, "Host is %q\n", ctx.Host())
        fmt.Fprintf(ctx, "Query string is %q\n", ctx.QueryArgs())
        fmt.Fprintf(ctx, "User-Agent is %q\n", ctx.UserAgent())
        fmt.Fprintf(ctx, "Connection has been established at %s\n", ctx.ConnTime())
        fmt.Fprintf(ctx, "Request has been started at %s\n", ctx.Time())
        fmt.Fprintf(ctx, "Serial request number for the current connection is %d\n", ctx.ConnRequestNum())
        fmt.Fprintf(ctx, "Your ip is %q\n\n", ctx.RemoteIP())

        fmt.Fprintf(ctx, "Raw request is:\n---CUT---\n%s\n---CUT---", &ctx.Request)

        ctx.SetContentType("text/plain; charset=utf8")

        // Set arbitrary headers
        ctx.Response.Header.Set("X-My-Header", "my-header-value")

        // Set cookies
        var c fasthttp.Cookie
        c.SetKey("cookie-name")
        c.SetValue("cookie-value")
        ctx.Response.Header.SetCookie(&c)
}



////////////////////

//Server


package main

import (
        "github.com/valyala/fasthttp"
        "github.com/akutz/memconn"
        "log"
)

/*
var proxyClient = &fasthttp.HostClient{
        Addr: "upstream.host:port",
        // set other options here if required - most notably timeouts.
}
*/

var (


        proxyClient = &fasthttp.HostClient{
                Addr: "/tmp/http.socket", //?????????
                Dial: func(addr string) (net.Conn, error) {
                        return net.Dial("unix", addr)
                        //return memconn.Dial("memu", "MYTEST")
                },
                //      }
                //      proxyClient = &fasthttp.HostClient{
                //              Addr: "127.0.0.1:87",
                //              Addr: xServerClient,
                //              MaxConns: 16777216,
                MaxIdleConnDuration: 30 * time.Second,
                ReadBufferSize:  64 * 1024,
                WriteBufferSize: 64 * 1024,
                ReadTimeout: 30 * time.Second,
                WriteTimeout: 30 * time.Second,
        }


)

func reverseProxyHandler(ctx *fasthttp.RequestCtx) {
        req := &ctx.Request
        resp := &ctx.Response
        prepareRequest(req)
        if err := proxyClient.Do(req, resp); err != nil {
                ctx.Logger().Printf("error when proxying the request: %s", err)
        }
        postprocessResponse(resp)
}

func prepareRequest(req *fasthttp.Request) {
        // do not proxy "Connection" header.
        req.Header.Del("Connection")
        // strip other unneeded headers.

        // alter other request params before sending them to upstream host
}

func postprocessResponse(resp *fasthttp.Response) {
        // do not proxy "Connection" header
        resp.Header.Del("Connection")

        // strip other unneeded headers

        // alter other response data if needed
}

func main() {

        if err := fasthttp.ListenAndServe(":80", reverseProxyHandler); err != nil {
                log.Fatalf("error in fasthttp server: %s", err)
        }
} 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant