models

package
v0.0.0-...-70a9c39 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateEvent

func CreateEvent(db *gorm.DB, eventType EventType, resourceType string, resourceID string, userAgent string, ipAddress string, refererURL string) error

CreateEvent is a helper function to create a new analytics event

func GenerateAPIKey

func GenerateAPIKey() string

GenerateKey generates a new API key string

Types

type APIKey

type APIKey struct {
	Key       string `gorm:"primarykey;type:varchar(64)"`
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt gorm.DeletedAt `gorm:"index"`

	// Paste-related limits and permissions
	MaxFileSize  int64 // 10MB default
	RateLimit    int   // Requests per hour
	AllowPrivate bool  `gorm:"default:true"`
	AllowUpdates bool  `gorm:"default:true"`

	// URL shortening permissions
	AllowShortlinks bool   `gorm:"default:true"`     // Whether this key can create shortlinks
	ShortlinkQuota  int    `gorm:"default:0"`        // 0 = unlimited
	ShortlinkPrefix string `gorm:"type:varchar(16)"` // Optional custom prefix for shortened URLs

	// Optional user information
	Email string `gorm:"type:varchar(255)"`
	Name  string `gorm:"type:varchar(255)"`

	// Usage tracking
	LastUsedAt *time.Time
	UsageCount int64

	// Verification
	Verified     bool   `gorm:"default:false"`
	VerifyToken  string `gorm:"type:varchar(64)"`
	VerifyExpiry time.Time

	IsReset bool `json:"is_reset" gorm:"default:false"`
}

func NewAPIKey

func NewAPIKey() *APIKey

NewAPIKey creates a new APIKey with default values

func (*APIKey) BeforeCreate

func (k *APIKey) BeforeCreate(tx *gorm.DB) error

BeforeCreate sets defaults and generates the API key if not set

type AnalyticsEvent

type AnalyticsEvent struct {
	ID        uint `gorm:"primarykey"`
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt gorm.DeletedAt `gorm:"index"`

	// Event information
	EventType EventType `gorm:"type:varchar(32);index;not null"`

	// Resource information (what the event is about)
	ResourceID   string `gorm:"type:varchar(16);index;not null"` // ID of the shortlink or paste
	ResourceType string `gorm:"type:varchar(32);index;not null"` // "shortlink" or "paste"

	// Request information
	UserAgent  string `gorm:"type:text"`
	IPAddress  string `gorm:"type:varchar(45)"` // IPv6 addresses can be up to 45 chars
	RefererURL string `gorm:"type:text"`
	Browser    string `gorm:"type:varchar(32)"`
	OS         string `gorm:"type:varchar(32)"`
	Device     string `gorm:"type:varchar(32)"`

	// Location information
	City    string `gorm:"type:varchar(255)"`
	Region  string `gorm:"type:varchar(255)"`
	ZipCode string `gorm:"type:varchar(10)"`
	Country string `gorm:"type:varchar(2)"`

	// Additional data
	Metadata JSON `gorm:"type:jsonb"`
}

AnalyticsEvent represents a single analytics event

type EventType

type EventType string

EventType represents different types of events that can be tracked

const (
	EventShortlinkClick EventType = "shortlink_click"
	EventPasteView      EventType = "paste_view"
)

type JSON

type JSON json.RawMessage

JSON custom type to handle both PostgreSQL JSONB and SQLite TEXT

func (JSON) MarshalJSON

func (j JSON) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler interface

func (*JSON) Scan

func (j *JSON) Scan(value any) error

Scan implement sql.Scanner interface

func (*JSON) UnmarshalJSON

func (j *JSON) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler interface

func (JSON) Value

func (j JSON) Value() (driver.Value, error)

Value implement driver.Valuer interface

type Paste

type Paste struct {
	ID        string `gorm:"primarykey;type:varchar(16)"`
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt gorm.DeletedAt `gorm:"index"`

	// Content information
	Filename  string `gorm:"type:varchar(255)"`
	MimeType  string `gorm:"type:varchar(255)"`
	Size      int64
	Extension string `gorm:"type:varchar(32)"`

	// Storage information
	StoragePath string `gorm:"type:varchar(512)"`
	StorageType string `gorm:"type:varchar(32)"` // "local" or "s3"
	StorageName string `gorm:"type:varchar(64)"` // Name of the storage config

	// Access control
	Private   bool
	DeleteKey string `gorm:"type:varchar(32)"`
	APIKey    string `gorm:"type:varchar(64);index"` // If created with an API key

	// Expiration
	ExpiresAt *time.Time `gorm:"index"`

	// Optional metadata
	Metadata JSON `gorm:"type:jsonb"` // For PostgreSQL, will fallback to JSON string for SQLite
}

func (*Paste) BeforeCreate

func (p *Paste) BeforeCreate(tx *gorm.DB) error

BeforeCreate generates ID and DeleteKey if not set

type Shortlink struct {
	ID        string `gorm:"primarykey;type:varchar(8)"` // Shorter IDs for URLs
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt gorm.DeletedAt `gorm:"index"`

	// URL information
	TargetURL string `gorm:"type:text;not null"`
	Title     string `gorm:"type:varchar(255)"` // Optional, can be fetched from target

	// Access control
	APIKey    string     `gorm:"type:varchar(64);not null;index"` // Required for creation
	DeleteKey string     `gorm:"type:varchar(32);not null"`
	ExpiresAt *time.Time `gorm:"index"`

	// Optional metadata (referrer stats, etc.)
	Metadata JSON `gorm:"type:jsonb"`
}

func (*Shortlink) BeforeCreate

func (s *Shortlink) BeforeCreate(tx *gorm.DB) error

func (*Shortlink) ToResponse

func (s *Shortlink) ToResponse(baseURL string) fiber.Map

Jump to

Keyboard shortcuts

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