Skip to content

Commit

Permalink
Dynamically inject api version inside swagger and add basic auth auth…
Browse files Browse the repository at this point in the history
…entication.
  • Loading branch information
gfyrag committed Feb 4, 2022
1 parent a4fe5fa commit 151a47a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
23 changes: 18 additions & 5 deletions pkg/api/controllers/config_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controllers

import (
"bytes"
"encoding/json"
"gopkg.in/yaml.v3"
"net/http"

Expand Down Expand Up @@ -56,16 +57,28 @@ func (ctl *ConfigController) GetInfo(c *gin.Context) {
//go:embed swagger.yaml
var swagger string

func parseSwagger(version string) map[string]interface{} {
ret := make(map[string]interface{})
err := yaml.NewDecoder(bytes.NewBufferString(swagger)).Decode(&ret)
if err != nil {
panic(err)
}
ret["info"].(map[string]interface{})["version"] = version
return ret
}

func (ctl *ConfigController) GetDocsAsYaml(c *gin.Context) {
c.Writer.Write([]byte(swagger))
err := yaml.NewEncoder(c.Writer).Encode(parseSwagger(ctl.Version))
if err != nil {
panic(err)
}
}

func (ctl *ConfigController) GetDocsAsJSON(c *gin.Context) {
ret := make(map[string]interface{})
err := yaml.NewDecoder(bytes.NewBufferString(swagger)).Decode(&ret)
enc := json.NewEncoder(c.Writer)
enc.SetIndent("", " ")
err := enc.Encode(parseSwagger(ctl.Version))
if err != nil {
panic(err)
}

c.JSON(http.StatusOK, ret)
}
8 changes: 8 additions & 0 deletions pkg/api/controllers/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,15 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/TransactionCommitErrorResponse'

security:
- basicAuth: []

components:
securitySchemes:
basicAuth:
type: http
scheme: basic
schemas:
Config:
type: object
Expand Down

0 comments on commit 151a47a

Please sign in to comment.