This is a simple project built using Django to provide an API endpoint to greet visitors and provide their IP address, location, and current temperature. I deployed project on DigitalOcean's App Platform.
- GET /api/visitor: Returns the client's IP address, location, and a personalized greeting along with the current temperature of the location.
- Django
- Django REST Framework
- IP2Location API (for location data)
- OpenWeatherMap API (for weather data)
- Python 3.6+
- Django 3.0+
- Django REST Framework
- Requests
- Gunicorn
- Environs
- Psycopg
git clone https://github.com/BjornOnGit/hng-backend_stage_1.git
cd hello-api
python3 -m .venv venv
source venv/bin/activate
pip install -r requirements.txt
Create a .env file in the project root and add your API keys:
SECRET_KEY=your_secret_key
DEBUG=True
IP2LOCATION_API_KEY=your_ip2location_api_key
OPENWEATHER_API_KEY=your_openweather_api_key
python manage.py migrate
python manage.py runserver
- visitor_name (optional): The name of the visitor to include in the greeting.
curl "http://127.0.0.1:8000/api/hello?visitor_name=Mark"
{
"client_ip": "203.0.113.195",
"location": "New York",
"greeting": "Hello, Mark! The temperature is 11 degrees Celsius in New York"
}
-
Create a DigitalOcean Account: Sign up here.
-
Create a New Project: Log in to your DigitalOcean account and create a new project.
-
Create a New App: In your project, click "Create App" and follow the prompts to deploy your application.
-
Configure Environment Variables: Set the IP2LOCATION_API_KEY and OPENWEATHER_API_KEY environment variables in the App Platform dashboard.
-
Deploy: Follow the prompts to deploy your app.
The following settings are used in the project:
- IP2LOCATION_API_KEY: Your IP2Location API key for getting location data.
- OPENWEATHER_API_KEY: Your OpenWeatherMap API key for getting weather data.
Ensure your middleware is correctly set up to handle X-Forwarded-For headers for accurate client IP detection:
MIDDLEWARE = [
...
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.http.ConditionalGetMiddleware',
]