Skip to content

Instantly share code, notes, and snippets.

@mohashari
mohashari / snippet-2.go
Created March 14, 2026 15:12
Code snippets — Monitoring Prometheus Grafana
var (
ordersCreated = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "orders_created_total",
Help: "Total orders created",
}, []string{"payment_method", "tier"})
orderValue = promauto.NewHistogramVec(prometheus.HistogramOpts{
Name: "order_value_dollars",
Help: "Distribution of order values",
Buckets: []float64{1, 5, 10, 25, 50, 100, 250, 500, 1000},
@mohashari
mohashari / snippet-2.go
Created March 14, 2026 15:13
Code snippets — Clean Code Principles
// Bad: This function does 3 things
func processUser(userID string) error {
// 1. Validate
if userID == "" {
return errors.New("user ID required")
}
// 2. Fetch from DB
user, err := db.GetUser(userID)
if err != nil {
@mohashari
mohashari / snippet-2.sh
Created March 14, 2026 15:13
Code snippets — Git Workflow Best Practices
feature/oauth-google-login
fix/order-status-not-updating
chore/upgrade-postgres-16
experiment/grpc-migration-poc
@mohashari
mohashari / snippet-10.go
Created March 14, 2026 15:13
Code snippets — Api Security Owasp
// BAD: Trust third-party data blindly
resp := stripe.GetPayment(paymentID)
db.Save(Payment{Amount: resp.Amount, Currency: resp.Currency})
// GOOD: Validate before using
resp := stripe.GetPayment(paymentID)
if resp.Amount <= 0 || resp.Amount > MaxPaymentAmount {
return errors.New("invalid amount from payment provider")
}
if !validCurrencies[resp.Currency] {
@mohashari
mohashari / snippet-2.go
Created March 14, 2026 15:13
Code snippets — Event Driven Architecture
func ReplayOrder(events []Event) Order {
var order Order
for _, event := range events {
switch e := event.(type) {
case OrderPlaced:
order.ID = e.OrderID
order.Status = "pending"
order.Items = e.Items
order.Total = e.Total
case PaymentTaken:
@mohashari
mohashari / snippet-2.sh
Created March 14, 2026 15:13
Code snippets — Linux Commands For Backend Engineers
# Find files
find /var/log -name "*.log" -newer /tmp/marker
find . -name "*.go" -size +1M
find . -mtime -1 # Modified in last 24 hours
# Search inside files
grep -r "ERROR" /var/log/myapp/
grep -r "panic" . --include="*.go"
grep -n "func.*Handler" src/api/*.go
@mohashari
mohashari / snippet-2.conf
Created March 14, 2026 15:13
Code snippets — Load Balancing Techniques
upstream backend {
server 192.168.1.10:8080 max_fails=3 fail_timeout=30s;
server 192.168.1.11:8080 max_fails=3 fail_timeout=30s;
}
@mohashari
mohashari / snippet-10.sql
Created March 14, 2026 15:13
Code snippets — Sql Query Optimization
-- Partition orders by year/month
CREATE TABLE orders (
id BIGSERIAL,
user_id INT NOT NULL,
amount DECIMAL NOT NULL,
created_at TIMESTAMP NOT NULL
) PARTITION BY RANGE (created_at);
CREATE TABLE orders_2025 PARTITION OF orders
FOR VALUES FROM ('2025-01-01') TO ('2026-01-01');
@mohashari
mohashari / snippet-2.ts
Created March 14, 2026 15:13
Code snippets — Typescript For Backend Engineers
// Type guard: narrows type based on runtime check
function isApiError(error: unknown): error is ApiError {
return (
typeof error === 'object' &&
error !== null &&
'statusCode' in error &&
'message' in error
);
}
@mohashari
mohashari / snippet-10.go
Created March 14, 2026 23:26
WebSockets at Scale: Real-Time Backend Architecture
func (c *Client) writePump(conn *websocket.Conn) {
ticker := time.NewTicker(30 * time.Second)
defer func() {
ticker.Stop()
conn.Close()
}()
conn.SetPongHandler(func(string) error {
conn.SetReadDeadline(time.Now().Add(65 * time.Second))
return nil