Skip to content

Commit

Permalink
add json support
Browse files Browse the repository at this point in the history
  • Loading branch information
futuretea committed Aug 29, 2019
1 parent 35c9cd7 commit e97cbe9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
vendor
yapidoc
dist
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ docker run -itd --name yapidoc -e YAPI_URL=your_yapi_url -p 8888:8888 futuretea/
```
2. download doc from the yapidoc server
```javascript
http://127.0.0.1:8888/projects/your_project_token?tag=your_api_tag
http://127.0.0.1:8888/projects/your_project_token
http://127.0.0.1:8888/projects/your_project_token?format=json&tag=your_api_tag
http://127.0.0.1:8888/projects/your_project_token?format=markdown&tag=your_api_tag
```

3. stop the yapidoc server
Expand Down
21 changes: 19 additions & 2 deletions router/router.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package router

import (
"fmt"
"github.com/futuretea/yapidoc/conf"
"github.com/gin-gonic/gin"
"net/http"
)

var (
GinEngine *gin.Engine
suffixMap = map[string]string{
"markdown": "md",
"json": "json",
}
)

func init() {
Expand All @@ -18,8 +23,20 @@ func init() {
token := c.Param("token")
tag := c.DefaultQuery("tag", "")
result := render(c, yapiURL, token, tag)
format := c.DefaultQuery("format", "markdown")
suffix, formatIsSupport := suffixMap[format]
if formatIsSupport == false {
c.AbortWithStatus(404)
}
fileName := fmt.Sprintf("%s.api.%s", tag, suffix)
c.Header("Content-Type", "application/text/plain")
c.Header("Content-Disposition", "attachment; filename=api.md")
c.HTML(http.StatusOK, "api.md", result)
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", fileName))
if format == "markdown" {
c.HTML(http.StatusOK, "api.md", result)
} else if format == "json" {
c.JSON(http.StatusOK, result)
} else {
c.AbortWithStatus(404)
}
})
}

0 comments on commit e97cbe9

Please sign in to comment.