From b7db235eca6929eb6150d4b5d5e220cba3d1c1b0 Mon Sep 17 00:00:00 2001 From: Skyenought Date: Fri, 24 Nov 2023 10:34:21 +0800 Subject: [PATCH] gofumpt --- _example/redis_cluster/main.go | 3 ++- redis/redistore_test.go | 3 ++- rediscluster/redisc_store.go | 18 ++++++------- rediscluster/redisc_test.go | 5 ---- serializer.go | 47 ++++++++++++++++++++++++++++++++++ 5 files changed, 60 insertions(+), 16 deletions(-) diff --git a/_example/redis_cluster/main.go b/_example/redis_cluster/main.go index 64e53c4..f93e48e 100644 --- a/_example/redis_cluster/main.go +++ b/_example/redis_cluster/main.go @@ -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" @@ -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) { diff --git a/redis/redistore_test.go b/redis/redistore_test.go index 286b64e..69e5547 100644 --- a/redis/redistore_test.go +++ b/redis/redistore_test.go @@ -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" ) diff --git a/rediscluster/redisc_store.go b/rediscluster/redisc_store.go index e3e8ac4..52c59a3 100644 --- a/rediscluster/redisc_store.go +++ b/rediscluster/redisc_store.go @@ -50,7 +50,6 @@ import ( "context" "encoding/base32" "errors" - gredis "github.com/redis/go-redis/v9" "net/http" "strings" "time" @@ -58,12 +57,13 @@ import ( "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 @@ -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: "/", @@ -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...) } @@ -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, diff --git a/rediscluster/redisc_test.go b/rediscluster/redisc_test.go index 83182fd..2b9436a 100644 --- a/rediscluster/redisc_test.go +++ b/rediscluster/redisc_test.go @@ -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()) } @@ -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()) } @@ -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()) } @@ -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()) } @@ -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()) } diff --git a/serializer.go b/serializer.go index e728abd..17e4126 100644 --- a/serializer.go +++ b/serializer.go @@ -1,3 +1,49 @@ +/* + * 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 ( @@ -5,6 +51,7 @@ import ( "encoding/gob" "encoding/json" "fmt" + "github.com/cloudwego/hertz/pkg/common/hlog" "github.com/gorilla/sessions" )