Skip to content

Commit

Permalink
gofumpt
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyenought committed Nov 24, 2023
1 parent afa1607 commit b7db235
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 16 deletions.
3 changes: 2 additions & 1 deletion _example/redis_cluster/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ package main

import (
"context"

"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/common/utils"
Expand All @@ -51,7 +52,7 @@ import (

func main() {
h := server.Default(server.WithHostPorts(":8000"))
store, _ := rediscluster.NewStore(10, "tcp", []string{"localhost:5001", "localhost:5002"}, "", []byte("secret"))
store, _ := rediscluster.NewStore(10, []string{"localhost:5001", "localhost:5002"}, "", nil, []byte("secret"))
h.Use(sessions.New("mysession", store))

h.GET("/incr", func(ctx context.Context, c *app.RequestContext) {
Expand Down
3 changes: 2 additions & 1 deletion redis/redistore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ import (
"bytes"
"encoding/base64"
"encoding/gob"
hs "github.com/hertz-contrib/sessions"
"net/http"
"net/http/httptest"
"testing"

hs "github.com/hertz-contrib/sessions"

"github.com/gorilla/sessions"
)

Expand Down
18 changes: 9 additions & 9 deletions rediscluster/redisc_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,20 @@ import (
"context"
"encoding/base32"
"errors"
gredis "github.com/redis/go-redis/v9"
"net/http"
"strings"
"time"

"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
hs "github.com/hertz-contrib/sessions"
"github.com/redis/go-redis/v9"
)

var sessionExpire = 86400 * 30

type Store struct {
Rdb *gredis.ClusterClient
Rdb *redis.ClusterClient
Codecs []securecookie.Codec
Opts *sessions.Options // default configuration
DefaultMaxAge int // default Redis TTL for a MaxAge == 0 session
Expand All @@ -77,9 +77,9 @@ func (s *Store) Options(options hs.Options) {
}

// NewStoreWithOption returns a new rediscluster.Store by setting redisc.Cluster
func NewStoreWithOption(opt *gredis.ClusterOptions, kvs ...[]byte) (*Store, error) {
func NewStoreWithOption(opt *redis.ClusterOptions, kvs ...[]byte) (*Store, error) {
rs := &Store{
Rdb: gredis.NewClusterClient(opt),
Rdb: redis.NewClusterClient(opt),
Codecs: securecookie.CodecsFromPairs(kvs...),
Opts: &sessions.Options{
Path: "/",
Expand All @@ -90,14 +90,14 @@ func NewStoreWithOption(opt *gredis.ClusterOptions, kvs ...[]byte) (*Store, erro
keyPrefix: "session_",
serializer: hs.GobSerializer{},
}
err := rs.Rdb.ForEachShard(context.Background(), func(ctx context.Context, shard *gredis.Client) error {
err := rs.Rdb.ForEachShard(context.Background(), func(ctx context.Context, shard *redis.Client) error {
return shard.Ping(ctx).Err()
})
return rs, err
}

// NewStore returns a new rediscluster.Store
func NewStore(maxIdle int, addrs []string, password string, newClient func(opt *gredis.Options) *gredis.Client, kvs ...[]byte) (*Store, error) {
func NewStore(maxIdle int, addrs []string, password string, newClient func(opt *redis.Options) *redis.Client, kvs ...[]byte) (*Store, error) {
return NewStoreWithOption(newOption(addrs, password, maxIdle, newClient), kvs...)
}

Expand Down Expand Up @@ -241,9 +241,9 @@ func newOption(
addrs []string,
password string,
maxIdleConns int,
newClient func(opt *gredis.Options) *gredis.Client,
) *gredis.ClusterOptions {
return &gredis.ClusterOptions{
newClient func(opt *redis.Options) *redis.Client,
) *redis.ClusterOptions {
return &redis.ClusterOptions{
Addrs: addrs,
NewClient: newClient,
Password: password,
Expand Down
5 changes: 0 additions & 5 deletions rediscluster/redisc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ func TestNewRedisCluster(t *testing.T) {

{
store, err := NewStore(10, []string{"localhost:5000", "localhost:5001"}, "", nil, []byte("secret-key"))

if err != nil {
t.Fatal(err.Error())
}
Expand Down Expand Up @@ -213,7 +212,6 @@ func TestNewRedisCluster(t *testing.T) {

{
store, err := NewStore(10, []string{"localhost:5000", "localhost:5001"}, "", nil, []byte("secret-key"))

if err != nil {
t.Fatal(err.Error())
}
Expand Down Expand Up @@ -254,7 +252,6 @@ func TestNewRedisCluster(t *testing.T) {

{
store, err := NewStore(10, []string{"localhost:5000", "localhost:5001"}, "", nil, []byte("secret-key"))

if err != nil {
t.Fatal(err.Error())
}
Expand Down Expand Up @@ -288,7 +285,6 @@ func TestNewRedisCluster(t *testing.T) {

{
store, err := NewStore(10, []string{"localhost:5000", "localhost:5001"}, "", nil, []byte("secret-key"))

if err != nil {
t.Fatal(err.Error())
}
Expand Down Expand Up @@ -319,7 +315,6 @@ func TestNewRedisCluster(t *testing.T) {

{
store, err := NewStore(10, []string{"localhost:5000", "localhost:5001"}, "", nil, []byte("secret-key"))

if err != nil {
t.Fatal(err.Error())
}
Expand Down
47 changes: 47 additions & 0 deletions serializer.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,57 @@
/*
* Copyright 2023 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright (c) 2012 Rodrigo Moraes. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This file may have been modified by CloudWeGo authors. All CloudWeGo
* Modifications are Copyright 2022 CloudWeGo Authors.
*/

package sessions

import (
"bytes"
"encoding/gob"
"encoding/json"
"fmt"

"github.com/cloudwego/hertz/pkg/common/hlog"
"github.com/gorilla/sessions"
)
Expand Down

0 comments on commit b7db235

Please sign in to comment.