Skip to content

Commit

Permalink
imp #13: Addig swagger default endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
donkeyx committed Sep 26, 2023
1 parent 8b880f6 commit b31187e
Show file tree
Hide file tree
Showing 10 changed files with 752 additions and 48 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ BUILD_FLAGS = go build -ldflags "-w -s -X main.Version=$(VERSION) -X main.GitHas

all: clean deps test build-all
build:
swag init
CGO_ENABLED=0 $(BUILD_FLAGS) -o bin/$(BINARY_NAME) -v
test:
go test ./... --race
Expand All @@ -22,7 +23,8 @@ clean:
find ./bin/ -type f | grep -v keep | xargs rm

deps:
go get .
go get ./...
go install github.com/swaggo/swag/cmd/swag@latest

update:
go get ./...
Expand Down
33 changes: 19 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,32 @@ Simple docker image to allow testing within clusters or locally. It provides me
Default route is json response with environment variables, with more to come...

dockerhub: https://hub.docker.com/repository/docker/donkeyx/cluster-utils-api
github: https://github.com/donkeyx/cluster-utils-api

## Usage

The the endpoints are generally readable, but /a/ will be authenticated and you can find the bearer token in the logs. This will rotate on every startup to keep the sensitive endpoints a bit more secure.

### Start container:

```bash
docker run -d -p 8080:8080 --name test-api donkeyx/cluster-utils-api:latest

# then curl the container
$ curl -sS localhost:8080 | jq
{
"version": "v1.1",
"endpoints": [
"/health",
"/healthz",
"/ready",
"/readyz",
"/headers",
"/readyness_delay",
"/a/env"
]
}

```

### run image in k8 cluster:
Expand Down Expand Up @@ -47,20 +66,6 @@ Now you can use port forwarding to curl your apis inside the cluster
# in one windows forward the ports to the service
$ kubectl -n default port-forward svc/cluster-utils-api 8080:80

# then curl the service -> pod
$ curl -sS localhost:8080 | jq
{
"version": "v1.1",
"endpoints": [
"/health",
"/healthz",
"/ready",
"/readyz",
"/headers",
"/readyness_delay",
"/a/env"
]
}
```


Expand Down
198 changes: 198 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
// Package docs Code generated by swaggo/swag. DO NOT EDIT
package docs

import "github.com/swaggo/swag"

const docTemplate = `{
"schemes": {{ marshal .Schemes }},
"swagger": "2.0",
"info": {
"description": "{{escape .Description}}",
"title": "{{.Title}}",
"contact": {},
"version": "{{.Version}}"
},
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/debug": {
"get": {
"description": "Get lots of info from running container headers/ips",
"produces": [
"application/json"
],
"summary": "Debug",
"operationId": "debug",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
}
}
}
},
"/env": {
"get": {
"security": [
{
"BearerAuth": []
}
],
"description": "Get the env variables available to the api. This is behind auth",
"produces": [
"application/json"
],
"summary": "Get environment variables",
"operationId": "env",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"401": {
"description": "Unauthorized",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
}
},
"/headers": {
"get": {
"description": "Get the headers recieved by the api",
"produces": [
"application/json"
],
"summary": "Get headers",
"operationId": "headers",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
}
}
}
},
"/health": {
"get": {
"description": "Get the health of the api",
"produces": [
"application/json"
],
"summary": "Get health",
"operationId": "health",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
}
}
}
},
"/healthz": {
"get": {
"description": "Get the health of the api",
"produces": [
"application/json"
],
"summary": "Get healthz",
"operationId": "healthz",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
}
}
}
},
"/ping": {
"get": {
"description": "Get the readyness of the api",
"produces": [
"application/json"
],
"summary": "Get ping",
"operationId": "ping",
"responses": {
"200": {
"description": "PONG",
"schema": {
"type": "string"
}
}
}
}
},
"/ready": {
"get": {
"description": "Get the readyness of the api",
"produces": [
"application/json"
],
"summary": "Get ready",
"operationId": "ready",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
}
}
}
},
"/readyz": {
"get": {
"description": "Get the readyness of the api",
"produces": [
"application/json"
],
"summary": "Get readyz",
"operationId": "readyz",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
}
}
}
}
}
}`

// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = &swag.Spec{
Version: "2.0",
Host: "localhost:8080",
BasePath: "/",
Schemes: []string{},
Title: "Cluster Util API",
Description: "This is a util api which lots of endpoints making it easy to test routing/ingress/egress",
InfoInstanceName: "swagger",
SwaggerTemplate: docTemplate,
LeftDelim: "{{",
RightDelim: "}}",
}

func init() {
swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo)
}
Loading

0 comments on commit b31187e

Please sign in to comment.