Skip to content

Commit

Permalink
Add swagger docs
Browse files Browse the repository at this point in the history
  • Loading branch information
tadaskay committed Mar 30, 2021
1 parent 936b6d5 commit e15d87b
Show file tree
Hide file tree
Showing 14 changed files with 792 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ RUN apk add --no-cache ca-certificates git
# Install application
COPY --from=builder /go/src/github.com/mysteriumnetwork/discovery/build/discovery /usr/bin/discovery

EXPOSE 3000
EXPOSE 8080

CMD ["/usr/bin/discovery"]
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ Or using docker:
docker-compose up
```

API: http://localhost:3000/api/v3
Docs: http://localhost:8080/swagger/index.html
API: http://localhost:8080/api/v3
54 changes: 54 additions & 0 deletions ci/swagger/swagger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) 2021 BlockDev AG
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

package swagger

import (
"os"

"github.com/fatih/color"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/mysteriumnetwork/go-ci/util"
)

// Installs the swag generation tool
func swagInstall() error {
err := sh.RunV("go", "get", "-u", "github.com/swaggo/swag/cmd/swag")
if err != nil {
color.Red("Could not go get swag")
return err
}
return nil
}

// Swag generates the swagger documents
func Swag() error {
mg.Deps(swagInstall)
swag, err := util.GetGoBinaryPath("swag")
if err != nil {
color.Red("Could not find swag")
return err
}

color.Cyan("Removing old docs")
err = os.RemoveAll("./docs")
if err != nil {
return err
}
color.Cyan("Old docs removed")

color.Cyan("Generating swagger docs with %s", swag)
err = sh.RunV(
swag, "init",
"--generalInfo", "main.go",
"--dir", "cmd",
"--parseDependency",
)
if err != nil {
return err
}
color.Green("Generating docs DONE!")
return nil
}
12 changes: 11 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,23 @@ import (
"github.com/gin-gonic/gin"
"github.com/mysteriumnetwork/discovery/config"
"github.com/mysteriumnetwork/discovery/db"
_ "github.com/mysteriumnetwork/discovery/docs"
"github.com/mysteriumnetwork/discovery/listener"
"github.com/mysteriumnetwork/discovery/proposal"
"github.com/mysteriumnetwork/discovery/quality"
"github.com/mysteriumnetwork/discovery/quality/oracleapi"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)

var Version = "<dev>"

// @title Discovery API
// @version 3.0
// @BasePath /api/v3
// @description Discovery API for Mysterium Network
func main() {
configureLogger()
printBanner()
Expand All @@ -31,14 +38,17 @@ func main() {
}

r := gin.Default()
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

rdb := db.New(cfg.DBHost, cfg.DBPassword)
proposalRepo := proposal.NewRepository(rdb)
qualityOracleAPI := oracleapi.New(cfg.QualityOracleURL.String())
qualityService := quality.NewService(qualityOracleAPI, rdb)
proposalService := proposal.NewService(proposalRepo, qualityService)

proposal.NewAPI(proposalService).RegisterRoutes(r)
v3 := r.Group("/api/v3")

proposal.NewAPI(proposalService).RegisterRoutes(v3)

brokerListener := listener.New(cfg.BrokerURL.String(), proposalRepo)

Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ services:
build:
context: .
ports:
- 3000:3000
- 8080:8080
environment:
- PORT=3000
- PORT=8080
- DB_HOST=db:6379
- DB_PASSWORD=
- QUALITY_ORACLE_URL=https://testnet2-quality.mysterium.network
Expand Down
242 changes: 242 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
// 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": {},
"version": "{{.Version}}"
},
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {
"/ping": {
"get": {
"description": "Ping",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"summary": "Ping",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/proposal.PingResponse"
}
}
}
}
},
"/proposals": {
"get": {
"description": "List proposals",
"consumes": [
"application/json"
],
"summary": "List proposals",
"parameters": [
{
"type": "string",
"description": "Consumer country",
"name": "from",
"in": "query"
},
{
"type": "string",
"description": "Service type",
"name": "service_type",
"in": "query"
},
{
"type": "string",
"description": "Provider country",
"name": "country",
"in": "query"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/v2.Proposal"
}
}
}
}
}
}
},
"definitions": {
"proposal.PingResponse": {
"type": "object",
"properties": {
"message": {
"type": "string"
}
}
},
"v2.AccessPolicy": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"source": {
"type": "string"
}
}
},
"v2.Contact": {
"type": "object",
"properties": {
"definition": {
"type": "object"
},
"type": {
"type": "string"
}
}
},
"v2.Location": {
"type": "object",
"properties": {
"asn": {
"type": "integer"
},
"city": {
"type": "string"
},
"continent": {
"type": "string"
},
"country": {
"type": "string"
},
"ip_type": {
"type": "string"
},
"isp": {
"type": "string"
}
}
},
"v2.Price": {
"type": "object",
"properties": {
"currency": {
"type": "string"
},
"per_gib": {
"type": "integer"
},
"per_hour": {
"type": "integer"
}
}
},
"v2.Proposal": {
"type": "object",
"properties": {
"access_policies": {
"type": "array",
"items": {
"$ref": "#/definitions/v2.AccessPolicy"
}
},
"compatibility": {
"type": "integer"
},
"contacts": {
"type": "array",
"items": {
"$ref": "#/definitions/v2.Contact"
}
},
"format": {
"type": "string"
},
"location": {
"$ref": "#/definitions/v2.Location"
},
"price": {
"$ref": "#/definitions/v2.Price"
},
"provider_id": {
"type": "string"
},
"quality": {
"type": "number"
},
"service_type": {
"type": "string"
}
}
}
}
}`

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: "3.0",
Host: "",
BasePath: "/api/v3",
Schemes: []string{},
Title: "Discovery API",
Description: "Discovery API for Mysterium Network",
}

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{})
}
Loading

0 comments on commit e15d87b

Please sign in to comment.