New creates idempotency middleware that caches responses keyed by the configured idempotency header.
(config ...Config)
| 43 | // New creates idempotency middleware that caches responses keyed by the |
| 44 | // configured idempotency header. |
| 45 | func New(config ...Config) fiber.Handler { |
| 46 | // Set default config |
| 47 | cfg := configDefault(config...) |
| 48 | |
| 49 | redactKeys := !cfg.DisableValueRedaction |
| 50 | |
| 51 | maskKey := func(key string) string { |
| 52 | if redactKeys { |
| 53 | return redactedKey |
| 54 | } |
| 55 | return key |
| 56 | } |
| 57 | |
| 58 | keepResponseHeadersMap := make(map[string]struct{}, len(cfg.KeepResponseHeaders)) |
| 59 | for _, h := range cfg.KeepResponseHeaders { |
| 60 | // CopyString is needed because utils.ToLower uses UnsafeString |
| 61 | // and map keys must be immutable |
| 62 | keepResponseHeadersMap[utilsstrings.ToLower(h)] = struct{}{} |
| 63 | } |
| 64 | |
| 65 | maybeWriteCachedResponse := func(c fiber.Ctx, key string) (bool, error) { |
| 66 | if val, err := cfg.Storage.GetWithContext(c, key); err != nil { |
| 67 | return false, fmt.Errorf("failed to read response: %w", err) |
| 68 | } else if val != nil { |
| 69 | var res response |
| 70 | if _, err := res.UnmarshalMsg(val); err != nil { |
| 71 | return false, fmt.Errorf("failed to unmarshal response: %w", err) |
| 72 | } |
| 73 | |
| 74 | _ = c.Status(res.StatusCode) |
| 75 | |
| 76 | for header, vals := range res.Headers { |
| 77 | for _, val := range vals { |
| 78 | c.RequestCtx().Response.Header.Add(header, val) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | if len(res.Body) != 0 { |
| 83 | if err := c.Send(res.Body); err != nil { |
| 84 | return true, err |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | _ = c.Locals(localsKeyIsFromCache, true) |
| 89 | |
| 90 | return true, nil |
| 91 | } |
| 92 | |
| 93 | return false, nil |
| 94 | } |
| 95 | |
| 96 | return func(c fiber.Ctx) error { |
| 97 | // Don't execute middleware if Next returns true |
| 98 | if cfg.Next != nil && cfg.Next(c) { |
| 99 | return c.Next() |
| 100 | } |
| 101 | |
| 102 | // Don't execute middleware if the idempotency key is empty |