This is a server that will give you the country associated with an IP address.
The only endpoint available takes this form: localhost:3000/ips_country/<your_ip>
.
Below are some example requests and responses.
curl http://localhost:3000/ips_country/151.25.185.91
will return
{"country": "IT"}
or
{"country": "Italy"}
http://localhost:3000/ips_country/207.100.5.101
will return
{"country": "US"}
or
{"country": "United States"}
-
Open a terminal, and navigate to where you have downloaded this code. Continue to work in this terminal.
-
Run
npm install
to install dependencies needed. -
Set the environment variable
IP_STACK_KEY
to your api key for IP Stack -
Set the environment variable
IP_INFO_KEY
to your api key for IP Info -
Run
npm run start
to start the server.
This server relies on the function called should_use_ipstack
to choose which service to use for IP-to-country translation. In the current implementation, I simply return true
. Returning true
means IP Stack will be used when the server is running. Returning false
means IP Info will be used when the server is running.
The two IP to Country providers I used are below:
I chose to use Axios and Express, as these sped up development and are industry standard tools.
I implemented a caching mechanism that simply uses a JSON object attached to my express app.
Given that I was instructed to limit myself to two hours of effort and my expertise in backend lie more with Python and Golang, there were several crucial items I didn't get to. I will go through each and describe what I would have done.
Currently, this function is only stubbed out. I would implement this, probably leveraging the solution described below in Per-Vendor Global Configurable Rate Limit
.
I started working on this but did not complete it. Had I had more time, I likely would have implemented a fixed window solution.
I would have written test cases in Jest. I would have written the following test cases:
- Send a request with an invalid ip address. Expect a 400 response.
- Send a request with a valid ip address that had not yet been seen. Expect a 200 response, a correct country code/name, and no header indicating the data was retrieved from the cache (using a mock for IP-to-country service)
- Send a request with a valid ip address that had not yet been seen. Expect a 200 response, a correct country code/name, and a header indicating the data was retrieved from the cache
In this exercise, I chose to use a RegEx because I knew I could validate the solution was working relatively quickly. I would prefer to avoid writing production grade software with regular expressions, so I would have instead used an algorithmic approach to validate a string represented an IP address.
In this implementation, I also only support IPv4 addresses. In a more robust implementation of this tool, I would add support for IPv6 addresses.
There are a number of errors I can think of that are not well-handled in this implementation. Ones I have not yet mentioned relate to the connection between this server and the third party services used. I would return a 500 error code for any such error.