idea behind Finally implemented a light-weight yet powerful and a scalable also an effeiciant software solution for companies or organizations that need to restrict access to their services based on geographic location or whatever their needs may be.
- in-memory data storage without database dependencies
- thread-safe under concurrent operations. (refer to microsoft docs about concurrent dictionaries/hashtables)
- third-party geolocation integration
- comprehensive validation and testing at every level
- backround processing of temporary expired countries without performance overhead
Follow these steps to install and run the Country Blocking API on your local machine:
- .NET 8.0 SDK or later installed on your machine
- Git (to clone the repository)
-
Clone the repository:
<git clone SSH> git clone https://github.com/ahmedabougabal/dotnet-ip-geolocation-api.git
-
Navigate to the project directory:
cd dotnet-ip-geolocation-api
-
Build the application:
dotnet build
-
Run the application:
dotnet run
Once the application is running, you can access the Swagger UI documentation using the following link.
This will open the interactive API documentation where you can test all the endpoints directly from your browser.
Note: The port number (5059
) might be different on your machine. Check the console output when you run the application to see the actual URL.
Design Patterns & Clean Code Arch & Separation of concerns were carefully taken into consideration when engineering this app.
-
Repository Pattern : to abstract data access logic
-
Dependency Injection : registration of services and repositories using the built-in DI container
-
Service Layer Pattern : The GeolocationService encapsulates external API communication
-
Factory Pattern: HttpClientFactory for creating HttpClient instances
-
Singleton Pattern : repositories are registered as singletons to maintain in-memory state
this was before refactoring the code after recognizing that the input should be the country code only and an ip address is not required based on the assignment
this is after, works perfectly without any issues
testing against an invalid country code "XX" as per task that shall return a Bad Request as the country code should be an ISO 3166-1 alpha-2 code.
An Ip address lookup endpoint that returns all user's data including the timezone, country, location, ISP based on the input IP Address
these are some concerns and key takeaways related to Networking Fundamentals encountered when developing this app and testing it locally
The /api/logs/blocked-attempts
endpoint returns an empty response because no blocked attempts are recorded from outside (a real user with a real IP address from a restricted country) when testing locally, (in short : no actual one from a blocked country tried to access this API/Service)
Thus :
- No access attempts are made from blocked countries during testing.
- The logs repository remains empty without sample data.
Solution: I have added a sample data generation for blocked attempts as a mock testing, ensuring the endpoint returns meaningful data for local testing purposes.
2. Local Environment IP Detection Limitations (it is not possible - when running locally - to get the real external IP address from HttpContext so a default public IP is used instead that's all)
The /api/ip/check-block
endpoint faces limitations when running locally:
- On
localhost
,HttpContext.Connection.RemoteIpAddress
returns loopback addresses (::1
or127.0.0.1
), which cannot be geolocated. - This prevents accurate external IP detection.
Solution:
I added a localEnviroment logic that has a fallback IP (Google DNS: 8.8.8.8
) is used for testing, along with the local address, while providing a clear message in the response explaining the limitation.
These solutions I implemented to ensure a smooth addressing testing challenges and offering transparency.
The End :D - made with ❤️ by Ahmed Abou Gabal