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

middleware proxy #485

Closed
gambol99 opened this issue Nov 16, 2015 · 0 comments
Closed

middleware proxy #485

gambol99 opened this issue Nov 16, 2015 · 0 comments

Comments

@gambol99
Copy link

I'm having a issue with some middleware ... i'm not sure if it's a bug, or more likely the way i'm using it ... The service consumes the json payload, perform some check, aborts the context if required otherwise the proxy middleware forwards the require on. The proxy works fine for the urls not hitting the handler, but when it hits the handler, even with the context not abort the proxy middleware handler is never called, it just returns a 200 but is never forwarded .... Any help would be much appreciated!

Gin Setup

// step: create the gin router
router := gin.Default()

// step: handle operations related to replication controllers]
{
    replicationEndpoint := "/api/v1/namespaces/:namespace/replicationcontrollers"
    router.POST(replicationEndpoint, service.handleReplicationController)
}
router.Use(service.proxyHandler())

Handler

func (r *Service) handleReplicationController(cx *gin.Context) {

     READ PAYLOAD, PERFORM SOME CHECK AND SET THE BODY TO ORIGINAL CONTENT 
     // check the content is still there
    dump, err := httputil.DumpRequest(cx.Request, true)
    if err == nil {
        glog.V(10).Infof("%s", dump)
    }     

    glog.V(10).Infof("continuing the chain, is aborted: %t", cx.IsAborted())
}

Decode Input

// decode the payload and set the request body back to the original content
func (r *Service) decodeInput(req *http.Request, data interface{}) (string, error) {
    // step: read in the content payload
    content, err := ioutil.ReadAll(req.Body)
    if err != nil {
        glog.Errorf("unable to read in the content, error: %s", err)
        return "", err
    }
    defer func() {
        // we need to set the content back
        req.Body = ioutil.NopCloser(bytes.NewReader(content))
    }()

    rdr := strings.NewReader(string(content))

    // step: decode the json
    err = json.NewDecoder(rdr).Decode(data)
    if err != nil {
        glog.Errorf("unable to decode the request body, error: %s", err)
        return "", err
    }

    return string(content), nil
}

Proxy Middleware

func (r *Service) proxyHandler() gin.HandlerFunc {
    return func(cx *gin.Context) {
        fmt.Println("PROXYING ON")

        // step: does the connection need upgrading?
        if cx.Request.Header.Get("Upgrade") != "" {
            if err := r.hijackRequest(cx); err != nil {
                glog.Errorf("unable to upgrade the connetcion, %s", err)
            }
            return
        }

        // step: pass into the reverse proxy
        r.proxy.ServeHTTP(cx.Writer, cx.Request)
    }
}

Output

PROXYING ON
[GIN] 2015/11/16 - 12:07:10 | 200 |  130.368449ms | 127.0.0.1 |   GET     /api
I1116 12:07:10.975621   20514 handlers.go:57] continuing the chain, is aborted: false
[GIN] 2015/11/16 - 12:07:10 | 200 |       19.52µs | 127.0.0.1:40498 |   POST    /api/v1/namespaces/default/replicationcontrollers
@gambol99 gambol99 changed the title middle proxy middleware proxy Nov 16, 2015
@gambol99 gambol99 closed this as completed May 9, 2016
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