https://github.com/abecli/subnet
Subnet Calculator Backend (Go)
https://github.com/abecli/subnet
cidr golang subnets
Last synced: 5 months ago
JSON representation
Subnet Calculator Backend (Go)
- Host: GitHub
- URL: https://github.com/abecli/subnet
- Owner: abecli
- License: mit
- Created: 2025-09-04T17:35:45.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2025-11-14T03:32:12.000Z (7 months ago)
- Last Synced: 2026-01-12T00:26:11.095Z (6 months ago)
- Topics: cidr, golang, subnets
- Language: Go
- Homepage: https://subnets.web.app/
- Size: 6.77 MB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Subnet Calculator Backend (Go)
[](LICENSE)
[](https://pkg.go.dev/github.com/Abe-alt/subnet)



Go CLI and HTTP API for subnet calculations. Includes a Dockerfile and supports deployment to Cloud Run.
### Live Demo
- Live demo of the calculator: https://subnets.web.app
### Requirements
- Go 1.22+
- Optional: Docker (for container builds)
- Optional: gcloud CLI (for Cloud Run deploys)
### Quick Start
- 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.
### API
- Endpoint: `GET /api/subnet?cidr=`
- Query params:
- `cidr` (required): IPv4 CIDR like `192.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:
```
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: `*`).
### Docker
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`.
### Cloud Run (container deploy)
Using an already built container image:
```
gcloud builds submit --tag gcr.io//subnet-api ./backend
gcloud run deploy subnet-api \
--image gcr.io//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
```