Skip to content

Latest commit

 

History

History
114 lines (86 loc) · 2.72 KB

File metadata and controls

114 lines (86 loc) · 2.72 KB

Wirekit Examples

This directory contains example code demonstrating various features of the wirekit package.

Examples

Directory Description
basic/ Core utilities: logging, strings, crypto, time, email, context
http-client/ HTTP client with retries, timeouts, and JSON handling
retry/ Retry logic with exponential backoff
jwt/ JWT token generation and validation
validation/ Request validation with custom rules
ratelimit/ Rate limiting (token bucket, sliding window)
response/ API response formatting and pagination
database/ PostgreSQL and MongoDB connections
cache/ Redis client, cache, pub/sub, and queue

Running Examples

# Navigate to an example directory
cd example/basic

# Run the example
go run main.go

Quick Start

Logging

import "github.com/yigithankarabulut/wirekit"

// Initialize logger
wirekit.Log(wirekit.Zap).
    WithLevel("debug").
    WithDevelopment(true).
    Init()

// Log messages
wirekit.Info("server started", zap.String("port", "8080"))
wirekit.Error("connection failed", zap.Error(err))

HTTP Client

client := wirekit.HTTP().
    WithBaseURL("https://api.example.com").
    WithTimeout(10 * time.Second).
    WithRetry(3, time.Second).
    Build()

var data Response
err := client.GetJSON(ctx, "/users/1", &data)

JWT

// Generate token
token, _ := wirekit.JWT().
    WithSecret("secret").
    WithExpiration(24 * time.Hour).
    WithClaim("role", "admin").
    Generate("user-123")

// Validate token
claims, _ := wirekit.JWT().WithSecret("secret").Validate(token)
fmt.Println(claims.Subject()) // user-123

Database

// PostgreSQL with GORM
db, err := wirekit.Postgres().GORM().
    WithDSN("host=localhost user=postgres dbname=myapp").
    Connect()

// MongoDB
client, db, err := wirekit.Mongo().
    WithURI("mongodb://localhost:27017").
    WithDatabase("myapp").
    ConnectWithDB(ctx)

Utilities

// String utilities
slug := wirekit.Str.Slugify("Hello World!") // hello-world
random := wirekit.Str.RandomAlphanumeric(16)

// Crypto utilities
hash, _ := wirekit.Crypto.HashPassword("password")
valid := wirekit.Crypto.CheckPassword("password", hash)
uuid := wirekit.Crypto.UUID()

// Time utilities
start := wirekit.Time.StartOfDay(time.Now())
human := wirekit.Time.HumanDuration(3 * time.Hour) // "3 hours"

// Email utilities
valid := wirekit.Email.IsValid("user@example.com")
masked := wirekit.Email.Mask("user@example.com") // u***@example.com

More Information

See the main README for complete documentation.