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

r.group not working #108

Closed
RoelVanNyen opened this issue Aug 31, 2014 · 6 comments
Closed

r.group not working #108

RoelVanNyen opened this issue Aug 31, 2014 · 6 comments

Comments

@RoelVanNyen
Copy link

I made a quick test with custom middleware groups:

func TestHandle() gin.HandlerFunc {
    return func(c *gin.Context) {
        fmt.Printf("TEST: \n" )

        c.Next()

        fmt.Printf("TEST2: \n" )
    }
}

 r.Group("/", TestHandle()) {
        r.GET("/test", func(c *gin.Context) {
            fmt.Printf("test")
            c.String(200, "ok")
        })
}

TEST and TEST2 never gets printed.

I do get a warning:
[GIN] WARNING. Headers were already written!

Any ideas ?

@manucorporat
Copy link
Contributor

You are referencing the same "group".

// notify the "test" variable
test := r.Group("/", TestHandle())
test.GET("/test", func(c *gin.Context) {
    fmt.Printf("test")
    c.String(200, "ok")
})

I, personally, like to use the brackets because it gives a sense of group, and makes the routes easier to read.

// notify the "test" variable
test := r.Group("/", TestHandle())
{
    test.GET("/test", func(c *gin.Context) {
        fmt.Printf("test")
        c.String(200, "ok")
    })
}

It's exactly the same.

@manucorporat
Copy link
Contributor

anyway, I am more worried about "[GIN] WARNING. Headers were already written!"
please can you share all the code? and tell me if you still get that warning?

@RoelVanNyen
Copy link
Author

Ah ok, did not know I had to use thegroupname.GET :-)
This fixes the problem, now my code works and the warning is gone.

Thanks!

@manucorporat
Copy link
Contributor

anyway, could you post the code? that warning should never appear

@RoelVanNyen
Copy link
Author

Its a lot of code so i`ll keep it simple:

func MongoAuth() gin.HandlerFunc {
    return func(c *gin.Context) {
        fmt.Printf("TEST: \n" )
        cookie := c.Request.Header.Get("Cookie")

        fmt.Printf("cookie: %v\n", cookie)

        c.Next()

        fmt.Printf("TEST2: \n" )
    }
}



func main() {
    db := database.GDatabase{}
    db.Initialize()
    gs := Server{}
    gs.db = &db
    ....
    // no more rabbit for the moment
    //gs.initialize_rabbit()

    r := gin.Default()


    r.Group("/", MongoAuth())
    {
        r.GET("/test", func(c *gin.Context) {
            fmt.Printf("test")
            c.String(200, "ok")
        })


        ///////
        // Bitbucket
        r.GET("/auth/login/bitbucket", func(c *gin.Context) {
            ... logic here ...

            c.Redirect(301, "http://localhost:8080/wert")

        })

        r.GET("/auth/login/bitbucket/token", func(c *gin.Context) {
            ... logic here ...
            fmt.Printf("url: %s\n", url)
            if strings.EqualFold("", url) {
                c.String(503, "Internal error")     
            }
            c.Redirect(301, url)
        })

    }
}

@RoelVanNyen RoelVanNyen reopened this Sep 1, 2014
@javierprovecho
Copy link
Member

@RoelVanNyen sorry for the delay

Pretty sure your warning about headers is related because you write to the response before trying to redirect.

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

3 participants