A simple API for storing DNS records (IP addresses) belonging to hostnames, developed with Ruby (v2.6.5) on Rails (v5.2.3).
This is not meant to run as a production application.
Note: These commands were tested in a Linux terminal.
-
Clone the repo.
git clone https://github.com/ivamluz/DnsEntriesAPI.git cd DnsEntriesAPI
-
Create the
.env
file.cp .env.template .env
-
Update the
.env
file with the required values. -
Start the services. The first run may take a few minutes as Docker images are pulled/built for the first time.
make debug
-
Open http://localhost:3000 in a web browser. You should see the Rails welcome page
- Check what container the application is running at. Run
docker ps
and take note of the ruby:2.6.5 container id. It should look similar to this:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
734bbd53f0ca ruby:2.6.5 "entrypoint.sh rails…" 23 seconds ago Up 22 seconds 0.0.0.0:3000->3000/tcp
-
Apply migrations:
docker exec 734bbd53f0ca rails db:migrate
-
Setup the
test
database:docker exec 734bbd53f0ca rails db:setup RAILS_ENV=test
docker exec 734bbd53f0ca rails test
# Note: These commands were tested in a Linux terminal.
curl -XPOST \
--header "Content-Type: application/json" \
--header "Accept: application/json" \
'http://localhost:3000/dns_records/' \
-d @- << EOF
{
"dns_records": {
"ip": "1.1.1.1",
"hostnames_attributes": [
{ "hostname": "lorem.com" },
{ "hostname": "ipsum.com" },
{ "hostname": "dolor.com" },
{ "hostname": "amet.com" }
]
}
}
EOF
curl -XPOST \
--header "Content-Type: application/json" \
--header "Accept: application/json" \
'http://localhost:3000/dns_records/' \
-d @- << EOF
{
"dns_records": {
"ip": "2.2.2.2",
"hostnames_attributes": [
{ "hostname": "ipsum.com" }
]
}
}
EOF
curl -XPOST \
--header "Content-Type: application/json" \
--header "Accept: application/json" \
'http://localhost:3000/dns_records/' \
-d @- << EOF
{
"dns_records": {
"ip": "3.3.3.3",
"hostnames_attributes": [
{ "hostname": "ipsum.com" },
{ "hostname": "dolor.com" },
{ "hostname": "amet.com" }
]
}
}
EOF
curl -XPOST \
--header "Content-Type: application/json" \
--header "Accept: application/json" \
'http://localhost:3000/dns_records/' \
-d @- << EOF
{
"dns_records": {
"ip": "4.4.4.4",
"hostnames_attributes": [
{ "hostname": "ipsum.com" },
{ "hostname": "dolor.com" },
{ "hostname": "sit.com" },
{ "hostname": "amet.com" }
]
}
}
EOF
curl -XPOST \
--header "Content-Type: application/json" \
--header "Accept: application/json" \
'http://localhost:3000/dns_records/' \
-d @- << EOF
{
"dns_records": {
"ip": "5.5.5.5",
"hostnames_attributes": [
{ "hostname": "sit.com" }
]
}
}
EOF
Request
curl 'http://localhost:3000/dns_records?included[]=ipsum.com&included[]=dolor.com&excluded[]=sit.com&page=1'
Response
{
"total_records": 2,
"records": [
{
"id": 1,
"ip_address": "1.1.1.1"
},
{
"id": 3,
"ip_address": "3.3.3.3"
}
],
"related_hostnames": [
{
"hostname": "amet.com",
"count": 2
},
{
"hostname": "lorem.com",
"count": 1
}
]
}
- Use
page
argument to paginate DB results. - Check query performance with a bigger dataset.
- Check if there is a better place transform the DB result into the API format. This is currently done inside the DnsRecord model.
When you're finished, stop the services.
make stop
All available commands can be seen by calling make help
:
$ make help
Usage:
make <target>
Targets:
help Show help
start Start the services
debug Start the services in debug mode
sql Start an interactive psql session (services must be running)
logs Show the service logs (services must be running)
stop Stop the services
clear-db Clear the sandbox and development databases
The Docker Compose setup and the Makefile where inspired on Plaid Pattern app.