The URL Shortener is a web application designed to simplify the process of sharing long URLs by converting them into short, easy-to-remember aliases. Users can generate short aliases for lengthy links and use these aliases to redirect to the original URLs. The project is built using Go (Golang) and utilizes SQLite for efficient data storage.
- Short URL Creation: Users can submit a long URL and receive a short alias.
- Redirection via Alias: When accessing the short alias, users will be redirected to the original URL.
- Link Deletion: Users can delete existing aliases.
- Basic Authentication: Authentication is required to access the API.
- Programming Language: Go (Golang)
- Database: SQLite
- Web Framework: Chi (router and middleware)
- Logging: slog (structured logging)
- Testing: httpexpect, testify
cmd/url-shortener
: Main package for running the application.internal/config
: Application configuration (loaded from a YAML file).internal/http-server
: HTTP server with request handlers.internal/storage
: Database logic (SQLite).pkg/logger
: Custom logger with support for different logging levels.pkg/random
: Random alias generation.tests
: Integration tests for the API.
- Installed Go (version 1.23 or higher).
- SQLite3 (for data storage).
-
Clone the repository:
git clone https://github.com/your-username/url-shortener.git cd url-shortener
-
Install dependencies:
go mod download
-
Create a configuration file config/local.yaml
env: "local" storage_path: "./storage/storage.db" http_server: host: "localhost" port: "8081" timeout: "5s" idle_timeout: "60s" user: "admin" password: "admin"
-
Run the application:
go run cmd/url-shortener/main.go
-
The application will be available at: http://localhost:8081
docker build -t url-shortener .
docker run -p 8081:8081 url-shortener
Creating a Short URL
curl -X POST http://localhost:8081/url \
-H "Content-Type: application/json" \
-u admin:admin \
-d '{"url": "https://example.com", "alias": "example"}'
Response:
{
"status": "OK",
"alias": "example"
}
Access the link http://localhost:8081/example
You will be redirected to https://example.com.
Deleting a URL
curl -X DELETE http://localhost:8081/url/example \
-u admin:admin
Response:
{
"status": "OK"
}
Logging is configured using the slog library. Depending on the environment (local, dev, prod), logs may be in text or JSON format. In local mode, logs are pretty printed to the console.