url-shortner is a fast, secure, and efficient URL shortening service built with Go. It generates unique, random alphanumeric codes for URLs, ensuring unpredictability and uniqueness. The application uses PostgreSQL with GORM ORM, Tailwind for UI, and templ for template generation.
- Shorten long URLs to compact codes
- Redirect to original URLs securely
- Unique, random code generation
- Database persistence using PostgreSQL
- Live reload for frontend/backend development
- Backend: Go, GORM ORM
- Database: PostgreSQL
- Frontend: Tailwind CSS
- Templates: templ
- Containerization: Docker & Docker Compose
- Build Tools: Makefile
Before running the project, make sure you have:
- Go installed (v1.20+ recommended)
- Docker & Docker Compose
- Make
- Optional:
psqlCLI for database inspection
-
Clone the repository
git clone <repository-url> cd url-shortner
-
Create a
.envfilecp .env.example .env
Update the file with your local configuration.
-
Set up PostgreSQL database
-
Start the Docker container:
docker compose up -d
-
Example
.envconfiguration:DB_HOST=localhost DB_PORT=5432 DB_NAME=url-shortner DB_USER=testing DB_PASSWORD=12345678
-
Verify connection:
psql -h localhost -p 5432 -U yusuf -d url-shortner
If prompted, enter your password. You should see the
url-shortnerdatabase. -
If the database does not exist (first-time setup):
CREATE DATABASE "url-shortner";
-
-
Run the application
make watch
-
Open in browser
http://localhost:3001to access live reload version.
NOTE: Always run the application on port 3000. If you change the port in .env, also update .air.toml.
| Variable | Description | Example |
|---|---|---|
| DB_HOST | Database host | localhost |
| DB_PORT | Database port | 5431 |
| DB_NAME | Database name | url-shortner |
| DB_USER | Database user | yusuf |
| DB_PASSWORD | Database password | 12345678 |
| DB_SSLMODE | Enable/disable SSL for Postgres | disable |
| PORT | Application port | 3000 |
| WEB_PORT | Proxy / frontend port | 3002 |
| APP_URL | App URL for local testing | http://localhost:3001 |
| ENV | Environment (local/dev/prod) | local |
Build the application
make buildRun the application
make runLive reload for development
make watchClean up binaries from previous build
make cleanGenerate templ function files
templ generateRun all tests:
go test ./...- Fork the repository
- Create a feature branch
- Run tests
- Submit a pull request
This project is licensed under the MIT License.
