An open API service indexing awesome lists of open source software.

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)

Awesome Lists containing this project

README

          

## Subnet Calculator Backend (Go)

[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](LICENSE)
[![Go Reference](https://pkg.go.dev/badge/github.com/Abe-alt/subnet.svg)](https://pkg.go.dev/github.com/Abe-alt/subnet)
![Go 1.22+](https://img.shields.io/badge/go-1.22%2B-00ADD8?logo=go)
![Cloud Run](https://img.shields.io/badge/Cloud%20Run-ready-4285F4?logo=google-cloud&logoColor=white)
![Docker](https://img.shields.io/badge/container-ready-2496ED?logo=docker&logoColor=white)

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
```