From dd08312c0f493a1b8826d9b06bc3c8b8811c5381 Mon Sep 17 00:00:00 2001 From: Adam Hanna Date: Mon, 1 May 2017 22:14:35 -0700 Subject: [PATCH] Added travis ci, make file and license. Updated readme --- .gitignore | 4 ++ .travis.yml | 20 ++++++++ LICENSE | 21 ++++++++ Makefile | 16 ++++++ README.md | 116 +++++++++++++++++++++++++++++++++++++++++-- store/service.go | 2 +- transport/service.go | 4 +- 7 files changed, 176 insertions(+), 7 deletions(-) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 Makefile diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8790525 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.DS_Store + +coverage.out +coverage-all.out \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..24f4c50 --- /dev/null +++ b/.travis.yml @@ -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 \ No newline at end of file diff --git a/LICENSE b/LICENSE index e69de29..fdefb07 100644 --- a/LICENSE +++ b/LICENSE @@ -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. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..951c4d7 --- /dev/null +++ b/Makefile @@ -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;) \ No newline at end of file diff --git a/README.md b/README.md index 271dfd5..2dfb960 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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: @@ -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. ~~~ \ No newline at end of file diff --git a/store/service.go b/store/service.go index 2522bc6..489c7ff 100644 --- a/store/service.go +++ b/store/service.go @@ -15,7 +15,7 @@ const ( // DefaultMaxIdleConnections sets the maximum number of idle connections on the redis server DefaultMaxIdleConnections = 3 // DefaultMaxActiveConnections sets the maximum number of active connections on the redis server - DefaultMaxActiveConnections = 10 + // DefaultMaxActiveConnections = 10 // changing this to 0, the uninitialized val, for now // DefaultIdleTimeoutDuration sets the maximum duration to wait before closing an idle connection on the redis server DefaultIdleTimeoutDuration = 10 * time.Second ) diff --git a/transport/service.go b/transport/service.go index 25d3757..e7041cc 100644 --- a/transport/service.go +++ b/transport/service.go @@ -15,9 +15,9 @@ const ( // DefaultCookiePath is the default cookie path DefaultCookiePath = "/" // DefaultHTTPOnlyCookie is the default HTTPOnly option of the cookie - DefaultHTTPOnlyCookie = true + // DefaultHTTPOnlyCookie = true // changing this to false, the uninitialized val, for now // DefaultSecureCookie is the default Secure option of the cookie - DefaultSecureCookie = true + // DefaultSecureCookie = true // changing this to false, the uninitialized val, for now ) // Service writes sessions on responseWriters and reads sessions from requests