This project is a simple URL shortener built in Go. It allows users to shorten long URLs and provides redirection to the original URLs when the shortened URL is visited.
- Generate a short URL for any long URL.
- Redirect users to the original URL using the short URL.
- In-memory URL store (no persistence across restarts).
main.go
: The entry point of the application that sets up the HTTP server and routes.store.go
: Contains logic for the in-memory URL storage and URL generation.handlers.go
: Contains the HTTP handler functions for shortening and redirecting URLs.
- Go installed on your machine.
-
Clone the repository:
git clone https://github.com/your-username/go-url-shortener.git cd go-url-shortener
-
Run the application:
go run *.go
-
The server will start on
localhost:8080
.
-
Endpoint:
/shorten
-
Method:
POST
-
Request Body: JSON with the original URL:
{ "url": "https://www.example.com" }
-
Response: JSON with the shortened URL:
{ "short_url": "abc123" }
curl -X POST http://localhost:8080/shorten \
-H "Content-Type: application/json" \
-d '{"url":"https://www.example.com"}'
- Endpoint: `/r/{short_url}
- Method:
GET
- Response: Redirects to the original URL
http://localhost:8080/r/abc123
- Persistent storage (e.g., using a database).
- Custom short URL creation.
- URL expiration feature.