Skip to content

Commit

Permalink
Merge branch 'feature/travisCI' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-hanna committed May 2, 2017
2 parents b76bf67 + dd08312 commit 2174421
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store

coverage.out
coverage-all.out
20 changes: 20 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
language: go
go:
- 1.7.x
# branches:
# only:
# - feature/travisCI
env:
global:
secure: hEiVIu1nvTloePPm72L6867b8h11/dxydcbpqoujUE89n5MiFh0zo/Du05N0eYjlmjANq3uvVGgHnWNSX4l8yqZf1QcOE2yvs9e06vqMZbK6iryT2Mm8KuLFgpHulQinz/SSJo6Kw4o8UXhJT9r80YqpgOT6EZX/2YRetz/kYQqI+qe6GqYSDZVATYgG54E0puLR0hBNwWuGfjWCmiGXFuIJl8mEqnx9rd4B8VPARMWToVvTo/IC3O1zpNPMV9zDcC3CG+/quQJHFELpCB1c7SHEYClKeZrVnrqcoGgs+3z3P0wPfXo+fTragRhtLf5Ynbru+Af1p3vzfcOjmVys+k6+b1cqS8GJ73k/+Y2L7QrrHrOZRJbA9ATYobXSfPFbCCw+iRRHnwZDHnZlMt8mQkn+LA0SH+YRNiyLRhgqPOKr00UwzMoi4ckDd6PjhT5SuWaoiADDFuxZ5Ld+VbWoNoLHeVtt+Kn+nR9PhOJ0taWnOKo6VOVg2oainR5vERuuP/JOG+sH1lN8LTjxHERb9Zx0zZowDczvLi0mD7ehoOwBp3fpuJ5FOSZyI8+jB0dZTzaOTBQWve71SQzGt7z9eXAzA8RNTkBt/MTCf9k0qqVD6n237nkMZMaluOeQ8ez6O8v1H2nZ9R/BrWcLhFwjLg+8294MCNYjcKmzhg6S5cY=
services:
- redis-server
install:
- go get golang.org/x/tools/cmd/cover
- go get github.com/mattn/goveralls
- go get github.com/garyburd/redigo/redis
- go get github.com/pborman/uuid
script:
- make test-cover-html
- $(go env GOPATH | awk 'BEGIN{FS=":"} {print $1}')/bin/goveralls -coverprofile=coverage-all.out
-service=travis-ci -repotoken=$COVERALLS_TOKEN
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Adam Hanna

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.PHONY: test-cover-html
PACKAGES = $(shell find ./ -type d -not -path '*/\.*' | grep -v vendor)

fmt:
bash -c 'go list ./... | grep -v vendor | xargs -n1 go fmt'

test:
bash -c 'go list ./... | grep -v vendor | xargs -n1 go test -timeout=30s -tags="unit integration e2e"'

# thanks!
# https://gist.github.com/skarllot/13ebe8220822bc19494c8b076aabe9fc
test-cover-html:
echo "mode: count" > coverage-all.out
$(foreach pkg,$(PACKAGES),\
go test -tags="unit integration e2e" -coverprofile=coverage.out -covermode=count $(pkg);\
tail -n +2 coverage.out >> coverage-all.out;)
116 changes: 112 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
# Go Sessions
[![Build Status](https://travis-ci.org/adam-hanna/sessions.svg)](https://travis-ci.org/adam-hanna/sessions) [![Coverage Status](https://coveralls.io/repos/github/adam-hanna/sessions/badge.svg)](https://coveralls.io/github/adam-hanna/sessions) [![Go Report Card](https://goreportcard.com/badge/github.com/adam-hanna/sessions)](https://goreportcard.com/report/github.com/adam-hanna/sessions) [![GoDoc](https://godoc.org/github.com/adam-hanna/sessions?status.svg)](https://godoc.org/github.com/adam-hanna/sessions)

# Sessions
A dead simple, highly customizable sessions service for go http servers

## Quickstart
**README Contents:**

1. [Quickstart](https://github.com/adam-hanna/sessions#quickstart)
2. [Performance](https://github.com/adam-hanna/sessions#performance)
3. [API](https://github.com/adam-hanna/sessions#api)
4. [Test Coverage](https://github.com/adam-hanna/sessions#test-coverage)
5. [Example](https://github.com/adam-hanna/sessions#example)
6. [License](https://github.com/adam-hanna/sessions#license)

## Quickstart
~~~go
package main

Expand Down Expand Up @@ -52,12 +62,85 @@ func main() {
}
~~~

## Testing
Tests are broken down into three categories: unit, integration and e2e. Integration and e2e tests require a connection to a redis server. The connection address can be set in the `REDIS_URL` environment variable. The default is ":6379"
## Performance
Benchmarks require a redis-server running. Set the `REDIS_URL` environment variable, otherwise the benchmarks look for ":6379".

YMMV
~~~ bash
$ (cd benchmark && go test -bench=.)

setting up benchmark tests
BenchmarkBaseServer-2 20000 72479 ns/op
BenchmarkValidSession-2 10000 151650 ns/op
PASS
shutting down benchmark tests
ok github.com/adam-hanna/sessions/benchmark 3.727s
~~~

## API
### [user.Session](https://godoc.org/github.com/adam-hanna/sessions#Session)
~~~go
type Session struct {
ID string
UserID string
ExpiresAt time.Time
JSON string
}
~~~
Session is the struct that is used to store session data. The JSON field allows you to set any custom information you'd like. See the [example](https://github.com/adam-hanna/sessions#example)

### [IssueUserSession](https://godoc.org/github.com/adam-hanna/sessions#IssueUserSession)
~~~ go
func (s *Service) IssueUserSession(userID string, json string, w http.ResponseWriter) (*user.Session, *sessionerrs.Custom)
~~~
IssueUserSession grants a new user session, writes that session info to the store and writes the session on the http.ResponseWriter.

This method should be called when a user logs in, for example.

### [ClearUserSession](https://godoc.org/github.com/adam-hanna/sessions#ClearUserSession)
~~~go
func (s *Service) ClearUserSession(userSession *user.Session, w http.ResponseWriter) *sessionerrs.Custom
~~~
ClearUserSession is used to remove the user session from the store and clear the cookies on the ResponseWriter.

This method should be called when a user logs out, for example.

### [GetUserSession](https://godoc.org/github.com/adam-hanna/sessions#GetUserSession)
~~~go
func (s *Service) GetUserSession(r *http.Request) (*user.Session, *sessionerrs.Custom)
~~~
GetUserSession returns a user session from a request. This method only returns valid sessions. Therefore, sessions that have expired, or that fail signature verification will return a custom session error with code 401.

### [ExtendUserSession](https://godoc.org/github.com/adam-hanna/sessions#ExtendUserSession)
~~~go
func (s *Service) ExtendUserSession(userSession *user.Session, r *http.Request, w http.ResponseWriter) *sessionerrs.Custom
~~~
ExtendUserSession extends the ExpiresAt of a session by the Options.ExpirationDuration

Note that this function must be called, manually! Extension of user session expiry's does not happen automatically!

## Testing Coverage
~~~bash
ok github.com/adam-hanna/sessions 9.012s coverage: 94.1% of statements
? github.com/adam-hanna/sessions/sessionerrs [no test files]
ok github.com/adam-hanna/sessions/auth 0.003s coverage: 100.0% of statements
ok github.com/adam-hanna/sessions/store 0.006s coverage: 85.4% of statements
ok github.com/adam-hanna/sessions/benchmark 0.004s coverage: 0.0% of statements [no tests to run]
ok github.com/adam-hanna/sessions/transport 0.004s coverage: 95.2% of statements
ok github.com/adam-hanna/sessions/user 0.003s coverage: 100.0% of statements
~~~

Tests are broken down into three categories: unit, integration and e2e. Integration and e2e tests require a connection to a redis server. The connection address can be set in the `REDIS_URL` environment variable. The default is ":6379".

To run all tests, simply:
~~~
$ go test -tags="unit integration e2e" ./...

// or
$ make test

// or
$ make test-cover-html && go tool cover -html=coverage-all.out
~~~

To run only tests from one of the categories:
Expand Down Expand Up @@ -271,4 +354,29 @@ func generateKey() (string, error) {
}
return base64.URLEncoding.EncodeToString(b), nil
}
~~~

## License
~~~
The MIT License (MIT)
Copyright (c) 2017 Adam Hanna
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
~~~

0 comments on commit 2174421

Please sign in to comment.