web

package
v0.1.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 28, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package web provides utilities for handling web assets in the application. It supports both development mode (using Vite dev server) and production mode (using built assets from the Vite manifest).

Package web provides utilities for handling web assets in the application.

Package web provides utilities for handling web assets in the application.

Package web provides utilities for handling web assets in the application. It supports both development mode (using Vite dev server) and production mode (using built assets from the Vite manifest).

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAssetNotFound    = errors.New("asset not found")
	ErrInvalidManifest  = errors.New("invalid manifest")
	ErrInvalidPath      = errors.New("invalid asset path")
	ErrManifestNotFound = errors.New("manifest not found")
	ErrPathTooLong      = errors.New("asset path too long")
	ErrPathTooShort     = errors.New("asset path too short")
	ErrEmptyManifest    = errors.New("manifest is empty")
	ErrResolverNotFound = errors.New("asset resolver not found")
)

Asset-related errors

Functions

func NormalizePath added in v0.1.5

func NormalizePath(path string) string

NormalizePath normalizes an asset path for consistent handling

func ValidateAssetPath added in v0.1.5

func ValidateAssetPath(path string) error

ValidateAssetPath validates an asset path according to standard rules

Types

type AssetCache added in v0.1.5

type AssetCache interface {
	// Get retrieves a cached asset path
	Get(key string) (string, bool)

	// Set stores an asset path in the cache
	Set(key, value string)

	// Clear clears all cached entries
	Clear()

	// Size returns the number of cached entries
	Size() int
}

AssetCache defines the interface for caching resolved asset paths

type AssetConfig added in v0.1.5

type AssetConfig struct {
	Environment       Environment
	BaseURL           string
	ManifestPath      string
	StaticPath        string
	CacheEnabled      bool
	MaxCacheSize      int
	AllowedExtensions []string
	ViteDevServerURL  string
}

AssetConfig holds configuration for asset management

type AssetInfo added in v0.1.5

type AssetInfo struct {
	Path         string
	ResolvedPath string
	Type         AssetType
	Size         int64
	Exists       bool
	IsEntry      bool
	Dependencies []string
}

AssetInfo contains metadata about an asset

type AssetManager added in v0.1.5

type AssetManager struct {
	// contains filtered or unexported fields
}

AssetManager handles asset path resolution and caching

func NewAssetManager added in v0.1.5

func NewAssetManager(cfg *config.Config, logger logging.Logger, distFS embed.FS) (*AssetManager, error)

NewAssetManager creates a new asset manager with proper dependency injection

func (*AssetManager) AssetPath added in v0.1.5

func (m *AssetManager) AssetPath(path string) string

AssetPath returns the resolved asset path for the given input path This is a convenience method that uses a background context

func (*AssetManager) ClearCache added in v0.1.5

func (m *AssetManager) ClearCache()

ClearCache clears the asset path cache

func (*AssetManager) GetAssetType added in v0.1.5

func (m *AssetManager) GetAssetType(path string) AssetType

GetAssetType returns the type of asset based on its path

func (*AssetManager) GetBaseURL added in v0.1.5

func (m *AssetManager) GetBaseURL() string

GetBaseURL returns the base URL for assets (useful for CSP headers)

func (*AssetManager) GetCacheSize added in v0.1.5

func (m *AssetManager) GetCacheSize() int

GetCacheSize returns the current number of cached entries

func (*AssetManager) GetConfig added in v0.1.5

func (m *AssetManager) GetConfig() *config.Config

GetConfig returns the configuration (useful for testing)

func (*AssetManager) GetResolver added in v0.1.5

func (m *AssetManager) GetResolver() AssetResolver

GetResolver returns the underlying resolver (useful for testing)

func (*AssetManager) IsPathCached added in v0.1.5

func (m *AssetManager) IsPathCached(path string) bool

IsPathCached checks if a path is already cached

func (*AssetManager) ResolveAssetPath added in v0.1.5

func (m *AssetManager) ResolveAssetPath(ctx context.Context, path string) (string, error)

ResolveAssetPath resolves asset paths with context and proper error handling

func (*AssetManager) ValidatePath added in v0.1.5

func (m *AssetManager) ValidatePath(path string) error

ValidatePath validates an asset path

type AssetManagerFactory added in v0.1.5

type AssetManagerFactory struct {
	// contains filtered or unexported fields
}

AssetManagerFactory creates asset managers with different configurations

func NewAssetManagerFactory added in v0.1.5

func NewAssetManagerFactory(cfg *config.Config, logger logging.Logger) *AssetManagerFactory

NewAssetManagerFactory creates a new asset manager factory

func (*AssetManagerFactory) CreateManager added in v0.1.5

func (f *AssetManagerFactory) CreateManager(distFS embed.FS) (*AssetManager, error)

CreateManager creates an asset manager for the current environment

func (*AssetManagerFactory) CreateManagerWithResolver added in v0.1.5

func (f *AssetManagerFactory) CreateManagerWithResolver(resolver AssetResolver) *AssetManager

CreateManagerWithResolver creates an asset manager with a specific resolver

type AssetManagerInterface added in v0.1.5

type AssetManagerInterface interface {
	// AssetPath returns the resolved asset path for the given input path
	// This is a convenience method that uses a background context
	AssetPath(path string) string

	// ResolveAssetPath resolves asset paths with context and proper error handling
	ResolveAssetPath(ctx context.Context, path string) (string, error)

	// GetAssetType returns the type of asset based on its path
	GetAssetType(path string) AssetType

	// ClearCache clears the asset path cache (if applicable)
	ClearCache()

	// ValidatePath validates an asset path
	ValidatePath(path string) error

	// GetBaseURL returns the base URL for assets (useful for CSP headers)
	GetBaseURL() string
}

AssetManagerInterface defines the contract for asset management

type AssetPathRule added in v0.1.5

type AssetPathRule struct {
	Condition func(string) bool
	Transform func(string, string) string
}

AssetPathRule defines a rule for transforming asset paths

type AssetPathValidator added in v0.1.5

type AssetPathValidator interface {
	// ValidatePath validates an asset path according to defined rules
	ValidatePath(path string) error

	// IsValidExtension checks if the file extension is allowed
	IsValidExtension(ext string) bool
}

AssetPathValidator defines validation rules for asset paths

type AssetResolver added in v0.1.5

type AssetResolver interface {
	// ResolveAssetPath resolves an asset path with context and proper error handling
	ResolveAssetPath(ctx context.Context, path string) (string, error)
}

AssetResolver defines the interface for resolving asset paths

type AssetResolverFactory added in v0.1.5

type AssetResolverFactory struct {
	// contains filtered or unexported fields
}

AssetResolverFactory creates the appropriate resolver based on environment

func NewAssetResolverFactory added in v0.1.5

func NewAssetResolverFactory(cfg *config.Config, logger logging.Logger) *AssetResolverFactory

NewAssetResolverFactory creates a new factory

func (*AssetResolverFactory) CreateResolver added in v0.1.5

func (f *AssetResolverFactory) CreateResolver(distFS embed.FS) (AssetResolver, error)

CreateResolver creates the appropriate resolver for the current environment

type AssetServer added in v0.1.5

type AssetServer interface {
	// RegisterRoutes registers the necessary routes for serving assets
	RegisterRoutes(e *echo.Echo) error
	// Start starts the asset server if needed (e.g., for development mode)
	Start(ctx context.Context) error
	// Stop stops the asset server gracefully
	Stop(ctx context.Context) error
}

AssetServer defines the interface for serving assets

type AssetServerConfig added in v0.1.5

type AssetServerConfig struct {
	PublicDir       string
	MaxAge          time.Duration
	EnableGzip      bool
	EnableBrotli    bool
	SecurityHeaders map[string]string
	CustomMimeTypes map[string]string
}

AssetServerConfig holds configuration for asset servers

func DefaultAssetServerConfig added in v0.1.5

func DefaultAssetServerConfig() AssetServerConfig

DefaultAssetServerConfig returns default configuration for asset servers

type AssetServerFactory added in v0.1.5

type AssetServerFactory struct {
	// contains filtered or unexported fields
}

AssetServerFactory creates the appropriate asset server based on environment

func NewAssetServerFactory added in v0.1.5

func NewAssetServerFactory(cfg *config.Config, logger logging.Logger) *AssetServerFactory

NewAssetServerFactory creates a new asset server factory

func (*AssetServerFactory) CreateServer added in v0.1.5

func (f *AssetServerFactory) CreateServer(distFS embed.FS) AssetServer

CreateServer creates the appropriate asset server for the current environment

type AssetType added in v0.1.5

type AssetType string

AssetType represents the type of asset

const (
	// AssetTypeJS represents JavaScript files
	AssetTypeJS AssetType = "js"
	// AssetTypeTS represents TypeScript files
	AssetTypeTS AssetType = "ts"
	// AssetTypeCSS represents CSS files
	AssetTypeCSS AssetType = "css"
	// AssetTypeImage represents image files
	AssetTypeImage AssetType = "image"
	// AssetTypeFont represents font files
	AssetTypeFont AssetType = "font"
	// AssetTypeJSON represents JSON files
	AssetTypeJSON AssetType = "json"
	// AssetTypeUnknown represents unknown file types
	AssetTypeUnknown AssetType = "unknown"

	// MaxPathLength represents the maximum allowed path length
	MaxPathLength = 255
	// MinPathLength represents the minimum allowed path length
	MinPathLength = 1
)

func GetAssetTypeFromPath added in v0.1.5

func GetAssetTypeFromPath(path string) AssetType

GetAssetTypeFromPath determines the asset type from a file path

type DevelopmentAssetResolver added in v0.1.5

type DevelopmentAssetResolver struct {
	// contains filtered or unexported fields
}

DevelopmentAssetResolver handles development asset resolution

func NewDevelopmentAssetResolver added in v0.1.5

func NewDevelopmentAssetResolver(cfg *config.Config, logger logging.Logger) *DevelopmentAssetResolver

NewDevelopmentAssetResolver creates a new development asset resolver

func (*DevelopmentAssetResolver) ClearCache added in v0.1.5

func (r *DevelopmentAssetResolver) ClearCache()

ClearCache clears the path resolution cache

func (*DevelopmentAssetResolver) ResolveAssetPath added in v0.1.5

func (r *DevelopmentAssetResolver) ResolveAssetPath(ctx context.Context, path string) (string, error)

ResolveAssetPath resolves asset paths for development using the Vite dev server

type DevelopmentAssetServer added in v0.1.5

type DevelopmentAssetServer struct {
	// contains filtered or unexported fields
}

DevelopmentAssetServer implements AssetServer for development mode

func NewDevelopmentAssetServer added in v0.1.5

func NewDevelopmentAssetServer(cfg *config.Config, logger logging.Logger) *DevelopmentAssetServer

NewDevelopmentAssetServer creates a new development asset server

func (*DevelopmentAssetServer) RegisterRoutes added in v0.1.5

func (s *DevelopmentAssetServer) RegisterRoutes(e *echo.Echo) error

RegisterRoutes registers routes for static files in development

func (*DevelopmentAssetServer) Start added in v0.1.5

Start initializes the development asset server

func (*DevelopmentAssetServer) Stop added in v0.1.5

Stop gracefully stops the development asset server

func (*DevelopmentAssetServer) WithConfig added in v0.1.5

WithConfig allows customizing the server configuration

type EmbeddedAssetServer added in v0.1.5

type EmbeddedAssetServer struct {
	// contains filtered or unexported fields
}

EmbeddedAssetServer implements AssetServer for embedded static files in production

func NewEmbeddedAssetServer added in v0.1.5

func NewEmbeddedAssetServer(logger logging.Logger, distFS embed.FS) *EmbeddedAssetServer

NewEmbeddedAssetServer creates a new embedded asset server

func (*EmbeddedAssetServer) RegisterRoutes added in v0.1.5

func (s *EmbeddedAssetServer) RegisterRoutes(e *echo.Echo) error

RegisterRoutes registers the embedded static file serving routes

func (*EmbeddedAssetServer) Start added in v0.1.5

func (s *EmbeddedAssetServer) Start(ctx context.Context) error

Start initializes the embedded asset server

func (*EmbeddedAssetServer) Stop added in v0.1.5

Stop gracefully stops the embedded asset server

func (*EmbeddedAssetServer) WithConfig added in v0.1.5

WithConfig allows customizing the server configuration

type Environment added in v0.1.5

type Environment string

Environment represents the deployment environment

const (
	// EnvironmentDevelopment represents development mode
	EnvironmentDevelopment Environment = "development"
	// EnvironmentProduction represents production mode
	EnvironmentProduction Environment = "production"
	// EnvironmentTest represents test mode
	EnvironmentTest Environment = "test"
)

type Manifest

type Manifest map[string]ManifestEntry

Manifest represents the Vite manifest file

func (Manifest) GetEntry added in v0.1.5

func (m Manifest) GetEntry(path string) (ManifestEntry, bool)

GetEntry safely retrieves a manifest entry

func (Manifest) GetEntryPaths added in v0.1.5

func (m Manifest) GetEntryPaths() []string

GetEntryPaths returns all entry point paths from the manifest

func (Manifest) IsEmpty added in v0.1.5

func (m Manifest) IsEmpty() bool

IsEmpty returns true if the manifest has no entries

type ManifestEntry

type ManifestEntry struct {
	File           string   `json:"file"`
	Name           string   `json:"name,omitempty"`
	Src            string   `json:"src,omitempty"`
	IsEntry        bool     `json:"is_entry"`
	CSS            []string `json:"css,omitempty"`
	Assets         []string `json:"assets,omitempty"`
	Imports        []string `json:"imports,omitempty"`
	DynamicImports []string `json:"dynamic_imports,omitempty"`
}

ManifestEntry represents an entry in the Vite manifest file

type Module added in v0.1.5

type Module struct {
	AssetManager  AssetManagerInterface
	AssetServer   AssetServer
	AssetResolver AssetResolver
}

Module encapsulates the asset manager and server to eliminate global state

func NewModule added in v0.1.5

func NewModule(cfg *config.Config, logger logging.Logger, distFS embed.FS) (*Module, error)

NewModule creates a new web module with proper dependency injection

type ProductionAssetResolver added in v0.1.5

type ProductionAssetResolver struct {
	// contains filtered or unexported fields
}

ProductionAssetResolver handles production asset resolution

func NewProductionAssetResolver added in v0.1.5

func NewProductionAssetResolver(manifest Manifest, logger logging.Logger) *ProductionAssetResolver

NewProductionAssetResolver creates a new production asset resolver

func (*ProductionAssetResolver) ResolveAssetPath added in v0.1.5

func (r *ProductionAssetResolver) ResolveAssetPath(ctx context.Context, path string) (string, error)

ResolveAssetPath resolves asset paths for production using the manifest

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL