Skip to content

Commit

Permalink
Merge pull request #1054 from Fenny/master
Browse files Browse the repository at this point in the history
🩹 draft for #1051
  • Loading branch information
Fenny authored Dec 9, 2020
2 parents c9df437 + 142e6e5 commit e9a0f33
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 17 deletions.
2 changes: 1 addition & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
)

// Version of current fiber package
const Version = "2.2.3"
const Version = "2.2.4"

// Handler defines a function to serve HTTP requests.
type Handler = func(*Ctx) error
Expand Down
6 changes: 4 additions & 2 deletions internal/gotiny/decEngine.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gotiny

import (
"fmt"
"reflect"
"sync"
"time"
Expand Down Expand Up @@ -178,7 +179,8 @@ func buildDecEngine(rt reflect.Type, engPtr *decEng) {
decString(d, unsafe.Pointer(&name))
et, has := name2type[name]
if !has {
panic("unknown typ:" + name)
//panic("unknown typ:" + name)
fmt.Println("[session] Register this type first with the `RegisterType` method.")
}
v := reflect.NewAt(rt, p).Elem()
var ev reflect.Value
Expand All @@ -194,7 +196,7 @@ func buildDecEngine(rt reflect.Type, engPtr *decEng) {
}
}
case reflect.Chan, reflect.Func:
panic("not support " + rt.String() + " type")
//panic("not support " + rt.String() + " type")
default:
engine = baseDecEngines[kind]
}
Expand Down
2 changes: 1 addition & 1 deletion internal/gotiny/encEngine.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func buildEncEngine(rt reflect.Type, engPtr *encEng) {
}
}
case reflect.Chan, reflect.Func:
panic("not support " + rt.String() + " type")
//panic("not support " + rt.String() + " type")
default:
engine = encEngines[kind]
}
Expand Down
5 changes: 4 additions & 1 deletion middleware/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ func New(config ...Config) fiber.Handler {
c.Response().SetBodyRaw(e.body)
c.Response().SetStatusCode(e.status)
c.Response().Header.SetContentTypeBytes(e.ctype)

if len(e.cencoding) > 0 {
c.Response().Header.SetBytesV(fiber.HeaderContentEncoding, e.cencoding)
}
// Set Cache-Control header if enabled
if cfg.CacheControl {
maxAge := strconv.FormatUint(e.exp-ts, 10)
Expand All @@ -107,6 +109,7 @@ func New(config ...Config) fiber.Handler {
e.body = utils.SafeBytes(c.Response().Body())
e.status = c.Response().StatusCode()
e.ctype = utils.SafeBytes(c.Response().Header.ContentType())
e.cencoding = utils.SafeBytes(c.Response().Header.Peek(fiber.HeaderContentEncoding))

// For external Storage we store raw body seperated
if cfg.Storage != nil {
Expand Down
9 changes: 5 additions & 4 deletions middleware/cache/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import (
// don't forget to replace the msgp import path to:
// "github.com/gofiber/fiber/v2/internal/msgp"
type item struct {
body []byte
ctype []byte
status int
exp uint64
body []byte
ctype []byte
cencoding []byte
status int
exp uint64
}

//msgp:ignore manager
Expand Down
35 changes: 30 additions & 5 deletions middleware/cache/manager_msgp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions middleware/session/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Session middleware for [Fiber](https://github.com/gofiber/fiber)
### Signatures
```go
func New(config ...Config) *Store
func (s *Store) RegisterType(i interface{})
func (s *Store) Get(c *fiber.Ctx) (*Session, error)
func (s *Store) Reset() error

Expand Down
5 changes: 5 additions & 0 deletions middleware/session/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ type Config struct {
// KeyGenerator generates the session key.
// Optional. Default value utils.UUIDv4
KeyGenerator func() string

// CustomType allows you to store custom type/struct
// in any Storage provider. Only use this option if you
// are using custom a custom type/struct used as value.
CustomType interface{}
}

// ConfigDefault is the default config
Expand Down
10 changes: 7 additions & 3 deletions middleware/session/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ type Store struct {

var mux sync.Mutex

// Storage ErrNotExist
var errNotExist = "key does not exist"

func New(config ...Config) *Store {
// Set default config
cfg := configDefault(config...)
Expand All @@ -30,6 +27,13 @@ func New(config ...Config) *Store {
}
}

// RegisterType will allow you to encode/decode custom types
// into any Storage provider
func (s *Store) RegisterType(i interface{}) {
gotiny.Register(i)
}

// Get will get/create a session
func (s *Store) Get(c *fiber.Ctx) (*Session, error) {
var fresh bool

Expand Down

0 comments on commit e9a0f33

Please sign in to comment.