Git Product home page Git Product logo

Comments (8)

manucorporat avatar manucorporat commented on April 27, 2024

Gin workflow spins around gin.Context. It represents the current state and information of a HTTP request. We do not really need to integrate session management in Gin, you can use github.com/gorilla/sessions without any problem. Just set the your session store to a global variable in your Go! package and use it.

Anyway I will do research about that, it could be interesting to provide a built-in solution.

If you want to use Gin with Gorilla, use this:

import (
    "net/http"
    "github.com/gorilla/sessions"
    "github.com/gin-gonic/gin"
)

var store = sessions.NewCookieStore([]byte("something-very-secret"))

func MyHandler(c *gin.Context) {
    // Get a session. We're ignoring the error resulted from decoding an
    // existing session: Get() always returns a session, even if empty.
    session, _ := store.Get(c.Req, "session-name")
    // Set some session values.
    session.Values["foo"] = "bar"
    session.Values[42] = 43
    // Save it.
    session.Save(c.Req, c.Writer)
}

from gin.

ekanna avatar ekanna commented on April 27, 2024

Thank you. It will be great if it is part of the framework. Nice work!

from gin.

allochi avatar allochi commented on April 27, 2024

@manucorporat maybe leave this to gin-contrib, gin is a lightweight framework, I don't see why gorrilla sessions won't do the job? keeping gin open to use any library out there is a big plus. Same goes for BasicAuth

This is just me being enthusiastic about gin 😄

from gin.

TimothyYe avatar TimothyYe commented on April 27, 2024

I do think it is necessary to create a repository called "gin-contrib" or other names, developers then can create and submit middleware for gin.

from gin.

austinheap avatar austinheap commented on April 27, 2024

+1

from gin.

manucorporat avatar manucorporat commented on April 27, 2024

I will create a gin-contrib tomorrow! :)

from gin.

allochi avatar allochi commented on April 27, 2024

Hey, cool, could you please decide on #22 also before that, since this could go into the contrib packages when people use context in them.

from gin.

troyk avatar troyk commented on April 27, 2024

@manucorporat the gorilla site (http://www.gorillatoolkit.org/pkg/sessions) mentions

Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory! An easy way to do this is to wrap the top-level mux when calling http.ListenAndServe:

Gorilla stores the data using the request as a map key (https://github.com/gorilla/context/blob/master/context.go#L91-L105) using the gorilla context pkg

Here is the Gin implementation (https://github.com/gorilla/context/blob/master/context.go#L91-L105)

Gorilla context is so light weight and it's session pkg seems to be the best available, would you be interested in a pull request to use gorilla context under to hood to power context data. It appears that the Gin API would not change and then Gin gets compatibility with any middleware depending on gorilla context

from gin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.