Home / File/ storage_manager.go — fiber Source File

storage_manager.go — fiber Source File

Architecture documentation for storage_manager.go, a go file in the fiber codebase. 1 imports, 0 dependents.

File go FiberMiddleware Session 1 imports 1 functions

Entity Profile

Dependency Diagram

graph LR
  5f51eff0_8a81_9f3c_b272_baa3335689bd["storage_manager.go"]
  cc7104af_aece_1fe5_3985_791c7f34910c["context"]
  5f51eff0_8a81_9f3c_b272_baa3335689bd --> cc7104af_aece_1fe5_3985_791c7f34910c
  style 5f51eff0_8a81_9f3c_b272_baa3335689bd fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

package csrf

import (
	"context"
	"fmt"
	"sync"
	"time"

	"github.com/gofiber/fiber/v3"
	"github.com/gofiber/fiber/v3/internal/memory"
)

// msgp -file="storage_manager.go" -o="storage_manager_msgp.go" -tests=true -unexported
//
//go:generate msgp -o=storage_manager_msgp.go -tests=true -unexported
type item struct{}

const redactedKey = "[redacted]"

//msgp:ignore manager
//msgp:ignore storageManager
type storageManager struct {
	pool       sync.Pool       `msg:"-"` //nolint:revive // Ignore unexported type
	memory     *memory.Storage `msg:"-"` //nolint:revive // Ignore unexported type
	storage    fiber.Storage   `msg:"-"` //nolint:revive // Ignore unexported type
	redactKeys bool
}

func newStorageManager(storage fiber.Storage, redactKeys bool) *storageManager {
	// Create new storage handler
	storageManager := &storageManager{
		pool: sync.Pool{
			New: func() any {
				return new(item)
			},
		},
		redactKeys: redactKeys,
	}
	if storage != nil {
		// Use provided storage if provided
		storageManager.storage = storage
	} else {
		// Fallback to memory storage
		storageManager.memory = memory.New()
	}
	return storageManager
}

// get raw data from storage or memory
func (m *storageManager) getRaw(ctx context.Context, key string) ([]byte, error) {
	if m.storage != nil {
		raw, err := m.storage.GetWithContext(ctx, key)
		if err != nil {
			return nil, fmt.Errorf("csrf: failed to get value from storage: %w", err)
		}
		return raw, nil
	}

	if value := m.memory.Get(key); value != nil {
		raw, ok := value.([]byte)
		if !ok {
			return nil, fmt.Errorf("csrf: unexpected value type %T in storage", value)
		}
		return raw, nil
	}

	return nil, nil
}

// set data to storage or memory
func (m *storageManager) setRaw(ctx context.Context, key string, raw []byte, exp time.Duration) error {
	if m.storage != nil {
		if err := m.storage.SetWithContext(ctx, key, raw, exp); err != nil {
			return fmt.Errorf("csrf: failed to store key %q: %w", m.logKey(key), err)
		}
		return nil
	}

	m.memory.Set(key, raw, exp)
	return nil
}

// delete data from storage or memory
func (m *storageManager) delRaw(ctx context.Context, key string) error {
	if m.storage != nil {
		if err := m.storage.DeleteWithContext(ctx, key); err != nil {
			return fmt.Errorf("csrf: failed to delete key %q: %w", m.logKey(key), err)
		}
		return nil
	}

	m.memory.Delete(key)
	return nil
}

func (m *storageManager) logKey(key string) string {
	if m.redactKeys {
		return redactedKey
	}
	return key
}

Subdomains

Dependencies

  • context

Frequently Asked Questions

What does storage_manager.go do?
storage_manager.go is a source file in the fiber codebase, written in go. It belongs to the FiberMiddleware domain, Session subdomain.
What functions are defined in storage_manager.go?
storage_manager.go defines 1 function(s): newStorageManager.
What does storage_manager.go depend on?
storage_manager.go imports 1 module(s): context.
Where is storage_manager.go in the architecture?
storage_manager.go is located at middleware/csrf/storage_manager.go (domain: FiberMiddleware, subdomain: Session, directory: middleware/csrf).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free