A simple RESTful API for storing and retrieving geolocation data based on IP address or URL. It uses ipstack as the geolocation provider, but the provider is pluggable so it can be swapped out later.
- Rails 8 API-only application
- PostgreSQL for persistence
- Geolocation lookups via ipstack
- JSON:API-compliant input/output
- RSpec test suite
- Docker + Docker Compose for easy setup
- Optional API key authentication (X-API-Keyheader)
(Optional) If you want to run locally without Docker:
- Ruby 3.4+
- PostgreSQL 15+
- 
Clone the repository: git clone https://github.com/digirat/geolocation-api.git cd geolocation-api
- 
Copy the example environment file: cp .env.docker.example .env.docker 
- 
Edit .env.dockerand set your ipstack API key:IPSTACK_API_KEY=your_real_key_here 
- 
Build and run the stack: docker compose up --build 
- 
The API will be available at: http://localhost:3000
curl -X POST http://localhost:3000/geolocations \
  -H 'Content-Type: application/json' \
  -H 'X-API-Key: secret' \
  -d '{"data":{"type":"geolocations","attributes":{"query":"8.8.8.8"}}}'curl -H 'X-API-Key: secret' http://localhost:3000/geolocationscurl -H 'X-API-Key: secret' http://localhost:3000/geolocations/8.8.8.8curl -X DELETE -H 'X-API-Key: secret' http://localhost:3000/geolocations/8.8.8.8Inside Docker:
docker compose run --rm web bin/rails specLocally (if you prefer):
bundle exec rspec- 
Install dependencies: bundle install 
- 
Set up PostgreSQL and environment variables (example .envrcfor direnv):export POSTGRESQL_USERNAME=postgres export POSTGRESQL_PASSWORD=yourpassword export POSTGRESQL_ADDRESS=localhost export POSTGRESQL_DB=geolocation_dev export POSTGRESQL_TEST_DB=geolocation_test export POSTGRESQL_PROD_DB=geolocation_prod export IPSTACK_API_KEY=your_real_key_here export IPSTACK_BASE_URL=http://api.ipstack.com export GEO_PROVIDER=ipstack export API_KEY=secret export CORS_ORIGINS=* 
- 
Prepare the database: bin/rails db:prepare 
- 
Start the server: bin/rails s 
- All endpoints require the header:
(configurable in your env file)X-API-Key: secret
- By default, CORS is open (*). Restrict it by settingCORS_ORIGINSin your environment.
- If you change DB credentials in .env.docker, delete volumes withdocker compose down -vand re-run.
MIT