Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/gbaeke/go-template into main
Browse files Browse the repository at this point in the history
  • Loading branch information
gbaeke committed Jan 3, 2021
2 parents de28cc7 + 24a9307 commit b01a232
Show file tree
Hide file tree
Showing 8 changed files with 343 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ docker-build:

docker-push:
docker build -t $(IMAGE) .
docker push $(IMAGE)
docker push $(IMAGE)

swagger:
cd pkg/api ; swag init -g server.go
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@ module github.com/gbaeke/go-template
go 1.15

require (
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/go-openapi/spec v0.20.0 // indirect
github.com/gorilla/mux v1.8.0
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.7.1
github.com/swaggo/http-swagger v1.0.0
github.com/swaggo/swag v1.7.0
go.uber.org/zap v1.16.0
golang.org/x/net v0.0.0-20201224014010-6772e930b67b // indirect
golang.org/x/tools v0.0.0-20210102185154-773b96fafca2 // indirect
)
92 changes: 92 additions & 0 deletions go.sum

Large diffs are not rendered by default.

112 changes: 112 additions & 0 deletions pkg/api/docs/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// This file was generated by swaggo/swag

package docs

import (
"bytes"
"encoding/json"
"strings"

"github.com/alecthomas/template"
"github.com/swaggo/swag"
)

var doc = `{
"schemes": {{ marshal .Schemes }},
"swagger": "2.0",
"info": {
"description": "{{.Description}}",
"title": "{{.Title}}",
"contact": {
"name": "Source Code",
"url": "https://github.com/gbaeke/go-template"
},
"version": "{{.Version}}"
},
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/healthz": {
"get": {
"description": "Kubernetes uses this as liveness probe",
"consumes": [
"application/json"
],
"summary": "Liveness probe",
"responses": {
"200": {
"description": "ok",
"schema": {
"type": "string"
}
}
}
}
},
"/readyz": {
"get": {
"description": "Kubernetes uses this as readiness probe",
"consumes": [
"application/json"
],
"summary": "Readiness probe",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "boolean"
}
}
}
}
}
}
}`

type swaggerInfo struct {
Version string
Host string
BasePath string
Schemes []string
Title string
Description string
}

// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = swaggerInfo{
Version: "0.1",
Host: "localhost:8080",
BasePath: "/",
Schemes: []string{"http", "https"},
Title: "go-template API",
Description: "Go template",
}

type s struct{}

func (s *s) ReadDoc() string {
sInfo := SwaggerInfo
sInfo.Description = strings.Replace(sInfo.Description, "\n", "\\n", -1)

t, err := template.New("swagger_info").Funcs(template.FuncMap{
"marshal": func(v interface{}) string {
a, _ := json.Marshal(v)
return string(a)
},
}).Parse(doc)
if err != nil {
return doc
}

var tpl bytes.Buffer
if err := t.Execute(&tpl, sInfo); err != nil {
return doc
}

return tpl.String()
}

func init() {
swag.Register(swag.Name, &s{})
}
54 changes: 54 additions & 0 deletions pkg/api/docs/swagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"schemes": [
"http",
"https"
],
"swagger": "2.0",
"info": {
"description": "Go template",
"title": "go-template API",
"contact": {
"name": "Source Code",
"url": "https://github.com/gbaeke/go-template"
},
"version": "0.1"
},
"host": "localhost:8080",
"basePath": "/",
"paths": {
"/healthz": {
"get": {
"description": "Kubernetes uses this as liveness probe",
"consumes": [
"application/json"
],
"summary": "Liveness probe",
"responses": {
"200": {
"description": "ok",
"schema": {
"type": "string"
}
}
}
}
},
"/readyz": {
"get": {
"description": "Kubernetes uses this as readiness probe",
"consumes": [
"application/json"
],
"summary": "Readiness probe",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "boolean"
}
}
}
}
}
}
}
36 changes: 36 additions & 0 deletions pkg/api/docs/swagger.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
basePath: /
host: localhost:8080
info:
contact:
name: Source Code
url: https://github.com/gbaeke/go-template
description: Go template
title: go-template API
version: "0.1"
paths:
/healthz:
get:
consumes:
- application/json
description: Kubernetes uses this as liveness probe
responses:
"200":
description: ok
schema:
type: string
summary: Liveness probe
/readyz:
get:
consumes:
- application/json
description: Kubernetes uses this as readiness probe
responses:
"200":
description: OK
schema:
type: boolean
summary: Readiness probe
schemes:
- http
- https
swagger: "2.0"
12 changes: 12 additions & 0 deletions pkg/api/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,22 @@ import (
"net/http"
)

// @Summary Liveness probe
// @Description Kubernetes uses this as liveness probe
// @Accept json
// @Produces json
// @Router /healthz [get]
// @Success 200 {string} string "ok"
func (s *Server) healthz(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
}

// @Summary Readiness probe
// @Description Kubernetes uses this as readiness probe
// @Accept json
// @Produces json
// @Router /readyz [get]
// @Success 200 {boolean} boolean true
func (s *Server) readyz(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(map[string]bool{"ready": true})
}
25 changes: 25 additions & 0 deletions pkg/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,23 @@ import (

"github.com/gorilla/mux"
"go.uber.org/zap"

_ "github.com/gbaeke/go-template/pkg/api/docs"
httpSwagger "github.com/swaggo/http-swagger"
"github.com/swaggo/swag"
)

// @title go-template API
// @version 0.1
// @description Go template

// @contact.name Source Code
// @contact.url https://github.com/gbaeke/go-template

// @host localhost:8080
// @BasePath /
// @schemes http https

//Config API configuration via viper
type Config struct {
Welcome string
Expand Down Expand Up @@ -44,6 +59,16 @@ func (s *Server) setupRoutes() {
s.router.HandleFunc("/healthz", s.healthz)
s.router.HandleFunc("/readyz", s.readyz)
s.router.HandleFunc("/", s.indexHandler)
s.router.PathPrefix("/swagger/").Handler(httpSwagger.Handler(
httpSwagger.URL("/swagger/doc.json"),
))
s.router.HandleFunc("/swagger.json", func(w http.ResponseWriter, r *http.Request) {
doc, err := swag.ReadDoc()
if err != nil {
s.logger.Error("swagger error", zap.Error(err), zap.String("path", "/swagger.json"))
}
w.Write([]byte(doc))
})
}

func (s *Server) setupMiddlewares() {
Expand Down

0 comments on commit b01a232

Please sign in to comment.