Skip to content

Commit 6ad3179

Browse files
committed
update config
1 parent 652bfb1 commit 6ad3179

File tree

3 files changed

+47
-25
lines changed

3 files changed

+47
-25
lines changed

.travis.yml

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ go_import_path: github.com/go-session/gin-session
44
go:
55
- 1.9
66
before_install:
7-
- go get github.com/mattn/goveralls
7+
- go get -t -v ./...
8+
89
script:
9-
- $HOME/gopath/bin/goveralls -service=travis-ci
10+
- go test -race -coverprofile=coverage.txt -covermode=atomic
11+
12+
after_success:
13+
- bash <(curl -s https://codecov.io/bash)

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Session middleware for [Gin](https://github.com/gin-gonic/gin)
22

3-
[![Build][Build-Status-Image]][Build-Status-Url] [![Coverage][Coverage-Image]][Coverage-Url] [![ReportCard][reportcard-image]][reportcard-url] [![GoDoc][godoc-image]][godoc-url] [![License][license-image]][license-url]
3+
[![Build][Build-Status-Image]][Build-Status-Url] [![Codecov][codecov-image]][codecov-url] [![Coverage][Coverage-Image]][Coverage-Url] [![ReportCard][reportcard-image]][reportcard-url] [![GoDoc][godoc-image]][godoc-url] [![License][license-image]][license-url]
44

55
## Quick Start
66

@@ -77,8 +77,8 @@ $ ./server
7777

7878
[Build-Status-Url]: https://travis-ci.org/go-session/gin-session
7979
[Build-Status-Image]: https://travis-ci.org/go-session/gin-session.svg?branch=master
80-
[Coverage-Url]: https://coveralls.io/github/go-session/gin-session?branch=master
81-
[Coverage-Image]: https://coveralls.io/repos/github/go-session/gin-session/badge.svg?branch=master
80+
[codecov-url]: https://codecov.io/gh/go-session/gin-session
81+
[codecov-image]: https://codecov.io/gh/go-session/gin-session/branch/master/graph/badge.svg
8282
[reportcard-url]: https://goreportcard.com/report/github.com/go-session/gin-session
8383
[reportcard-image]: https://goreportcard.com/badge/github.com/go-session/gin-session
8484
[godoc-url]: https://godoc.org/github.com/go-session/gin-session

session.go

+38-20
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,31 @@ import (
88
"gopkg.in/session.v2"
99
)
1010

11+
type (
12+
// ErrorHandleFunc error handling function
13+
ErrorHandleFunc func(*gin.Context, error)
14+
// Config defines the config for Session middleware
15+
Config struct {
16+
// error handling when starting the session
17+
ErrorHandleFunc ErrorHandleFunc
18+
// keys stored in the context
19+
StoreKey string
20+
}
21+
)
22+
1123
var (
12-
// DefaultKey Keys stored in the context
13-
DefaultKey = "github.com/go-session/gin-session"
1424
once sync.Once
1525
internalManager *session.Manager
16-
internalError ErrorHandleFunc
17-
)
26+
storeKey string
1827

19-
func init() {
20-
internalError = func(ctx *gin.Context, err error) {
21-
ctx.AbortWithError(500, err)
28+
// DefaultConfig is the default Recover middleware config.
29+
DefaultConfig = Config{
30+
ErrorHandleFunc: func(ctx *gin.Context, err error) {
31+
ctx.AbortWithError(500, err)
32+
},
33+
StoreKey: "github.com/go-session/gin-session",
2234
}
23-
}
24-
25-
// ErrorHandleFunc error handling function
26-
type ErrorHandleFunc func(ctx *gin.Context, err error)
27-
28-
// SetErrorHandler Set error handling
29-
func SetErrorHandler(handler ErrorHandleFunc) {
30-
internalError = handler
31-
}
35+
)
3236

3337
func manager(opt ...session.Option) *session.Manager {
3438
once.Do(func() {
@@ -37,22 +41,36 @@ func manager(opt ...session.Option) *session.Manager {
3741
return internalManager
3842
}
3943

40-
// New Create a session middleware
44+
// New create a session middleware
4145
func New(opt ...session.Option) gin.HandlerFunc {
46+
return NewWithConfig(DefaultConfig, opt...)
47+
}
48+
49+
// NewWithConfig create a session middleware
50+
func NewWithConfig(config Config, opt ...session.Option) gin.HandlerFunc {
51+
if config.ErrorHandleFunc == nil {
52+
config.ErrorHandleFunc = DefaultConfig.ErrorHandleFunc
53+
}
54+
55+
storeKey = config.StoreKey
56+
if storeKey == "" {
57+
storeKey = DefaultConfig.StoreKey
58+
}
59+
4260
return func(ctx *gin.Context) {
4361
store, err := manager(opt...).Start(context.Background(), ctx.Writer, ctx.Request)
4462
if err != nil {
45-
internalError(ctx, err)
63+
config.ErrorHandleFunc(ctx, err)
4664
return
4765
}
48-
ctx.Set(DefaultKey, store)
66+
ctx.Set(storeKey, store)
4967
ctx.Next()
5068
}
5169
}
5270

5371
// FromContext Get session storage from context
5472
func FromContext(ctx *gin.Context) session.Store {
55-
return ctx.MustGet(DefaultKey).(session.Store)
73+
return ctx.MustGet(storeKey).(session.Store)
5674
}
5775

5876
// Destroy a session

0 commit comments

Comments
 (0)