Build your own IPV4 geolocation database
Build with ❤ in Rust
Don't judge my code, I've only been programming in RUST for 3 weeks without being a developer :)
Table of Contents generated with DocToc
- Badges
- Introduction
- How works
- Local development
- Running in local
- Examples
- Import from local mongodb to mongodb atlas
- TO DO
- Contribution
- LICENSE
So imagine that you want to start indexing public IPv4 geolocation data. There are paid services, others free but requiring registration, that offer this type of content. By consulting one of these services you will obtain the data you need.
I do not know exactly all the public services that offer this data, in my case I have used the public database of ip-api.com
Without this service this tool does not work, it would be necessary to refactor and use another API.
Other possible solutions (not implemented):
When you start the API, it accepts all 4 CRUD methods of a simple API. Insert, get, delete and update. All data is stored in mongodb (the only database implemented to date). We will see more examples of the commands in this README.
- Rust
- Cargo
- MongoDB
Take a look to the official documentation
git clone https://github.com/containerscrew/ipfinder.git
cd ipfinder
DB_ENDPOINT="mongodb://admin:admin@localhost:27017/?maxPoolSize=20&w=majority"
DB_NAME="ipfinder"
COLLECTION_NAME="ips"
RUST_LOG="actix_web=debug"
If you are using mongodb atlas, just set the endpoint that you get from the mongodb atlas console:
DB_ENDPOINT="mongodb+srv://XXXX:[email protected]/?retryWrites=true&w=majority"
DB_NAME="ipfinder"
COLLECTION_NAME="ips"
RUST_LOG="actix_web=debug"
If not, run the container locally
docker-compose -f compose.yml up -d
cargo binstall cargo-watch
cargo watch -x run
cargo run --
cargo build --release # --release flag for production environment, without --release flag for testing
If the previous build was success, then:
./target/release/ipfinder
Remember to have the database created locally or in mongo atlas, otherwise the API will panic
- containerfile
- k8s....
TO DO...
curl -XPOST http://127.0.0.1:8081/api/v1/ipfinder/insert -d '{"ip":"8.8.8.8"}' -H "Content-Type: application/json"
curl -XGET http://127.0.0.1:8081/api/v1/ipfinder/get/8.8.8.8
THIS METHOD IS ACTUALLY FAILING, NEED TO BE FIXED
curl -XPUT http://127.0.0.1:8081/api/v1/ipfinder/update/8.8.8.8
The update method is not a CRUD update as such. By relying on data from an external API, launching the update command basically re-queries the data from the external ip-api database and refreshes it again.
curl -XDELETE http://127.0.0.1:8081/api/v1/ipfinder/delete/8.8.8.8
curl -XGET http://127.0.0.1:8081/api/v1/ipfinder/health
Using mongodb compass you can visualize your data from the collection ips
brew install mongodb/brew/mongodb-database-tools
If you are not using OSX, please visit the official documentation to install mongodump
and mongorestore
mongodump --uri="mongodb://admin:admin@localhost:27017/?maxPoolSize=20&w=majority"
This command will create a new dump/
directory with the backup
mongorestore --uri="mongodb+srv://USERNAME:[email protected]/?retryWrites=true&w=majority" --db="ipfinder" --collection="ips" dump/ipfinder/ips.bson
- improve error handling and logging with env_logger (custom error handling)
- implement update function in CRUD
- middleware with API authentication?
- testing like: https://github.com/actix/examples/blob/master/databases/mongodb/src/test.rs
- testcontainers for pipeline testing: https://docs.rs/testcontainers/latest/testcontainers/
- containerize this application to allow launching inside a simple container or pod in k8s (create also a small helm chart)
- bind address and port should be defined by the user
Pull requests are welcome! Any code refactoring, improvement, implementation. I just want to learn Rust! I'm a rookie