Skip to content

Abe-alt/subnet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Subnet Calculator Backend (Go)

License: MIT Go Reference Go 1.22+ Cloud Run Docker

Go CLI and HTTP API for subnet calculations. Includes a Dockerfile and supports deployment to Cloud Run.

Live Demo

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=<ip/prefix>
  • 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/<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

About

Subnet Calculator Backend (Go)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published