Features: - URL shortener with QR codes, click tracking, deduplication, rate limiting - Pastebin with Markdown rendering, access tokens, raw view - Admin dashboard: dark theme, stats bar, Quick Links tiles with favicons, card-based layout, format/token badges, empty states - CSRF protection, bcrypt auth, in-memory sessions, security headers - Pure-Go SQLite (no CGo), single self-contained binary Deploy: - deploy/install.sh: auto-detects systemd vs SysV init - deploy/urlshort.service: hardened systemd unit - deploy/urlshort.init: SysV init script for older Ubuntu Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
45 lines
886 B
Go
45 lines
886 B
Go
package slug
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand/v2"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
const alphabet = "abcdefghijklmnopqrstuvwxyz0123456789"
|
|
|
|
var reservedSlugs = map[string]bool{
|
|
"admin": true,
|
|
"shorten": true,
|
|
"static": true,
|
|
"api": true,
|
|
"health": true,
|
|
}
|
|
|
|
var validSlugRe = regexp.MustCompile(`^[a-zA-Z0-9_-]+$`)
|
|
|
|
func Generate(length int) string {
|
|
b := make([]byte, length)
|
|
for i := range b {
|
|
b[i] = alphabet[rand.IntN(len(alphabet))]
|
|
}
|
|
return string(b)
|
|
}
|
|
|
|
func Validate(slug string) error {
|
|
if len(slug) == 0 {
|
|
return fmt.Errorf("slug cannot be empty")
|
|
}
|
|
if len(slug) > 64 {
|
|
return fmt.Errorf("slug too long (max 64 characters)")
|
|
}
|
|
if !validSlugRe.MatchString(slug) {
|
|
return fmt.Errorf("slug can only contain letters, numbers, hyphens, and underscores")
|
|
}
|
|
if reservedSlugs[strings.ToLower(slug)] {
|
|
return fmt.Errorf("slug %q is reserved", slug)
|
|
}
|
|
return nil
|
|
}
|