Skip to content

Wire in SQLite DB; Have routes write / read from the db#16

Merged
Howard-nolan merged 1 commit intomainfrom
feature/add_db
Nov 30, 2025
Merged

Wire in SQLite DB; Have routes write / read from the db#16
Howard-nolan merged 1 commit intomainfrom
feature/add_db

Conversation

@Howard-nolan
Copy link
Owner

Change Summary:

  • Wired DB into handlers; shortened URLs now persist to SQLite and redirects fetch from DB.
  • Added random code generation with collision retries, URL validation/normalization, and real redirects.
  • Fixed goose embed/dialect setup and used request-aware DB calls.

How It Works:

  • cmd/server/main.go opens ./data/app.db, runs migrations, builds a handlers.Handler with the shared *sql.DB, and registers routes.
  • ShortenHandler validates/normalizes the incoming URL (adds https:// if missing), generates a 6-char code, inserts (code, url) into links via ExecContext with retries on collisions, and returns the short link.
  • RedirectHandler looks up the URL by code using QueryRowContext; 404s if missing, otherwise issues an HTTP 302 to the stored URL.

Additional Notes (optional):

  • Bare hostnames like www.google.com are accepted; malformed URLs still 400.

@Howard-nolan Howard-nolan merged commit 17f8159 into main Nov 30, 2025
github-actions bot added a commit that referenced this pull request Nov 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant