• Stars
    star
    111
  • Rank 314,510 (Top 7 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created almost 9 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Go session management for web servers (including support for Google App Engine - GAE).

Session

Build Status GoDoc Go Report Card codecov

The Go standard library includes a nice http server, but unfortunately it lacks a very basic and important feature: HTTP session management.

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 a Session from an (incoming) HTTP request, and to add a Session to an HTTP response to let the client know about the session. A Manager has a backing Store which is responsible to manage Session 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:

sess := session.Get(r)
if sess == nil {
    // No session (yet)
} else {
    // We have a session, use it
}

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):

sess := session.NewSession()
session.Add(sess, w)

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:

sess := session.NewSessionOptions(&session.SessOptions{
    CAttrs: map[string]interface{}{"UserName": userName},
    Attrs:  map[string]interface{}{"Count": 1},
})

And to access these attributes and change value of "Count":

userName := sess.CAttr("UserName")
count := sess.Attr("Count").(int) // Type assertion, you might wanna check if it succeeds
sess.SetAttr("Count", count+1)    // Increment 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):

session.Remove(sess, w)

Check out the session demo application which shows all these in action.

Google App Engine support

The package https://github.com/icza/gaesession provides support for Google App Engine (GAE) platform.

The gaesession implementation stores sessions in the Memcache and also saves sessions in 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.

For details and examples, please visit https://github.com/icza/gaesession.

More Repositories

1

gowut

Go Web UI Toolkit - Public Releases and Development
Go
289
star
2

bitio

Optimized bit-level Reader and Writer for Go.
Go
220
star
3

scelight

The source code of the Scelight project with all its modules.
Java
117
star
4

gox

Minimalistic extension to Go. It means to be a complement to the standard library.
Go
109
star
5

mjpeg

MJPEG video writer implementation in Go.
Go
100
star
6

screp

StarCraft - Brood War replay parser
Go
73
star
7

minquery

MongoDB / mgo query that supports efficient pagination (cursors to continue listing documents where we left off).
Go
60
star
8

s2prot

Decoder/parser of Blizzard's StarCraft II replay file format (*.SC2Replay)
Go
49
star
9

backscanner

A scanner similar to bufio.Scanner, but it reads and returns lines in reverse order, starting at a given position and going backward.
Go
46
star
10

sc2gears

The COMPLETE (!) source code of the Sc2gears universe (Sc2gears app + Sc2gears Database + web-based parsing engine - bundled in an Eclipse project).
Java
33
star
11

mpq

Decoder/parser of Blizzard's MPQ archive file format
Go
32
star
12

huffman

Huffman coding implementation in Go (Huffman tree, Symbol table, Huffman Reader + Writer).
Go
27
star
13

golab

This is the reincarnation of my gophergala/golab: a 2D labyrinth game.
Go
26
star
14

gog

General extensions to the Go language, requiring generics
Go
20
star
15

kvcache

Simple, optimized, embedded, persistent (file-based) key-value cache
Go
13
star
16

shutdown

Tiny Go package to help controlling app shutdown and graceful termination of goroutines.
Go
8
star
17

bwhf

StarCraft BroodWar Hacker Finder, anti-hack, replay analyzer-organizer and utility tool
Java
7
star
18

abcsort

Go string sorting library that uses a custom, user-defined alphabet
Go
6
star
19

balls-sdl

Bouncing balls demo (using SDL2)
Go
5
star
20

srtgears

Subtitle engine for reading, manipulating / transforming and saving subtitle files.
Go
5
star
21

bombermen

Bombermen is a computer arcade game based on the classic Dynablaster and Atomic bomberman specified to be highly configurable and playable.
Java
5
star
22

mighty

Lightweight extension to Go's testing package.
Go
4
star
23

recursion

Recursive algorithms.
Java
3
star
24

v

V is a minimalist 3D labyrinth applet demo being less than 4 KB, 3D graphics and rendering coded manually
Java
3
star
25

gowut.dev

Go Web UI Toolkit - Development
Go
2
star
26

abdracommander

A 2-pane file manager written in Java
Java
1
star
27

java-fishing

Fishing is a 2D skill game.
Java
1
star
28

authn

Passwordless, email based authentication with MongoDB store.
Go
1
star
29

balls-wde

Bouncing balls demo (using go.wde)
Go
1
star
30

gaesession

Google App Engine (GAE) support for github.com/icza/session
Go
1
star
31

4kraft

4KB StarCraft like Java applet game
Java
1
star