Go URL Shortener
About This Project
go-url-shortener is a simple URL shortening service implemented in Go. It has a small web UI and a REST API. You give it a long URL, it gives you a short one, and visiting the short one sends you to the original. The service runs on port 8080 by default and persists URL mappings in a local JSON file (urls.json).
Disclaimer: This project was built purely for learning and should not be used anywhere.
Features
- Web UI: A simple interface available at
/uito create and manage shortened URLs through your browser. - REST API:
POST /api/shortenaccepts a JSON payload{"url":"..."}and returns a JSON object containingid,url,shortUrl, andcreatedtimestamp.DELETE /api/delete/{id}removes a URL mapping by its unique ID.
- Redirect Endpoint:
GET /short/{alias}automatically redirects to the original long URL if the alias exists. - Persistence: URL records are loaded from
urls.jsonat startup and saved back to disk upon graceful server shutdown.
Development Process
- Setting up router: Configure Gorilla Mux routes to handle:
- Creating shortened URLs via
POST /api/shorten, generating random‐string IDs and storing mappings. - Redirecting users from
GET /short/{alias}back to the original long URLs.
- Creating shortened URLs via
- Setting up persistent storage: Implement JSON-based loading and saving of URL mappings to
urls.json. - Adding deletions: Create an endpoint to remove URL mappings by ID.
- Building a simple web UI: Serve static HTML/JS files under
/uifor client-side interaction.