I'm new to the Go language (Golang) and I'm writing a web-based application. I'd like to use session variables, like the kind in PHP (variables that are available from one page to the next and unique for a user session). Is there something like that in Go? If not, how would I go about implementing them myself? Or what alternatives methods are there?
相关问题
- Golang mongodb aggregation
- How to flatten out a nested json structure in go
- how to install private repo using glide golang
- Data loss during sending via $_SESSION from one sc
- Chrome not keeping my _SESSION vars when coming fr
相关文章
- Page指令 的EnableSessionState="ReadOnly",怎么在web.confi
- Can I run a single test in a suite?
- How to check if a request was cancelled
- Is it possible to implement an interface with unex
- How exactly do Firebase Analytics handle session d
- How to access value of first index of array in Go
- Symfony2: check whether session exists or not
- Embedded Interface
You probably want to take a look at gorilla. It has session support as documented here.
Other than that or possibly one of the other web toolkits for go you would have to roll your own.
Possible solutions might be:
I'll leave the implementation details of each of those to the reader.
Here's an alternative in case you just want session support without a complete web toolkit.
https://github.com/bpowers/seshcookie
Here's another alternative (disclosure: I'm the author):
https://github.com/icza/session
Quoting from its doc:
This package provides an easy-to-use, extensible and secure session implementation and management. Package documentation can be found and godoc.org:
https://godoc.org/github.com/icza/session
This is "just" an HTTP session implementation and management, you can use it as-is, or with any existing Go web toolkits and frameworks.
Overview
There are 3 key players in the package:
Session
is the (HTTP) session interface. We can use it to store and retrieve constant and variable attributes from it.Store
is a session store interface which is responsible to store sessions and make them retrievable by their IDs at the server side.Manager
is a session manager interface which is responsible to acquire aSession
from an (incoming) HTTP request, and to add aSession
to an HTTP response to let the client know about the session. AManager
has a backingStore
which is responsible to manageSession
values at server side.Players of this package are represented by interfaces, and various implementations are provided for all these players. You are not bound by the provided implementations, feel free to provide your own implementations for any of the players.
Usage
Usage can't be simpler than this. To get the current session associated with the http.Request:
To create a new session (e.g. on a successful login) and add it to an http.ResponseWriter (to let the client know about the session):
Let's see a more advanced session creation: let's provide a constant attribute (for the lifetime of the session) and an initial, variable attribute:
And to access these attributes and change value of
"Count"
:(Of course variable attributes can be added later on too with
Session.SetAttr()
, not just at session creation.)To remove a session (e.g. on logout):
Check out the session demo application which shows all these in action.
Google App Engine support
The package provides support for Google App Engine (GAE) platform.
The documentation doesn't include it (due to the
+build appengine
build constraint), but here it is: gae_memcache_store.goThe implementation stores sessions in the Memcache and also saves sessions to the Datastore as a backup in case data would be removed from the Memcache. This behaviour is optional, Datastore can be disabled completely. You can also choose whether saving to Datastore happens synchronously (in the same goroutine) or asynchronously (in another goroutine), resulting in faster response times.
We can use
NewMemcacheStore()
andNewMemcacheStoreOptions()
functions to create a session Store implementation which stores sessions in GAE's Memcache. Important to note that since accessing the Memcache relies on Appengine Context which is bound to anhttp.Request
, the returned Store can only be used for the lifetime of a request! Note that the Store will automatically "flush" sessions accessed from it when the Store is closed, so it is very important to close the Store at the end of your request; this is usually done by closing the session manager to which you passed the store (preferably with the defer statement).So in each request handling we have to create a new session manager using a new Store, and we can use the session manager to do session-related tasks, something like this:
Expired sessions are not automatically removed from the Datastore. To remove expired sessions, the package provides a
PurgeExpiredSessFromDSFunc()
function which returns anhttp.HandlerFunc
. It is recommended to register the returned handler function to a path which then can be defined as a cron job to be called periodically, e.g. in every 30 minutes or so (your choice). As cron handlers may run up to 10 minutes, the returned handler will stop at 8 minutes to complete safely even if there are more expired, undeleted sessions. It can be registered like this:Check out the GAE session demo application which shows how it can be used. cron.yaml file of the demo shows how a cron job can be defined to purge expired sessions.
Check out the GAE session demo application which shows how to use this in action.