Go CLI and HTTP API for subnet calculations. Includes a Dockerfile and supports deployment to Cloud Run.
- Live demo of the calculator: https://subnets.web.app
- Go 1.22+
- Optional: Docker (for container builds)
- Optional: gcloud CLI (for Cloud Run deploys)
-
CLI (single calculation):
cd backend
go run . 192.168.1.10/26
-
HTTP server (local):
cd backend
go run . serve :8080
- Then:
curl 'http://localhost:8080/api/subnet?cidr=192.168.1.10/26'
Server respects PORT
env (e.g., PORT=8080 go run . serve
). If a positional port is provided (e.g., :8080
or 8080
), it overrides the env value.
- Endpoint:
GET /api/subnet?cidr=<ip/prefix>
- Query params:
cidr
(required): IPv4 CIDR like192.168.1.10/26
- Response (JSON):
- Subnet details:
address
,prefix
,total
,subnetMask
,wildcardMask
,network
,firstHost
,lastHost
,broadcast
,usableHosts
- Lists of possible addresses:
networks[]
,hosts[]
,broadcasts[]
Example:
- Subnet details:
GET /api/subnet?cidr=192.168.1.10/26
{
"address": "192.168.1.10",
"prefix": 26,
"total": 64,
"subnetMask": "255.255.255.192",
"wildcardMask": "0.0.0.63",
"network": "192.168.1.0",
"firstHost": "192.168.1.1",
"lastHost": "192.168.1.62",
"broadcast": "192.168.1.63",
"usableHosts": 62,
"networks": ["192.168.1.0", "192.168.1.64", "…"],
"hosts": ["192.168.1.1 - 192.168.1.62", "…"],
"broadcasts": ["192.168.1.63", "192.168.1.127", "…"]
}
Notes:
- Basic CORS is enabled (Access-Control-Allow-Origin:
*
).
Build a minimal image via multi-stage Dockerfile:
cd backend
docker build -t subnet-api .
docker run --rm -e PORT=8080 -p 8080:8080 subnet-api
# Test
curl 'http://localhost:8080/api/subnet?cidr=192.168.1.10/26'
The image uses a distroless runtime and reads the port from PORT
.
Using an already built container image:
gcloud builds submit --tag gcr.io/<PROJECT_ID>/subnet-api ./backend
gcloud run deploy subnet-api \
--image gcr.io/<PROJECT_ID>/subnet-api \
--region us-central1 \
--allow-unauthenticated
Source-based deploy (builds in Cloud Build):
gcloud run deploy subnet-api \
--source ./backend \
--region us-central1 \
--allow-unauthenticated