Skip to content

Conversation

@thomasorgeval
Copy link
Owner

This commit introduces a feature to track visits for each redirected domain, including geolocation information based on the visitor's IP address.

A SQLite database (stats.db) is used to persistently store visit data. Each visit records the URL, a timestamp, and the visitor's country and city.

The geolocation is performed by calling the ip-api.com external service. While a local database like GeoLite2 was initially considered, the external API is simpler to integrate for this project's scale.

A new API endpoint /stats is added to expose the collected data. It returns a JSON response with the total number of visits, a breakdown by URL, and for each URL, a breakdown by country. It also includes a simple metric for "unique users today," which is based on a count of distinct cities.

The previous in-memory visit counter has been removed. A .gitignore file has also been added to exclude build artifacts and the database file from version control.

This commit introduces a feature to track visits for each redirected domain, including geolocation information based on the visitor's IP address.

A SQLite database (`stats.db`) is used to persistently store visit data. Each visit records the URL, a timestamp, and the visitor's country and city.

The geolocation is performed by calling the `ip-api.com` external service. While a local database like GeoLite2 was initially considered, the external API is simpler to integrate for this project's scale.

A new API endpoint `/stats` is added to expose the collected data. It returns a JSON response with the total number of visits, a breakdown by URL, and for each URL, a breakdown by country. It also includes a simple metric for "unique users today," which is based on a count of distinct cities.

The previous in-memory visit counter has been removed. A `.gitignore` file has also been added to exclude build artifacts and the database file from version control.
This commit fixes the Docker build by enabling cgo, which is required by the `go-sqlite3` driver.

The `golang:alpine` base image does not have cgo enabled by default. This change adds the necessary build tools (`gcc`, `musl-dev`) to the builder stage and sets `CGO_ENABLED=1` during the `go build` command.

This ensures that the application can correctly link against the C-based SQLite library when built inside the Docker container.
This commit addresses two issues:
1. The visit counter was incorrectly incrementing for any visited path. The main handler has been modified to only process requests for the root path (`/`), ensuring that only relevant visits are counted.
2. The database was not persistent when running in Docker. The application now uses the `DB_PATH` environment variable to allow specifying a custom path for the database file. The `README.md` has been updated with instructions on how to use this with Docker volumes to achieve data persistence.
@thomasorgeval thomasorgeval merged commit 4e8b881 into main Aug 28, 2025
1 check passed
@thomasorgeval thomasorgeval deleted the feature/visit-counter branch August 28, 2025 08:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant