Add custom short code support to web UI

Allow users to optionally specify their own short code when
shortening a URL, instead of always generating a random one.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-29 21:55:59 -07:00
parent 131924520d
commit f96e8cfaf2
3 changed files with 27 additions and 8 deletions

View File

@@ -134,14 +134,16 @@ func Lookup(ctx context.Context, db *sql.DB, s string) (string, error) {
return short, nil
}
func StoreURL(ctx context.Context, db *sql.DB, s string) (string, error) {
short, err := Lookup(ctx, db, s)
if err == nil {
return short, nil
}
func StoreURL(ctx context.Context, db *sql.DB, s string, customShort string) (string, error) {
if customShort == "" {
short, err := Lookup(ctx, db, s)
if err == nil {
return short, nil
}
if err != sql.ErrNoRows {
return "", err
if err != sql.ErrNoRows {
return "", err
}
}
u, err := FromString(s)
@@ -149,6 +151,10 @@ func StoreURL(ctx context.Context, db *sql.DB, s string) (string, error) {
return "", err
}
if customShort != "" {
u.Short = customShort
}
err = u.Store(ctx, db)
if err != nil {
return "", err