A simple URL shortener service written in Go. This service allows users to submit any URL to be shortened and receive a valid shortened URL that will forward the user's request to the original URL.
- Shorten any valid URL
- Redirect to the original URL using the shortened URL
- JSON responses for errors and successful operations
git clone https://github.com/arshadsiddique/url-shortner.git
cd url-shortner
-
Install Dependencies:
go mod download
-
Run the Application:
go run main.go
-
Test the Application:
go test -v ./...
-
Build the Docker Image:
docker build -t urlshortener:latest .
-
Run the Docker Container:
docker run -p 8080:8080 urlshortener:latest
-
Shorten URL:
PUT /shorten Content-Type: application/json Request Body: { "destination": "https://www.google.com" } Response Body: { "shortened_url": "http://localhost:8080/abc123" }
-
Redirect to Original URL:
GET /{shortcode} Example: GET /abc123
-
Shorten a URL:
curl -X PUT http://localhost:8080/shorten -H "Content-Type: application/json" -d '{"destination": "https://www.google.com"}'
-
Redirect to the Original URL:
Assuming the shortened URL returned is http://localhost:8080/abc123, you can use:
curl -L http://localhost:8080/abc123