A Bitly-like URL shortener with analytics, built in Go using Gin.
- Shorten URLs
- Redirect with analytics (clicks, referrers, geolocation)
- Pluggable storage: BoltDB, Redis, or Postgres
- Prometheus metrics endpoint (
/metrics) - API key authentication & rate limiting
- Install dependencies:
go mod tidy
- Run the server (BoltDB by default):
STORE_TYPE=boltdb go run ./cmd/main.go
- API endpoints:
- POST
/shorten{ "url": "https://example.com" }(requiresX-API-Keyheader) - GET
/:shortcode(redirect) - GET
/:shortcode/stats(analytics) - GET
/metrics(Prometheus metrics)
- POST
STORE_TYPE:boltdb,redis, orpostgresDATABASE_URL: For PostgresREDIS_ADDR,REDIS_PASSWORD: For RedisAPI_KEY: Required for authenticated endpoints
To run all tests (unit and integration):
go test ./...To run tests for a specific package:
go test ./internal/api/...- Ensure all dependencies are installed and your configuration/environment variables are set.
- Build the application:
go build -o shorten-it ./cmd/main.go
- Deploy the binary to your server or preferred cloud provider.
- (Optional) For containerized deployment, use the provided Dockerfile:
docker build -t shorten-it . docker run -e STORE_TYPE=boltdb -e API_KEY=yourkey -p 8080:8080 shorten-it - For cloud deployment (Heroku, Google Cloud Run, AWS, Netlify, etc.), see DEPLOY.md.
This project uses GitHub Actions for CI. See .github/workflows/ci.yml for details.
- On every push and pull request, the workflow checks out the code, installs dependencies, builds the app, and runs all tests.
A multi-stage build for minimal images:
FROM golang:1.22-alpine AS builder
WORKDIR /app
COPY . .
RUN go build -o shorten-it ./cmd/main.go
FROM alpine:latest
WORKDIR /app
COPY --from=builder /app/shorten-it .
EXPOSE 8080
CMD ["./shorten-it"]See DEPLOY.md for step-by-step guides for Docker, Heroku, Google Cloud Run, AWS, and Netlify.
MIT License