Simple Django Website Use Google Maps API To Display Current Guest IP Location and Dockerizing It For Deploy By Docker

– Create new Django project and new app

-Configure new app on file settings.py

-Define new url for home page on urls.py

-Define function processing for home page:
+Get current IP by API from https://api.myip.com/
+Get information about country, city and latitude/longitude by API from http://ip-api.com/json/<ip>
+Support checking this information store on session cache if already exist not need to call API again(only load from cache and response for user) for reduce traffic for web server

-Log in to google cloud console and anable MAP API: https://console.cloud.google.com/home/

-Choose explore and enable API

-Choose ENABLE APIS AND SERVICES

– Choose Maps Embed API

-On API Dashboard choose Credentials for create API Key for use MAPS API service: https://console.cloud.google.com/apis/dashboard


-Choose Create Credentials->API Key
-Click on new generated key for restrict to access MAP API Key and Save



-Create template home.html for display IP information:
Use generated key for call Google Maps Embed API and show by iframe on home.html
Source value of tag iframe to display is: src=”https://www.google.com/maps/embed/v1/view?key=<your API Key>¢er=10.8142,106.6438&zoom=9″

-Apply migrations and running server

-User first browse will show result not get from cache

-Reload page will get result from cache (not need call APT to get information of visitor IP again)

*Build Django Project Image Use For Running Container:
-Go to source folder and create new Dockerfile with below content:
FROM python:3.8
LABEL maintainer=’vmt1991′
RUN pip install Django requests
WORKDIR /sources
COPY ./ /sources
EXPOSE 8000
CMD python manage.py runserver 0.0.0.0:8000
-Build new image with command:

-Run new container with new image:

-Config nginx as reverse proxy routing traffic to this container:
Edit: /etc/nginx/nginx.conf

-Start service nginx and testing browse to url http://checkipapi.vmt.local

