Skip to content

Commit

Permalink
add options
Browse files Browse the repository at this point in the history
  • Loading branch information
Stan committed Jan 27, 2020
1 parent e698eec commit 2981a50
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ import (
)
func main() {
expiration := 60 * time.Second
secretKey := "my-secret-key"
sessions.Init(secretKey, expiration)
opts := &sessions.Options{
Secret:"my-secret-key",
Expiration: 60 * time.Second,
}
sessions.Init(opts)
data := map[string]string{
"username": "Test",
"role": "user",
Expand Down
8 changes: 5 additions & 3 deletions docs/examples/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
)

func main() {
expiration := 60 * time.Second
secretKey := "my-secret-key"
sessions.Init(secretKey, expiration)
opts := &sessions.Options{
Secret:"my-secret-key",
Expiration: 60 * time.Second,
}
sessions.Init(opts)
data := map[string]string{
"username": "Test",
"role": "user",
Expand Down
11 changes: 8 additions & 3 deletions sessions/sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ var (
ErrEncryptionError = errors.New("error during encryption")
)

type Options struct {
Secret string
Expiration time.Duration
}

// Init - initialise library, set secretKey for cypher hash and expirationTime
func Init(key string, timeout time.Duration) {
crypt.SecretKey = key
expirationTime = timeout
func Init(opts *Options) {
crypt.SecretKey = opts.Secret
expirationTime = opts.Expiration
}

// Create - create session from map
Expand Down
13 changes: 6 additions & 7 deletions sessions/sessions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import (
)

func TestInit(t *testing.T) {
expectedSecretKey := "test"
expectedTimeout := 1 * time.Second
Init(expectedSecretKey, expectedTimeout)
if crypt.SecretKey != expectedSecretKey {
t.Error("Expected", expectedSecretKey, "got", crypt.SecretKey)
opts := &Options{Secret: "test", Expiration: 1 * time.Second}
Init(opts)
if crypt.SecretKey != opts.Secret {
t.Error("Expected", opts.Secret, "got", crypt.SecretKey)
}
if expirationTime != expectedTimeout {
t.Error("Expected", expectedTimeout, "got", expirationTime)
if expirationTime != opts.Expiration {
t.Error("Expected", opts.Expiration, "got", expirationTime)
}
}

Expand Down

0 comments on commit 2981a50

Please sign in to comment.